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.

2.6 KB
Raw Blame
import { ListTree, SquareCheck } from "lucide-react";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { Tooltip, TooltipPopup, TooltipTrigger } from "@/components/ui/tooltip";
import { cn } from "@/lib/cn";
import { getTaskItemStats } from "@/lib/get-task-item-stats";
import { useUserPreferencesStore } from "@/store/user-preferences";
import type Task from "@/types/task";

export function TaskProgressBadges({
  task,
  asText = false,
}: {
  task: Pick<Task, "description" | "descriptionDeferred" | "subtaskCounts">;
  asText?: boolean;
}) {
  const { t } = useTranslation();
  const { showTaskItemCounts } = useUserPreferencesStore();
  const checklist = useMemo(
    () =>
      showTaskItemCounts && !task.descriptionDeferred
        ? getTaskItemStats(task.description)
        : null,
    [task.description, task.descriptionDeferred, showTaskItemCounts],
  );
  const counters = [
    {
      kind: "subtasks",
      counts: task.subtaskCounts,
      Icon: ListTree,
      label: t("tasks:subtasks.progress", {
        completed: task.subtaskCounts?.completed ?? 0,
        total: task.subtaskCounts?.total ?? 0,
      }),
    },
    {
      kind: "checklist",
      counts: showTaskItemCounts ? checklist : undefined,
      Icon: SquareCheck,
      label: t("tasks:checklistProgress", {
        completed: checklist?.completed ?? 0,
        total: checklist?.total ?? 0,
      }),
    },
  ];

  return counters.map(({ kind, counts, Icon, label }) => {
    if (!counts || counts.total === 0) return null;
    const className = cn(
      "inline-flex h-5.5 shrink-0 cursor-inherit items-center gap-1 rounded border border-border/70 bg-muted/50 px-2 py-1 text-[10px] font-medium tabular-nums text-muted-foreground",
      counts.completed === counts.total &&
        "border-success/20 bg-success/10 text-success-foreground",
    );
    const content = (
      <>
        <Icon className="size-3" aria-hidden="true" />
        {counts.completed}/{counts.total}
      </>
    );

    // Public cards are buttons already; their badges must remain plain text.
    if (asText) {
      return (
        <span key={kind} className={className} title={label}>
          <span className="sr-only">{label}</span>
          <span aria-hidden="true" className="inline-flex items-center gap-1">
            {content}
          </span>
        </span>
      );
    }

    return (
      <Tooltip key={kind}>
        <TooltipTrigger aria-label={label} className={className}>
          {content}
        </TooltipTrigger>
        <TooltipPopup>{label}</TooltipPopup>
      </Tooltip>
    );
  });
}