import { useEffect, useState } from "react"; import { Badge, Box, Button, Group, SegmentedControl, Text } from "@mantine/core"; import { ShieldAlert } from "lucide-react"; import type { Freight } from "@edr/types"; import { useAssignRisk } from "@/hooks/contracts/useContracts"; import { ActionShell } from "./ActionShell"; const RISK_COLOR: Record = { GREEN: "green", YELLOW: "yellow", RED: "red", }; export function AssignRiskCard({ bookingId, milestone, locked = false, }: { bookingId: string; milestone: Freight.IClearanceMilestone; /** * Duty has already been advised off this risk level, so the decision is now * final. Until then a mis-assigned level must stay correctable — the server * accepts reassignment and overwrites the milestone metadata. */ locked?: boolean; }) { const assign = useAssignRisk(bookingId); const assigned = milestone.status === "COMPLETED"; const current = milestone.metadata?.riskLevel; const [level, setLevel] = useState( current ?? "GREEN", ); // The milestone loads (and refetches after a reassignment) after first render, // so mirror the persisted level onto the control whenever it changes. useEffect(() => { if (current) setLevel(current); }, [current]); return ( {current} ) : ( "Assigned" ) } > setLevel(v as Freight.CustomsRiskLevel)} data={[ { label: "Green", value: "GREEN" }, { label: "Yellow", value: "YELLOW" }, { label: "Red", value: "RED" }, ]} /> Customer is notified of the assigned risk. ); }