import { useEffect, useMemo, useState } from "react"; import { Link, useSearchParams } from "react-router-dom"; import { api, type DependencyGraph, type DependencyGraphNode } from "../api/client"; import { ErrorNotice } from "./Notice"; import { PageSkeleton } from "./Skeleton"; import { readableError } from "../utils/errors"; import MapLinesToggle, { useMapLineStyle } from "../components/MapLinesToggle"; import { edgePath } from "../utils/mapLines"; import { t } from "../i18n"; const NODE_W = 200; const NODE_H = 46; const GAP_Y = 12; const COL_GAP = 120; const STATUS_SYMBOL: Record = { ok: "✓", bad: "✕", warn: "▲", unknown: "·" }; const STATUS_WORD: Record = { ok: "working", bad: "problem", warn: "check", unknown: "no status" }; const KIND_LABEL: Record = { server: "Server", integration: "Integration", device: "Network device", internet: "Internet" }; interface Placed extends DependencyGraphNode { x: number; y: number; layer: number; } /** * Columns by how deep each thing sits: what needs nothing on the left, then what needs only those, and so on. A column is ordered by where the * things it needs sit, so the lines cross as little as is easy. (A loop can't be made, but if one ever got in the layering gives up rather than spin.) */ function layout(graph: DependencyGraph): { placed: Map; width: number; height: number } { const needs = new Map(); for (const e of graph.edges) needs.set(e.to, [...(needs.get(e.to) ?? []), e.from]); const layer = new Map(graph.nodes.map((n) => [n.key, 0])); for (let pass = 0; pass < graph.nodes.length; pass++) { let moved = false; for (const n of graph.nodes) { const want = Math.max(-1, ...(needs.get(n.key) ?? []).map((k) => layer.get(k) ?? 0)) + 1; if (want > (layer.get(n.key) ?? 0)) { layer.set(n.key, want); moved = true; } } if (!moved) break; } const columns = new Map(); for (const n of graph.nodes) columns.set(layer.get(n.key) ?? 0, [...(columns.get(layer.get(n.key) ?? 0) ?? []), n]); const placed = new Map(); const step = NODE_H + GAP_Y; let height = 0; for (const depth of [...columns.keys()].sort((a, b) => a - b)) { const wanted = columns.get(depth)!.map((n) => { const ys = (needs.get(n.key) ?? []).map((k) => placed.get(k)?.y).filter((y): y is number => y !== undefined); return { n, y: ys.length ? ys.reduce((a, b) => a + b, 0) / ys.length : 0 }; }); wanted.sort((a, b) => a.y - b.y || a.n.name.localeCompare(b.n.name)); let next = 0; for (const { n, y } of wanted) { const at = Math.max(next, depth === 0 ? next : y); placed.set(n.key, { ...n, x: depth * (NODE_W + COL_GAP), y: at, layer: depth }); next = at + step; height = Math.max(height, at + NODE_H); } } const maxLayer = Math.max(0, ...[...placed.values()].map((p) => p.layer)); return { placed, width: maxLayer * (NODE_W + COL_GAP) + NODE_W, height }; } /** Everything reached from `start` by following edges one way (`forward`: to what needs it; otherwise to what it needs). */ function reach(graph: DependencyGraph, start: string, forward: boolean): Set { const out = new Set(); const queue = [start]; while (queue.length) { const cur = queue.shift()!; for (const e of graph.edges) { const [a, b] = forward ? [e.from, e.to] : [e.to, e.from]; if (a === cur && !out.has(b) && b !== start) { out.add(b); queue.push(b); } } } return out; } const names = (graph: DependencyGraph, keys: Iterable) => [...keys].map((k) => graph.nodes.find((n) => n.key === k)?.name ?? k).sort((a, b) => a.localeCompare(b)); /** * What needs what, drawn with the foundations on the left. Pick a box to see what would go down with it (red) and what it relies on (blue); * hovering does the same for a moment. This is the answer to "what breaks if I reboot this?" before doing it. */ export default function DependencyMap() { const [lineStyle] = useMapLineStyle(); const [graph, setGraph] = useState(null); const [error, setError] = useState(null); // From the command palette ("What depends on …?"): start with that box picked. const [params] = useSearchParams(); const [selected, setSelected] = useState(params.get("select")); const [hover, setHover] = useState(null); useEffect(() => { api.dependencies .graph() .then(setGraph) .catch((err) => setError(readableError(err))); }, []); const view = useMemo(() => (graph ? layout(graph) : null), [graph]); const active = hover ?? selected; const sets = useMemo(() => { if (!graph || !active) return null; return { down: reach(graph, active, true), up: reach(graph, active, false) }; }, [graph, active]); const activeNode = graph?.nodes.find((n) => n.key === active) ?? null; if (error) return ; if (!graph || !view) return ; if (graph.nodes.length === 0) { return (
Nothing depends on anything yet. Say what needs what below, or set which server another runs on, and it is drawn here.
); } const state = (key: string) => (!sets || !active ? "" : key === active ? "map-active" : sets.down.has(key) ? "map-down" : sets.up.has(key) ? "map-up" : "map-dim"); return (
{[...view.placed.values()].map((n) => ( ))}
{activeNode && sets ? (
{activeNode.name}{" "} open {selected !== active && (click to keep this)}
{sets.down.size ? `If it goes down, ${sets.down.size} ${sets.down.size === 1 ? "thing goes" : "things go"} with it: ${names(graph, sets.down).join(", ")}.` : t("Nothing depends on it.")}
{sets.up.size ? `It relies on ${names(graph, sets.up).join(", ")}.` : t("It relies on nothing here.")}
) : (
Lines run from what is needed (left) to what needs it (right). Dashed: “runs on”, set on the server. {t("Pick a box to see what would go down with it.")}
)}
); }