Replace the per-person supervisor list as the primary chain-of-command surface on the mission page with a layered DAG rendered on React Flow. Both views share one draft; connections stay editable until the first evaluation or cancellation locks them. - getChainViewerState: always returns state (viewer-aware), replacing getChainEditorState's null-for-viewers contract so plain viewers see the chain read-only - auto-layout wraps wide layers instead of shrinking past readability - tactical dossier styling for nodes and canvas chrome
142 lines
4.9 KiB
TypeScript
142 lines
4.9 KiB
TypeScript
/**
|
|
* Client-side mirror of the command-chain save rules (`validateChain` in
|
|
* `src/lib/command-chain`). Used by the chart canvas to reject bad connections
|
|
* at drop time and to flag invalid links already present in a draft. These
|
|
* checks are a pre-filter for UX only; the server action remains the authority
|
|
* and re-validates everything on save.
|
|
*/
|
|
|
|
export type ChainParticipantLite = {
|
|
id: number;
|
|
rankIndex: number | null;
|
|
};
|
|
|
|
export type ConnectRejection = "self" | "rank-inverted" | "duplicate" | "cycle";
|
|
|
|
export const CONNECT_REJECTION_MESSAGES: Record<ConnectRejection, string> = {
|
|
self: "A user cannot be their own supervisor.",
|
|
"rank-inverted": "Supervisors cannot rank below the person they command.",
|
|
duplicate: "That command link already exists.",
|
|
cycle: "That link would create a circular chain of command.",
|
|
};
|
|
|
|
/** Draft shape shared with the list editor: subordinate id -> supervisor ids. */
|
|
export type ChainDraft = Record<number, number[]>;
|
|
|
|
/**
|
|
* Whether adding `supervisorId -> subordinateId` closes a loop: the supervisor
|
|
* is themselves commanded (transitively) by the subordinate already, so the
|
|
* new edge would put someone above themselves.
|
|
*/
|
|
export function wouldCreateCycle(
|
|
draft: ChainDraft,
|
|
supervisorId: number,
|
|
subordinateId: number,
|
|
): boolean {
|
|
let frontier = draft[supervisorId] ?? [];
|
|
const visited = new Set<number>();
|
|
while (frontier.length > 0) {
|
|
const next: number[] = [];
|
|
for (const s of frontier) {
|
|
if (s === subordinateId) return true;
|
|
if (visited.has(s)) continue;
|
|
visited.add(s);
|
|
next.push(...(draft[s] ?? []));
|
|
}
|
|
frontier = next;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Validate one prospective connect. Returns null when the link may be added,
|
|
* otherwise the first matching rejection rule (same order as the server).
|
|
*/
|
|
export function checkConnect(
|
|
draft: ChainDraft,
|
|
participants: readonly ChainParticipantLite[],
|
|
supervisorId: number,
|
|
subordinateId: number,
|
|
): ConnectRejection | null {
|
|
if (supervisorId === subordinateId) return "self";
|
|
const rankOf = new Map(participants.map((p) => [p.id, p.rankIndex]));
|
|
const supRank = rankOf.get(supervisorId);
|
|
const subRank = rankOf.get(subordinateId);
|
|
if (supRank != null && subRank != null && supRank < subRank) return "rank-inverted";
|
|
if ((draft[subordinateId] ?? []).includes(supervisorId)) return "duplicate";
|
|
if (wouldCreateCycle(draft, supervisorId, subordinateId)) return "cycle";
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Auto-orient a canvas drag by rank: the higher-ranked endpoint becomes the
|
|
* supervisor. With equal ranks or an unranked side, the drag direction stands
|
|
* (both are legal per the save rules).
|
|
*/
|
|
export function autoOrientPair(
|
|
participants: readonly ChainParticipantLite[],
|
|
dragFrom: number,
|
|
dragTo: number,
|
|
): { supervisorId: number; subordinateId: number } {
|
|
const rankOf = new Map(participants.map((p) => [p.id, p.rankIndex]));
|
|
const fromRank = rankOf.get(dragFrom);
|
|
const toRank = rankOf.get(dragTo);
|
|
if (fromRank != null && toRank != null && fromRank < toRank) {
|
|
return { supervisorId: dragTo, subordinateId: dragFrom };
|
|
}
|
|
return { supervisorId: dragFrom, subordinateId: dragTo };
|
|
}
|
|
|
|
/**
|
|
* Keys (`"supervisor->subordinate"`) of draft links that break a rule, so the
|
|
* canvas can paint them red until they are fixed or removed. Rank/self checks
|
|
* are per edge; edges sitting on (or inside) a command loop are marked when
|
|
* both endpoints participate in some cycle. Mission-sized graphs only, so the
|
|
* naive upward walks here are fine.
|
|
*/
|
|
export function invalidLinkKeys(
|
|
draft: ChainDraft,
|
|
participants: readonly ChainParticipantLite[],
|
|
): Set<string> {
|
|
const invalid = new Set<string>();
|
|
const key = (s: number, u: number) => `${s}->${u}`;
|
|
const rankOf = new Map(participants.map((p) => [p.id, p.rankIndex]));
|
|
|
|
for (const [subordinateId, supervisorIds] of Object.entries(draft)) {
|
|
const u = Number(subordinateId);
|
|
const subRank = rankOf.get(u);
|
|
for (const s of supervisorIds) {
|
|
const supRank = rankOf.get(s);
|
|
if (s === u || (supRank != null && subRank != null && supRank < subRank)) {
|
|
invalid.add(key(s, u));
|
|
}
|
|
}
|
|
}
|
|
|
|
const cycleNodes = new Set<number>();
|
|
const walkUp = (start: number) => {
|
|
const path: number[] = [];
|
|
const stack = [...(draft[start] ?? [])];
|
|
while (stack.length > 0) {
|
|
const s = stack.pop()!;
|
|
if (s === start) {
|
|
cycleNodes.add(start);
|
|
for (const n of path) cycleNodes.add(n);
|
|
continue;
|
|
}
|
|
if (path.includes(s)) continue;
|
|
path.push(s);
|
|
stack.push(...(draft[s] ?? []));
|
|
}
|
|
};
|
|
for (const node of participants.map((p) => p.id)) walkUp(node);
|
|
if (cycleNodes.size > 0) {
|
|
for (const [subordinateId, supervisorIds] of Object.entries(draft)) {
|
|
const u = Number(subordinateId);
|
|
for (const s of supervisorIds) {
|
|
if (cycleNodes.has(s) && cycleNodes.has(u)) invalid.add(key(s, u));
|
|
}
|
|
}
|
|
}
|
|
return invalid;
|
|
}
|