'use client' import { useEffect, useRef, type KeyboardEvent } from 'react' import { Check, AlertTriangle } from 'lucide-react' import { cn } from '@/lib/utils' // ─── Types ────────────────────────────────────────────────────────────────── export type StepStatus = 'idle' | 'active' | 'done' | 'error' export interface StepBarItem { id: string label: string status: StepStatus } interface StepBarProps { steps: StepBarItem[] onGoTo: (index: number) => void showLabels?: boolean } // ─── Helper ────────────────────────────────────────────────────────────────── function DotIcon({ status, index }: { status: StepStatus; index: number }) { if (status === 'done') return if (status === 'error') return return ( {index + 1} ) } // ─── Component ─────────────────────────────────────────────────────────────── export function StepBar({ steps, onGoTo, showLabels }: StepBarProps) { const btnRefs = useRef>([]) const total = steps.length const activeIdx = steps.findIndex((s) => s.status === 'active') const fillPct = total > 1 ? (Math.max(0, activeIdx) / (total - 1)) * 100 : 0 useEffect(() => { btnRefs.current[activeIdx]?.scrollIntoView({ behavior: 'smooth', inline: 'center', block: 'nearest', }) }, [activeIdx]) const isReachable = (i: number) => { const s = steps[i].status if (s === 'done' || s === 'active' || s === 'error') return true return i > 0 && steps[i - 1].status === 'done' } const handleKey = (e: KeyboardEvent, i: number) => { let next = i if (e.key === 'ArrowRight') next = Math.min(total - 1, i + 1) else if (e.key === 'ArrowLeft') next = Math.max(0, i - 1) else if (e.key === 'Home') next = 0 else if (e.key === 'End') next = total - 1 else return e.preventDefault() const dir = next > i ? 1 : -1 while (next !== i && !isReachable(next)) { next += dir if (next < 0 || next >= total) return } btnRefs.current[next]?.focus() } return ( ) }