Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

5.6 KB
Raw Blame
import { Calendar, CalendarClock, CalendarX } from "lucide-react";
import { useRef } from "react";
import { TaskProgressBadges } from "@/components/task/task-progress-badges";
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
import {
  dueDateStatusColors,
  getDueDateStatus,
  isTaskCompleted,
} from "@/lib/due-date-status";
import { formatDateShort } from "@/lib/format";
import { getInitials } from "@/lib/get-initials";
import { getPriorityIcon } from "@/lib/priority";
import type { ExternalLink } from "@/types/external-link";
import type Task from "@/types/task";
import { TaskPullRequests } from "@/components/task/task-pull-requests";
import { PublicTaskLabels } from "./public-task-labels";

type PublicTaskCardProps = {
  task: Task & {
    labels?: Array<{ id: string; name: string; color: string }>;
    externalLinks?: Array<ExternalLink>;
  };
  projectSlug: string;
  // Passed by views that hold the column, so completion comes from isFinal
  // rather than the slug fallback.
  isCompleted?: boolean;
  onTaskClick: (task: Task) => void;
};

export function PublicTaskCard({
  task,
  projectSlug,
  isCompleted,
  onTaskClick,
}: PublicTaskCardProps) {
  const taskIsCompleted = isCompleted ?? isTaskCompleted(task.status);
  const labels = task.labels || [];
  const externalLinks = task.externalLinks || [];
  const touchStartRef = useRef<{ x: number; y: number; time: number } | null>(
    null,
  );

  const handleTouchStart = (e: React.TouchEvent) => {
    const touch = e.touches[0];
    touchStartRef.current = {
      x: touch.clientX,
      y: touch.clientY,
      time: Date.now(),
    };
  };

  const handleTouchEnd = (e: React.TouchEvent) => {
    if (!touchStartRef.current) return;

    const touch = e.changedTouches[0];
    const deltaX = Math.abs(touch.clientX - touchStartRef.current.x);
    const deltaY = Math.abs(touch.clientY - touchStartRef.current.y);
    const deltaTime = Date.now() - touchStartRef.current.time;

    if (deltaX > 10 || deltaY > 10 || deltaTime > 300) {
      e.preventDefault();
      touchStartRef.current = null;
      return;
    }

    touchStartRef.current = null;
    onTaskClick(task);
  };

  return (
    <div className="group relative w-full text-left p-3 bg-card border border-border rounded-lg cursor-pointer transition-[background-color,border-color,box-shadow] duration-150 ease-out hover:border-border/70 hover:shadow-sm">
      <button
        type="button"
        className="absolute inset-0 rounded-lg focus:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background"
        onTouchStart={handleTouchStart}
        onTouchEnd={handleTouchEnd}
        onClick={() => onTaskClick(task)}
        aria-label={`View details for task ${task.title}`}
      />
      <div className="pointer-events-none">
        <div className="text-[10px] font-mono text-muted-foreground mb-2">
          {projectSlug}-{task.number}
        </div>

        {task.assigneeName && (
          <div className="flex items-center gap-1.5 mb-2">
            <Avatar className="h-5 w-5">
              <AvatarImage
                src={task.assigneeImage ?? ""}
                alt={task.assigneeName ?? ""}
              />
              <AvatarFallback className="text-[10px] font-medium border border-border/30">
                {getInitials(task.assigneeName)}
              </AvatarFallback>
            </Avatar>
            <span className="text-[10px] text-muted-foreground font-medium truncate">
              {task.assigneeName}
            </span>
          </div>
        )}

        <div className="mb-3">
          <h3
            className="font-medium text-foreground text-sm leading-relaxed overflow-hidden break-words"
            style={{
              display: "-webkit-box",
              WebkitLineClamp: 3,
              WebkitBoxOrient: "vertical",
              wordBreak: "break-word",
              hyphens: "auto",
            }}
          >
            {task.title}
          </h3>
        </div>

        {labels.length > 0 && (
          <div className="mb-3">
            <PublicTaskLabels labels={labels} />
          </div>
        )}

        <div className="flex flex-wrap items-center gap-2">
          <TaskProgressBadges task={task} asText />
          {task.priority && (
            <span className="inline-flex items-center gap-1.5 px-2 py-1 rounded border border-border bg-sidebar text-[10px] font-medium text-muted-foreground">
              {getPriorityIcon(task.priority ?? "")}
            </span>
          )}

          {task.dueDate && (
            <div
              className={`flex items-center gap-1 text-[10px] px-2 py-1 rounded ${dueDateStatusColors[getDueDateStatus(task.dueDate, taskIsCompleted)]}`}
            >
              {getDueDateStatus(task.dueDate, taskIsCompleted) ===
                "overdue" && <CalendarX className="w-3 h-3" />}
              {getDueDateStatus(task.dueDate, taskIsCompleted) ===
                "due-soon" && <CalendarClock className="w-3 h-3" />}
              {(getDueDateStatus(task.dueDate, taskIsCompleted) ===
                "far-future" ||
                getDueDateStatus(task.dueDate, taskIsCompleted) ===
                  "no-due-date") && <Calendar className="w-3 h-3" />}
              <span>{formatDateShort(task.dueDate)}</span>
            </div>
          )}

          {externalLinks.length > 0 && (
            <div className="pointer-events-auto relative">
              <TaskPullRequests
                externalLinks={externalLinks}
                className="border-border bg-sidebar"
              />
            </div>
          )}
        </div>
      </div>
    </div>
  );
}