"use client";
import { IconClockHour4, IconMail } from "@tabler/icons-react";
import { Tooltip, TooltipContent, TooltipTrigger } from "@kandev/ui/tooltip";
import { Fragment, type ReactNode } from "react";
import { useTranslation } from "react-i18next";
import { useAppStore } from "@/components/state-provider";
import { isDebugUI } from "@/lib/config";
import type { SessionPollMode } from "@/lib/state/slices/session-runtime/types";
import { cn, formatRelativeTime } from "@/lib/utils";
import type { WipQueueStatus } from "@/lib/kanban/wip-queue";
import {
SIDEBAR_TASK_ROW_DETAIL_KEYS,
type SidebarTaskRowDetail,
} from "@/lib/state/slices/ui/sidebar-task-row-presentation";
/** Debug-overlay-only poll-mode legend. Labels resolve through `t()` so the
* pseudo-locale stays complete; the row itself is gated behind `isDebugUI()`. */
const POLL_MODE_CONFIG: Record<
SessionPollMode,
{ letter: string; color: string; labelKey: string }
> = {
fast: { letter: "F", color: "text-emerald-500", labelKey: "sidebar:pollModeFastLabel" },
slow: { letter: "S", color: "text-yellow-500", labelKey: "sidebar:pollModeSlowLabel" },
paused: {
letter: "P",
color: "text-muted-foreground/40",
labelKey: "sidebar:pollModePausedLabel",
},
};
/**
* The repository a task belongs to. The one element of the stats row allowed to
* shrink: a long `owner/repo` truncates rather than pushing the PR number and
* badges out of the row, and `title` keeps the full name reachable on hover.
*/
function RepositoryLabel({ label }: { label?: string }) {
if (!label) return null;
return (
{label}
);
}
function buildDetailNodes({
updatedAt,
repositoryLabel,
prInfo,
hasRelativeTime,
hasRepository,
hasPullRequestNumber,
}: {
updatedAt?: string;
repositoryLabel?: string;
prInfo?: { number: number; state: string; aggregateState?: string };
hasRelativeTime: boolean;
hasRepository: boolean;
hasPullRequestNumber: boolean;
}): Record {
return {
relative_time:
hasRelativeTime && updatedAt ? (
{formatRelativeTime(updatedAt)}
) : null,
repository: hasRepository ? : null,
pull_request_number:
hasPullRequestNumber && prInfo ? (
#{prInfo.number}
) : null,
};
}
function QueuedPromptCount({ count }: { count?: number }) {
const { t } = useTranslation();
if (!count) return null;
const label = t("sidebar:queuedPromptCount", { count });
return (
{count}
);
}
function WipQueueIndicator({ wipQueue }: { wipQueue?: WipQueueStatus }) {
const { t } = useTranslation();
if (!wipQueue) return null;
const label = t("sidebar:wipQueuePosition", {
position: wipQueue.position,
total: wipQueue.total,
step: wipQueue.destinationTitle,
});
return (
{label}
);
}
function PollModeIndicator({
pollMode,
config,
}: {
pollMode: SessionPollMode | null;
config: (typeof POLL_MODE_CONFIG)[SessionPollMode] | null;
}) {
const { t } = useTranslation();
if (!pollMode || !config) return null;
return (
{config.letter}
{t("task:gitPollMode", { mode: pollMode, label: t(config.labelKey) })}
);
}
function hasStatsToRender({
hasRelativeTime,
hasRepository,
hasPullRequestNumber,
pollMode,
queuedCount,
wipQueue,
}: {
hasRelativeTime: boolean;
hasRepository: boolean;
hasPullRequestNumber: boolean;
pollMode: SessionPollMode | null;
queuedCount?: number;
wipQueue?: WipQueueStatus;
}): boolean {
return Boolean(
hasRelativeTime || hasRepository || hasPullRequestNumber || pollMode || queuedCount || wipQueue,
);
}
/**
* The sidebar row's metadata line: relative last-update time, repository, PR number,
* queued-prompt mail badge, WIP queue icon, and (debug UI only) the session
* poll-mode letter.
*/
export function TaskItemStatsRow({
updatedAt,
repositoryLabel,
prInfo,
primarySessionId,
queuedCount,
wipQueue,
detailOrder = [...SIDEBAR_TASK_ROW_DETAIL_KEYS],
showRelativeTime = true,
showRepository = true,
showPullRequestNumber = true,
}: {
updatedAt?: string;
/** Repository the task belongs to; already resolved to a slug or name. */
repositoryLabel?: string;
prInfo?: { number: number; state: string; aggregateState?: string };
primarySessionId?: string | null;
queuedCount?: number;
wipQueue?: WipQueueStatus;
detailOrder?: SidebarTaskRowDetail[];
showRelativeTime?: boolean;
showRepository?: boolean;
showPullRequestNumber?: boolean;
}) {
const pollMode = useAppStore((s) =>
isDebugUI() && primarySessionId
? (s.sessionPollMode.bySessionId[primarySessionId] ?? null)
: null,
);
const hasRelativeTime = showRelativeTime && !!updatedAt;
const hasRepository = showRepository && !!repositoryLabel;
const hasPullRequestNumber = showPullRequestNumber && !!prInfo;
if (
!hasStatsToRender({
hasRelativeTime,
hasRepository,
hasPullRequestNumber,
pollMode,
queuedCount,
wipQueue,
})
) {
return null;
}
const detailNodes = buildDetailNodes({
updatedAt,
repositoryLabel,
prInfo,
hasRelativeTime,
hasRepository,
hasPullRequestNumber,
});
return (
{detailOrder.map((detail) =>
detailNodes[detail] ? {detailNodes[detail]} : null,
)}
);
}