"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; import { ChevronDown } from "lucide-react"; import { cn } from "@/lib/utils"; import { Button } from "@/components/ui/button"; import { Badge } from "@/components/ui/badge"; import { Label } from "@/components/ui/label"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { resetMissionCommandChain, saveMissionCommandChain, } from "@/app/(frontend)/intelligence/missions/actions"; interface ChainParticipantView { id: number; name: string; } interface ChainLinkView { supervisorId: number; subordinateId: number; source: "default" | "override"; } interface MissionCommandChainEditorProps { missionId: number; mode: "stored" | "default" | "legacy"; links: ChainLinkView[]; participants: ChainParticipantView[]; editable: boolean; lockReason: string | null; } /** * Per-subordinate supervisor picker. Edits are held locally until Save; the * server re-validates everything (participants, rank, cycles, duplicates). */ export function MissionCommandChainEditor({ missionId, mode, links, participants, editable, lockReason, }: MissionCommandChainEditorProps) { const router = useRouter(); const [pending, setPending] = useState(false); const [contentOpen, setContentOpen] = useState(true); const initialSupervisors = (subordinateId: number) => links.filter((l) => l.subordinateId === subordinateId).map((l) => l.supervisorId); const [draft, setDraft] = useState>(() => Object.fromEntries(participants.map((p) => [p.id, initialSupervisors(p.id)])), ); const nameOf = (id: number) => participants.find((p) => p.id === id)?.name ?? `#${id}`; const save = async () => { setPending(true); const pairs = Object.entries(draft).flatMap(([sub, sups]) => sups.map((supervisorId) => ({ supervisorId, subordinateId: Number(sub) })), ); const result = await saveMissionCommandChain({ missionId, links: pairs }); setPending(false); if (!result.success) { toast.error(result.error ?? "Could not save the chain."); return; } toast.success("Command chain saved for this mission."); router.refresh(); }; const reset = async () => { setPending(true); const result = await resetMissionCommandChain({ missionId }); setPending(false); if (!result.success) { toast.error(result.error ?? "Could not reset the chain."); return; } toast.success("Chain reset to defaults."); router.refresh(); }; const addSupervisor = (subordinateId: number, supervisorId: number) => { setDraft((prev) => { const current = prev[subordinateId] ?? []; if (current.includes(supervisorId)) return prev; return { ...prev, [subordinateId]: [...current, supervisorId] }; }); }; const removeSupervisor = (subordinateId: number, supervisorId: number) => { setDraft((prev) => ({ ...prev, [subordinateId]: (prev[subordinateId] ?? []).filter((id) => id !== supervisorId), })); }; return ( {mode === "stored" ? "Override" : mode === "legacy" ? "Legacy" : "Defaults"} Who was in command of whom for this operation only. Permanent assignments are not changed. Supervisors may rate their subordinate as a command review.
{mode === "legacy" && ( This mission was rated before the chain existed, so its command reviews follow the original assignment rule and cannot be changed here. )} {!editable && lockReason && ( {lockReason} )} {mode !== "legacy" && (
{participants.map((sub) => { const supervisors = draft[sub.id] ?? []; const options = participants.filter( (p) => p.id !== sub.id && !supervisors.includes(p.id), ); return (
{supervisors.length === 0 && ( No supervisor )} {supervisors.map((supId) => ( {nameOf(supId)} {editable && ( )} ))} {editable && options.length > 0 && ( )}
); })}
)} {editable && mode !== "legacy" && (
)}
); }