"use client"; import { forwardRef, useEffect, useRef, useState, type ComponentPropsWithoutRef, type FocusEvent, type RefObject, } from "react"; import { cn } from "@kandev/ui/lib/utils"; import { Button } from "@kandev/ui/button"; import { Drawer, DrawerContent, DrawerDescription, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@kandev/ui/drawer"; import { Popover, PopoverContent, PopoverTrigger } from "@kandev/ui/popover"; import { IconArrowRight, 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 { useTranslation } from "react-i18next"; 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: (stepId: string) => Promise; /** Notified whenever the disclosure surface opens or closes. */ onDisclosureOpenChange?: (open: boolean) => void; }; export function MinimalWorkflowStepper({ sortedSteps, currentIndex, isArchived, taskId, workflowId, movingToStepId, onMove, 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 CompactWorkflowDisclosureControls = { open: boolean; setOpen: (open: boolean) => void; triggerRef: RefObject; setTriggerRef: (node: HTMLButtonElement | null) => void; contentRef: RefObject; openDisclosure: () => void; openDisclosureFromFocus: () => void; scheduleClose: () => void; cancelScheduledClose: () => void; handleTriggerFocus: () => void; handleTriggerBlur: (event: FocusEvent) => void; handleContentFocus: () => void; handleContentBlur: (event: FocusEvent) => void; handleOpenAutoFocus: (event: Event) => void; handleCloseAutoFocus: (event: Event) => void; }; function isElementWithin( target: EventTarget | null, ref: RefObject, ): boolean { return target instanceof Node && ref.current?.contains(target) === true; } function useCompactDisclosureCloseTimer( triggerRef: RefObject, contentRef: RefObject, setOpen: (open: boolean) => void, ) { const closeTimerRef = useRef(null); const cancelScheduledClose = () => { if (closeTimerRef.current !== null) { window.clearTimeout(closeTimerRef.current); closeTimerRef.current = null; } }; const scheduleClose = () => { cancelScheduledClose(); closeTimerRef.current = window.setTimeout(() => { closeTimerRef.current = null; const activeElement = document.activeElement; const focusIsInsideDisclosure = isElementWithin(activeElement, triggerRef) || isElementWithin(activeElement, contentRef); if (!focusIsInsideDisclosure) setOpen(false); }, 100); }; useEffect( () => () => { if (closeTimerRef.current !== null) window.clearTimeout(closeTimerRef.current); }, [], ); return { cancelScheduledClose, scheduleClose }; } function useCompactWorkflowDisclosure(): CompactWorkflowDisclosureControls { const [open, setOpen] = useState(false); const triggerRef = useRef(null); const setTriggerRef = (node: HTMLButtonElement | null) => { triggerRef.current = node; }; const contentRef = useRef(null); const suppressFocusOpenRef = useRef(false); const openedFromFocusRef = useRef(false); const contentHasFocusRef = useRef(false); const { cancelScheduledClose, scheduleClose } = useCompactDisclosureCloseTimer( triggerRef, contentRef, setOpen, ); const openDisclosure = () => { openedFromFocusRef.current = false; cancelScheduledClose(); setOpen(true); }; const openDisclosureFromFocus = () => { openedFromFocusRef.current = !open; cancelScheduledClose(); setOpen(true); }; const handleTriggerFocus = () => { if (suppressFocusOpenRef.current) { suppressFocusOpenRef.current = false; return; } openDisclosureFromFocus(); }; const handleTriggerBlur = (event: FocusEvent) => { suppressFocusOpenRef.current = false; if (isElementWithin(event.relatedTarget, contentRef)) { cancelScheduledClose(); return; } scheduleClose(); }; const handleContentBlur = (event: FocusEvent) => { const relatedTarget = event.relatedTarget; if (isElementWithin(relatedTarget, contentRef)) { contentHasFocusRef.current = true; cancelScheduledClose(); return; } contentHasFocusRef.current = false; if (isElementWithin(relatedTarget, triggerRef)) { cancelScheduledClose(); return; } scheduleClose(); }; const handleContentFocus = () => { contentHasFocusRef.current = true; cancelScheduledClose(); }; const handleCloseAutoFocus = (event: Event) => { event.preventDefault(); if (contentHasFocusRef.current) { contentHasFocusRef.current = false; suppressFocusOpenRef.current = true; triggerRef.current?.focus(); } }; const handleOpenAutoFocus = (event: Event) => { if (!openedFromFocusRef.current) event.preventDefault(); openedFromFocusRef.current = false; }; return { open, setOpen, triggerRef, setTriggerRef, contentRef, openDisclosure, openDisclosureFromFocus, scheduleClose, cancelScheduledClose, handleTriggerFocus, handleTriggerBlur, handleContentFocus, handleContentBlur, handleOpenAutoFocus, handleCloseAutoFocus, }; } type CompactWorkflowTriggerProps = ComponentPropsWithoutRef<"button"> & { current: Step; currentIndex: number; total: number; usesTouchDrawer: boolean; controls: CompactWorkflowDisclosureControls; }; const CompactWorkflowTrigger = forwardRef( function CompactWorkflowTrigger( { current, currentIndex, total, usesTouchDrawer, controls, className, ...buttonProps }, ref, ) { const { t } = useTranslation(); const currentDisplayIndex = currentIndex >= 0 ? currentIndex : 0; return ( ); }, ); function CompactWorkflowStepDisclosure({ sortedSteps, current, currentIndex, isArchived, taskId, workflowId, movingToStepId, onMove, onDisclosureOpenChange, }: { sortedSteps: Step[]; current: Step; currentIndex: number; isArchived?: boolean; taskId: string; workflowId: string; movingToStepId: string | null; onMove: (stepId: string) => Promise; onDisclosureOpenChange?: (open: boolean) => void; }) { const { t } = useTranslation(); const usesTouchDrawer = useTouchDrawer(); const controls = useCompactWorkflowDisclosure(); useEffect(() => { onDisclosureOpenChange?.(controls.open); }, [controls.open, onDisclosureOpenChange]); useEffect( () => () => { onDisclosureOpenChange?.(false); }, [onDisclosureOpenChange], ); const trigger = ( ); const handleDisclosureMove = async (stepId: string) => { const moved = await onMove(stepId); if (moved) controls.setOpen(false); return moved; }; const content = ( ); if (usesTouchDrawer) { return ( {trigger} {t("task:moveTo")} {t("task:stepCount", { count: sortedSteps.length })} {content} ); } return ( {trigger} event.stopPropagation()} onMouseEnter={controls.openDisclosure} onMouseLeave={controls.scheduleClose} onFocusCapture={controls.handleContentFocus} onBlurCapture={controls.handleContentBlur} > {content} ); } function MinimalStepIndicator({ current, currentIndex, total, }: { current: Step; currentIndex: number; total: number; }) { return (
); } function MinimalStepContents({ current, currentIndex, total, }: { current: Step; currentIndex: number; total: number; }) { const displayIndex = currentIndex >= 0 ? currentIndex : 0; return ( <>
= 0 ? "step" : undefined} className="flex min-w-0 items-center gap-1.5 text-xs" > = 0} isCompleted={false} /> {current.name}
{total > 1 && ( {displayIndex + 1}/{total} )} ); } function StepDisclosureBody({ sortedSteps, currentIndex, isArchived, taskId, workflowId, movingToStepId, onMove, }: { sortedSteps: Step[]; currentIndex: number; isArchived?: boolean; taskId: string; workflowId: string; movingToStepId: string | null; onMove: (stepId: string) => Promise; }) { const { t } = useTranslation(); 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 (
{step.name}
{isCurrent ? ( {t("task:currentStep")} ) : ( canMove && ( ) )}
); })}
); } 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 type StepMarkerState = "current" | "completed" | "upcoming"; export function StepCircleIndicator({ isCurrent, isCompleted, }: { isCurrent: boolean; isCompleted: boolean; }) { let state: StepMarkerState = "upcoming"; if (isCurrent) state = "current"; else if (isCompleted) state = "completed"; if (isCurrent) { return ( ); } if (isCompleted) { return ( ); } return ( ); } 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"; }