'use client'; import { memo, useCallback, useMemo, useState } from 'react'; import { cn } from '@kandev/ui/lib/utils'; import { HoverCard, HoverCardContent, HoverCardTrigger, } from '@kandev/ui/hover-card'; import { Button } from '@kandev/ui/button'; import { IconArrowRight } from '@tabler/icons-react'; import { moveTask } from '@/lib/api'; import { StepCapabilityIcons } from '@/components/step-capability-icons'; import type { KanbanStepEvents } from '@/lib/state/slices/kanban/types'; type Step = { id: string; name: string; color: string; position: number; events?: KanbanStepEvents; allow_manual_move?: boolean; prompt?: string; is_start_step?: boolean; }; type WorkflowStepperProps = { steps: Step[]; currentStepId: string | null; taskId?: string | null; workflowId?: string | null; isArchived?: boolean; }; const WorkflowStepper = memo(function WorkflowStepper({ steps, currentStepId, taskId, workflowId, isArchived, }: WorkflowStepperProps) { const [movingToStepId, setMovingToStepId] = useState(null); const sortedSteps = useMemo( () => [...steps].sort((a, b) => a.position - b.position), [steps] ); const currentIndex = useMemo( () => sortedSteps.findIndex((s) => s.id === currentStepId), [sortedSteps, currentStepId] ); const handleMove = useCallback( async (stepId: string) => { if (!taskId || !workflowId) return; setMovingToStepId(stepId); try { await moveTask(taskId, { workflow_id: workflowId, workflow_step_id: stepId, position: 0, }); } catch (err) { console.error('[WorkflowStepper] Failed to move task:', err); } finally { setMovingToStepId(null); } }, [taskId, workflowId] ); if (sortedSteps.length === 0) return null; return (
{sortedSteps.map((step, index) => { const isCompleted = !isArchived && currentIndex >= 0 && index < currentIndex; const isCurrent = !isArchived && index === currentIndex; const isAdjacent = currentIndex >= 0 && (index === currentIndex - 1 || index === currentIndex + 1); const canMove = !isArchived && !isCurrent && taskId && workflowId && (isAdjacent || step.allow_manual_move); return (
{/* Connector line before (except first) */} {index > 0 && (
)} {/* Step circle + label */}
{/* Circle indicator */} {isCurrent ? ( <> ) : isCompleted ? ( ) : ( )} {/* Label */} {step.name}
{canMove && ( )} {isCurrent && (
Current step
)}
); })} {isArchived && ( <>
Archived )}
); }); export { WorkflowStepper }; export type { Step as WorkflowStepperStep };