Replace the header chevron button with a full-width rule and a centered toggle between the description and the player list.
223 lines
8.1 KiB
TypeScript
223 lines
8.1 KiB
TypeScript
"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<Record<number, number[]>>(() =>
|
||
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 (
|
||
<Collapsible open={contentOpen} onOpenChange={setContentOpen}>
|
||
<Card>
|
||
<CardHeader className="flex flex-col gap-2">
|
||
<CardTitle className="flex items-center gap-2 text-base">
|
||
<Badge variant="outline">
|
||
{mode === "stored" ? "Override" : mode === "legacy" ? "Legacy" : "Defaults"}
|
||
</Badge>
|
||
</CardTitle>
|
||
<CardDescription>
|
||
Who was in command of whom for this operation only. Permanent assignments are not
|
||
changed. Supervisors may rate their subordinate as a command review.
|
||
</CardDescription>
|
||
</CardHeader>
|
||
|
||
<CollapsibleTrigger asChild>
|
||
<div
|
||
className="relative mx-2 mt-1 mb-5 flex cursor-pointer items-center outline-none focus-visible:ring-1 focus-visible:ring-ring"
|
||
aria-label={contentOpen ? "Collapse command chain" : "Expand command chain"}
|
||
>
|
||
<span className="block h-px flex-1 border-t border-border" />
|
||
<ChevronDown
|
||
aria-hidden
|
||
className={cn(
|
||
"h-4 w-4 text-muted-foreground transition-transform hover:text-foreground",
|
||
contentOpen && "rotate-180",
|
||
)}
|
||
/>
|
||
<span className="block h-px flex-1 border-t border-border" />
|
||
</div>
|
||
</CollapsibleTrigger>
|
||
|
||
<CollapsibleContent className="overflow-hidden">
|
||
<CardContent className="flex flex-col gap-4">
|
||
{mode === "legacy" && (
|
||
<Alert>
|
||
<AlertDescription>
|
||
This mission was rated before the chain existed, so its command reviews follow the
|
||
original assignment rule and cannot be changed here.
|
||
</AlertDescription>
|
||
</Alert>
|
||
)}
|
||
{!editable && lockReason && (
|
||
<Alert>
|
||
<AlertDescription>{lockReason}</AlertDescription>
|
||
</Alert>
|
||
)}
|
||
|
||
{mode !== "legacy" && (
|
||
<div className="flex flex-col gap-3">
|
||
{participants.map((sub) => {
|
||
const supervisors = draft[sub.id] ?? [];
|
||
const options = participants.filter(
|
||
(p) => p.id !== sub.id && !supervisors.includes(p.id),
|
||
);
|
||
return (
|
||
<div key={sub.id} className="flex flex-col gap-1.5">
|
||
<Label>{sub.name}</Label>
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
{supervisors.length === 0 && (
|
||
<span className="text-sm text-muted-foreground">No supervisor</span>
|
||
)}
|
||
{supervisors.map((supId) => (
|
||
<Badge key={supId} variant="secondary" className="gap-1">
|
||
{nameOf(supId)}
|
||
{editable && (
|
||
<button
|
||
type="button"
|
||
aria-label={`Remove ${nameOf(supId)} as supervisor`}
|
||
onClick={() => removeSupervisor(sub.id, supId)}
|
||
className="ml-1 text-muted-foreground hover:text-foreground"
|
||
>
|
||
×
|
||
</button>
|
||
)}
|
||
</Badge>
|
||
))}
|
||
{editable && options.length > 0 && (
|
||
<Select onValueChange={(value) => addSupervisor(sub.id, Number(value))}>
|
||
<SelectTrigger aria-label={`Add supervisor for ${sub.name}`}>
|
||
<SelectValue placeholder="Add supervisor" />
|
||
</SelectTrigger>
|
||
<SelectContent>
|
||
{options.map((opt) => (
|
||
<SelectItem key={opt.id} value={String(opt.id)}>
|
||
{opt.name}
|
||
</SelectItem>
|
||
))}
|
||
</SelectContent>
|
||
</Select>
|
||
)}
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
</div>
|
||
)}
|
||
|
||
{editable && mode !== "legacy" && (
|
||
<div className="flex flex-wrap gap-2">
|
||
<Button onClick={save} disabled={pending}>
|
||
Save chain
|
||
</Button>
|
||
<Button variant="outline" onClick={reset} disabled={pending}>
|
||
Reset to defaults
|
||
</Button>
|
||
</div>
|
||
)}
|
||
</CardContent>
|
||
</CollapsibleContent>
|
||
</Card>
|
||
</Collapsible>
|
||
);
|
||
}
|