/** * 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 = { 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; /** * 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(); 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 { const invalid = new Set(); 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(); 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; }