"use client"; import { useState } from "react"; import { toast } from "sonner"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Button } from "@/components/ui/button"; import { STATUS_LABELS, STATUS_ICONS } from "@/components/cases/status-badge"; import { useUpdateCase } from "@/lib/api/cases"; import { CASE_STATUSES, type CaseStatus } from "@/lib/api/case-status"; /* * Dropdown for manually overriding the case status — skip a step * or revert to an earlier stage. Calls PUT /api/cases/:caseNumber * with { status: newValue }. The option list is the SSoT lifecycle. */ const ALL_STATUSES: readonly CaseStatus[] = CASE_STATUSES; export function StatusChanger({ caseNumber, currentStatus, inline = false, }: { caseNumber: string; currentStatus?: CaseStatus; /** Compact horizontal layout for the status hero (mockup 18b hero-controls); * default is the stacked rail layout. */ inline?: boolean; }) { // `null` = untouched → the dropdown tracks the live `currentStatus` (which // arrives async and changes on the 5s poll / external updates). Only an // explicit pick overrides it, until save resets back to tracking. const [picked, setPicked] = useState(null); const mutate = useUpdateCase(caseNumber); const effective: CaseStatus | "" = picked ?? currentStatus ?? ""; const canSave = Boolean(effective) && effective !== currentStatus; const handleSave = async () => { if (!canSave || !effective) return; try { await mutate.mutateAsync({ status: effective }); toast.success(`הסטטוס עודכן ל${STATUS_LABELS[effective]}`); setPicked(null); } catch (e) { toast.error(e instanceof Error ? e.message : "שגיאה בעדכון הסטטוס"); } }; return (
{!inline && ( )}
); }