import { createElement, useLayoutEffect, useRef, useState, type ReactNode } from "react"; import { useTranslation } from "react-i18next"; import { IconArchive, IconArrowLeft, IconArrowRight, IconChevronRight, IconFlag, IconLink, IconLogicBuffer, IconTrash, IconX, } from "@tabler/icons-react"; import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, } from "@kandev/ui/drawer"; import { cn } from "@/lib/utils"; import { TASK_PRIORITY_TOKENS, TASK_PRIORITY_LABEL_KEYS } from "@/lib/tasks/task-priority"; import { resolvePluginIcon } from "@/lib/plugins/icons"; import { taskMoveOptions, stepHasAutoStart, type TaskMoveStep } from "./task-move-context-menu"; import { taskLinkMenuOptions } from "./task-switcher-link-menu"; import { useTaskPluginLinkActions } from "./task-session-sidebar-link-actions"; import type { TaskManagementMenuProps } from "./task-management-menu"; import { StepProgressDetails } from "./workflow-step-progress-details"; import { useWorkflowStepProgress, type WorkflowStepProgress, } from "@/hooks/domains/kanban/use-workflow-step-progress"; export type TaskManagementDrawerProps = TaskManagementMenuProps & { onCloseAutoFocus: (event: Event) => void; }; type Page = "root" | "priority" | "steps" | "workflows" | "links" | { workflowId: string }; export function TaskManagementSheet({ title, taskTitle, onBack, onClose, onCloseAutoFocus, focusChoiceId, children, }: { title: string; taskTitle: string; onBack?: () => void; onClose: () => void; onCloseAutoFocus: (event: Event) => void; focusChoiceId?: string; children: ReactNode; }) { const { t } = useTranslation(); const titleRef = useRef(null); const bodyRef = useRef(null); useLayoutEffect(() => { const body = bodyRef.current; if (body) body.scrollTop = 0; const choice = focusChoiceId ? Array.from(body?.querySelectorAll("button") ?? []).find( (button) => button.dataset.testid === focusChoiceId, ) : undefined; (choice ?? titleRef.current)?.focus({ preventScroll: true }); choice?.scrollIntoView?.({ block: "nearest" }); }, [title, focusChoiceId]); return ( { if (!open) onClose(); }} > { event.preventDefault(); titleRef.current?.focus({ preventScroll: true }); }} onEscapeKeyDown={(event) => { if (onBack) { event.preventDefault(); onBack(); } }} >
{onBack && ( )} {title}
{taskTitle}
{children}
); } function Choice({ children, icon, nested, disabled, destructive, onClick, testId, }: { children: ReactNode; icon?: ReactNode; nested?: boolean; disabled?: boolean; destructive?: boolean; onClick?: () => void; testId?: string; }) { return ( ); } function StepChoices({ steps, currentStepId, disabled, onSelect, progressByStepId, agentLabelsByProfileId, }: { steps: TaskMoveStep[]; currentStepId?: string; disabled?: boolean; onSelect: (stepId: string) => void; progressByStepId: Readonly>; agentLabelsByProfileId: Readonly>; }) { const { t } = useTranslation(); return steps.map((step) => { const progress = progressByStepId[step.id]; return (
onSelect(step.id)} > {step.title} {step.id === currentStepId && ( {t("task:current2")} )} {stepHasAutoStart(step) && ( {t("task:autoStart")} )} {progress && ( )}
); }); } function PriorityChoices({ task, disabled, onPriority }: TaskManagementDrawerProps) { const { t } = useTranslation(); return TASK_PRIORITY_TOKENS.map((priority) => ( onPriority(priority)} > {t(TASK_PRIORITY_LABEL_KEYS[priority])} {task.priority === priority && ( {t("kanban:current")} )} )); } function DrawerChoices({ page, setPage, props, progressByStepId, agentLabelsByProfileId, }: { page: Page; setPage: (page: Page) => void; props: TaskManagementDrawerProps; progressByStepId: Readonly>; agentLabelsByProfileId: Readonly>; }) { const { t } = useTranslation(); const { task, workflows, stepsByWorkflowId, disabled, onMove, linkActions, closeMenu } = props; const { currentSteps, targets } = taskMoveOptions(task.workflowId, workflows, stepsByWorkflowId); const links = taskLinkMenuOptions(linkActions); const plugins = useTaskPluginLinkActions(task.id, task.repositoryLinks ?? []); if (page === "priority") return ; if (page === "steps") return ( { if (task.workflowId) onMove(task.workflowId, stepId); }} /> ); if (typeof page === "object") return ( onMove(page.workflowId, stepId)} /> ); if (page === "workflows") return targets.map((workflow) => ( setPage({ workflowId: workflow.id })} > {workflow.name} {!stepsByWorkflowId[workflow.id]?.length && ( {t("task:noSteps")} )} )); if (page === "links") return ( <> {links.map(({ id, labelKey, Icon, onSelect }) => ( } onClick={onSelect} > {t(labelKey)} ))} {plugins.map((action) => ( { closeMenu(); queueMicrotask(action.onSelect); }} > {action.label} ))} ); return ( 0 || plugins.length > 0} /> ); } function RootChoices({ props, setPage, hasLinks, }: { props: TaskManagementDrawerProps; setPage: (page: Page) => void; hasLinks: boolean; }) { const { t } = useTranslation(); const { task, workflows, stepsByWorkflowId, disabled, onArchive, onDelete } = props; const { canMove, canSend } = taskMoveOptions(task.workflowId, workflows, stepsByWorkflowId); return ( <> } onClick={() => setPage("priority")} > {t("kanban:priority")} {canMove && ( } onClick={() => setPage("steps")} > {t("task:moveTo")} )} {canSend && ( } onClick={() => setPage("workflows")} > {t("task:sendToWorkflow")} )} {hasLinks && ( } onClick={() => setPage("links")} > {t("task:link")} )} } onClick={onArchive}> {t("task:archive")}
} onClick={onDelete} > {t("task:delete")} ); } export function TaskManagementDrawer(props: TaskManagementDrawerProps) { const { t } = useTranslation(); const { progressByStepId, agentLabelsByProfileId } = useWorkflowStepProgress({ taskId: props.task.id, currentStepId: props.task.workflowStepId, taskProjection: { id: props.task.id, state: props.task.state, primarySessionId: props.task.primarySessionId, primarySessionState: props.task.sessionState, }, // The drawer receives the bounded task-row projection. Prefer its current // lifecycle over a stale rich session cache, while still using a loaded // primary session for cancellation_pending when that projection exists. preferTaskProjection: true, }); const [page, setPage] = useState("root"); const [focusChoiceId, setFocusChoiceId] = useState(); const navigate = (next: Page) => { setFocusChoiceId(undefined); setPage(next); }; const back = () => { setFocusChoiceId( typeof page === "object" ? `task-context-workflow-${page.workflowId}` : `task-management-page-${page}`, ); setPage(typeof page === "object" ? "workflows" : "root"); }; const title = typeof page === "object" ? (props.workflows.find((workflow) => workflow.id === page.workflowId)?.name ?? t("task:sendToWorkflow")) : t( { root: "task:taskActions", priority: "kanban:priority", steps: "task:moveTo", workflows: "task:sendToWorkflow", links: "task:link", }[page], ); return ( ); }