1
0
Fork 0
polaris-task-force/src/components/frontend/intelligence/MissionCommandChainEditor.tsx
Z8MB1E ef3f26d4f9 feat(missions): add command chain editor and MissionCommandLinks collection
Introduce a per-mission command chain: a new MissionCommandLinks collection, a
lib/command-chain resolver, and an admin/mission-page editor for supervisor ->
subordinate links. Evaluations and mission reminders later consume this chain to
resolve equal-rank command relationships.
2026-11-02 09:59:55 -05:00

198 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"use client";
import { useState } from "react";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
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 { 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 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 (
<Card>
<CardHeader>
<CardTitle className="flex flex-wrap items-center justify-between gap-2 text-base">
<span>Command chain</span>
<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>
<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>
</Card>
);
}