"use client"; import { forwardRef, useEffect, useState, type ComponentPropsWithoutRef, type ReactNode, } from "react"; import { cn } from "@kandev/ui/lib/utils"; import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@kandev/ui/drawer"; import { Popover, PopoverContent, PopoverTrigger } from "@kandev/ui/popover"; import { IconChevronDown } from "@tabler/icons-react"; import { StepCapabilityIcons } from "@/components/step-capability-icons"; import { useTouchDrawer } from "@/hooks/use-compact-task-chrome"; import type { KanbanStepEvents } from "@/lib/state/slices/kanban/types"; import type { WorkflowMoveEntryOptions } from "@/lib/api/domains/kanban-api"; import { WorkflowMoveOptionsFields, useWorkflowMoveOptionsForm } from "./workflow-move-options"; import { useCompactWorkflowDisclosure, type CompactWorkflowDisclosureControls, } from "./workflow-step-disclosure-controls"; import type { WorkflowStepProgress } from "@/hooks/domains/kanban/use-workflow-step-progress"; import { StepProgressDetails, workflowStepProgressTranslationKey, } from "./workflow-step-progress-details"; import { StepCircleIndicator } from "./workflow-step-marker"; import { StepDisclosureRowActions } from "./workflow-step-disclosure-actions"; import { useTranslation } from "react-i18next"; /** Move callback shared by every compact-disclosure surface. A revealed, * filled options draft rides along as one-shot `entry_options`. */ export type DisclosureMove = ( stepId: string, entryOptions?: WorkflowMoveEntryOptions, ) => Promise; export type WorkflowStepperStep = { id: string; name: string; color: string; position: number; events?: KanbanStepEvents; allow_manual_move?: boolean; prompt?: string; is_start_step?: boolean; agent_profile_id?: string; }; type Step = WorkflowStepperStep; type MinimalWorkflowStepperProps = { sortedSteps: Step[]; currentIndex: number; isArchived?: boolean; taskId?: string | null; workflowId?: string | null; movingToStepId: string | null; onMove: DisclosureMove; progressByStepId?: Readonly>; agentLabelsByProfileId?: Readonly>; /** Notified whenever the disclosure surface opens or closes. */ onDisclosureOpenChange?: (open: boolean) => void; }; const EMPTY_PROGRESS_BY_STEP_ID: Readonly> = {}; const EMPTY_AGENT_LABELS_BY_PROFILE_ID: Readonly> = {}; export function MinimalWorkflowStepper({ sortedSteps, currentIndex, isArchived, taskId, workflowId, movingToStepId, onMove, progressByStepId = EMPTY_PROGRESS_BY_STEP_ID, agentLabelsByProfileId = EMPTY_AGENT_LABELS_BY_PROFILE_ID, onDisclosureOpenChange, }: MinimalWorkflowStepperProps) { const { t } = useTranslation(); if (isArchived) { return ( {t("task:filterDimensionArchived")} ); } const current = currentIndex >= 0 ? sortedSteps[currentIndex] : sortedSteps[0]; if (!current) return null; if (!taskId || !workflowId) { return ( ); } return ( ); } type CompactWorkflowTriggerProps = ComponentPropsWithoutRef<"button"> & { current: Step; currentIndex: number; total: number; progress?: WorkflowStepProgress; usesTouchDrawer: boolean; controls: CompactWorkflowDisclosureControls; }; const CompactWorkflowTrigger = forwardRef( function CompactWorkflowTrigger( { current, currentIndex, total, progress, usesTouchDrawer, controls, className, ...buttonProps }, ref, ) { const { t } = useTranslation(); const currentDisplayIndex = currentIndex >= 0 ? currentIndex : 0; const progressLabel = progress ? t(workflowStepProgressTranslationKey(progress.status)) : undefined; return ( ); }, ); function CompactWorkflowStepDisclosure({ sortedSteps, current, currentIndex, isArchived, taskId, workflowId, movingToStepId, onMove, progressByStepId, agentLabelsByProfileId, onDisclosureOpenChange, }: { sortedSteps: Step[]; current: Step; currentIndex: number; isArchived?: boolean; taskId: string; workflowId: string; movingToStepId: string | null; onMove: DisclosureMove; progressByStepId: Readonly>; agentLabelsByProfileId: Readonly>; onDisclosureOpenChange?: (open: boolean) => void; }) { const usesTouchDrawer = useTouchDrawer(); const controls = useCompactWorkflowDisclosure(); useEffect(() => { onDisclosureOpenChange?.(controls.open); }, [controls.open, onDisclosureOpenChange]); useEffect( () => () => { onDisclosureOpenChange?.(false); }, [onDisclosureOpenChange], ); const trigger = ( ); const handleDisclosureMove: DisclosureMove = async (stepId, entryOptions) => { const moved = await onMove(stepId, entryOptions); if (moved) controls.setOpen(false); return moved; }; const content = ( ); return ( ); } function CompactWorkflowDisclosureSurface({ trigger, content, controls, usesTouchDrawer, stepCount, }: { trigger: ReactNode; content: ReactNode; controls: CompactWorkflowDisclosureControls; usesTouchDrawer: boolean; stepCount: number; }) { const { t } = useTranslation(); if (usesTouchDrawer) { return ( {trigger} {t("task:moveTo")} {t("task:stepCount", { count: stepCount })} {content} ); } return ( {trigger} event.stopPropagation()} onMouseEnter={controls.openDisclosure} onMouseLeave={controls.scheduleClose} onFocusCapture={controls.handleContentFocus} onBlurCapture={controls.handleContentBlur} > {content} ); } function MinimalStepIndicator({ current, currentIndex, total, progress, }: { current: Step; currentIndex: number; total: number; progress?: WorkflowStepProgress; }) { return (
); } function MinimalStepContents({ current, currentIndex, total, progress, }: { current: Step; currentIndex: number; total: number; progress?: WorkflowStepProgress; }) { const { t } = useTranslation(); const displayIndex = currentIndex >= 0 ? currentIndex : 0; const pendingLabel = progress?.isPending ? t(workflowStepProgressTranslationKey(progress.status)) : undefined; return ( <>
= 0 ? "step" : undefined} className="flex min-w-0 items-center gap-1.5 text-xs" > = 0} isCompleted={false} isPending={progress?.isPending} pendingLabel={pendingLabel} /> {current.name}
{total > 1 && ( {displayIndex + 1}/{total} )} ); } function StepDisclosureBody({ sortedSteps, currentIndex, isArchived, taskId, workflowId, movingToStepId, isTouchSurface, progressByStepId, agentLabelsByProfileId, onMove, }: { sortedSteps: Step[]; currentIndex: number; isArchived?: boolean; taskId: string; workflowId: string; movingToStepId: string | null; isTouchSurface: boolean; progressByStepId: Readonly>; agentLabelsByProfileId: Readonly>; onMove: DisclosureMove; }) { return (
{sortedSteps.map((step, index) => { const isCurrent = index === currentIndex; const isCompleted = !isArchived && currentIndex >= 0 && index < currentIndex; const isAdjacent = currentIndex >= 0 && (index === currentIndex - 1 || index === currentIndex + 1); const canMove = canMoveToStep({ isArchived, isCurrent, taskId, workflowId, isAdjacent, allowManualMove: step.allow_manual_move, }); return ( ); })}
); } /** * A single step choice in the compact disclosure. Non-current, movable steps * carry the same opt-in one-time move options as the full stepper: the fields * stay hidden until the user reveals them, so a quick tap keeps the zero-config * move while a revealed, filled draft rides along as one-shot `entry_options`. * A successful move unmounts the row (the disclosure closes); a failed move * keeps the disclosure open so the draft the user typed is preserved. */ function StepDisclosureRow({ step, isCurrent, isCompleted, canMove, isMoving, movePending, isTouchSurface, progress, agentLabelsByProfileId, onMove, }: { step: Step; isCurrent: boolean; isCompleted: boolean; canMove: boolean; isMoving: boolean; movePending: boolean; isTouchSurface: boolean; progress?: WorkflowStepProgress; agentLabelsByProfileId: Readonly>; onMove: DisclosureMove; }) { const { t } = useTranslation(); const [showOptions, setShowOptions] = useState(false); const { draft, patchDraft } = useWorkflowMoveOptionsForm(); const buttonSizeClass = isTouchSurface ? "h-11" : "h-7 [@media(pointer:coarse)]:h-11"; return (
{step.name}
{isCurrent ? ( {t("task:currentStep")} ) : ( canMove && ( setShowOptions((value) => !value)} onMove={onMove} /> ) )}
{progress && ( )} {canMove && !isCurrent && showOptions && (
event.stopPropagation()} data-testid={`workflow-step-disclosure-options-panel-${step.id}`} >
)}
); } export function canMoveToStep(params: { isArchived: boolean | undefined; isCurrent: boolean; taskId: string | null | undefined; workflowId: string | null | undefined; isAdjacent: boolean; allowManualMove: boolean | undefined; }): boolean { if (params.isArchived || params.isCurrent || !params.taskId || !params.workflowId) return false; return params.isAdjacent || !!params.allowManualMove; } export function getStepLabelClass(isCurrent: boolean, isCompleted: boolean): string { if (isCurrent) return "text-foreground font-medium"; if (isCompleted) return "text-muted-foreground"; return "text-muted-foreground/60"; }