72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
|
|
// Auto-layout the pipeline graph with dagre, so adding a node in the backend
|
||
|
|
// graph needs no manual coordinates here — the map redraws itself.
|
||
|
|
import dagre from "@dagrejs/dagre";
|
||
|
|
import type { Edge, Node } from "reactflow";
|
||
|
|
import type { Topology, TopoNode } from "./api";
|
||
|
|
|
||
|
|
export const NODE_W = 184;
|
||
|
|
export const NODE_H = 72;
|
||
|
|
|
||
|
|
export interface RFData {
|
||
|
|
meta: TopoNode;
|
||
|
|
state: "idle" | "active" | "awaiting_human" | "parked";
|
||
|
|
count: number;
|
||
|
|
dimmed: boolean;
|
||
|
|
onPath: boolean;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Compute laid-out React Flow nodes + edges from the topology and live state.
|
||
|
|
// `onPath` marks the nodes a selected task has visited (highlighted); `dimmed`
|
||
|
|
// fades nodes outside the active task's path when one is selected.
|
||
|
|
export function layout(
|
||
|
|
topo: Topology,
|
||
|
|
liveNodes: Record<string, { state: RFData["state"]; count: number }>,
|
||
|
|
pathNodeIds: Set<string> | null,
|
||
|
|
): { nodes: Node<RFData>[]; edges: Edge[] } {
|
||
|
|
const g = new dagre.graphlib.Graph();
|
||
|
|
g.setGraph({ rankdir: "LR", nodesep: 36, ranksep: 72, marginx: 24, marginy: 24 });
|
||
|
|
g.setDefaultEdgeLabel(() => ({}));
|
||
|
|
|
||
|
|
for (const n of topo.nodes) g.setNode(n.id, { width: NODE_W, height: NODE_H });
|
||
|
|
for (const e of topo.edges) g.setEdge(e.from, e.to);
|
||
|
|
dagre.layout(g);
|
||
|
|
|
||
|
|
const nodes: Node<RFData>[] = topo.nodes.map((meta) => {
|
||
|
|
const pos = g.node(meta.id);
|
||
|
|
const live = liveNodes[meta.id];
|
||
|
|
const onPath = pathNodeIds ? pathNodeIds.has(meta.id) : false;
|
||
|
|
return {
|
||
|
|
id: meta.id,
|
||
|
|
type: "pipeline",
|
||
|
|
position: { x: pos.x - NODE_W / 2, y: pos.y - NODE_H / 2 },
|
||
|
|
data: {
|
||
|
|
meta,
|
||
|
|
state: live?.state ?? "idle",
|
||
|
|
count: live?.count ?? 0,
|
||
|
|
dimmed: pathNodeIds != null && !onPath,
|
||
|
|
onPath,
|
||
|
|
},
|
||
|
|
};
|
||
|
|
});
|
||
|
|
|
||
|
|
const edges: Edge[] = topo.edges.map((e) => {
|
||
|
|
const loop = e.kind === "loopback";
|
||
|
|
return {
|
||
|
|
id: `${e.from}->${e.to}`,
|
||
|
|
source: e.from,
|
||
|
|
target: e.to,
|
||
|
|
animated: false,
|
||
|
|
style: {
|
||
|
|
stroke: loop ? "#7a5cff" : e.kind === "branch" ? "#3f7fd8" : "#39414f",
|
||
|
|
strokeWidth: 1.5,
|
||
|
|
strokeDasharray: loop ? "5 4" : undefined,
|
||
|
|
},
|
||
|
|
label: loop ? "↺" : undefined,
|
||
|
|
labelStyle: { fill: "#9b8bff", fontWeight: 700 },
|
||
|
|
type: "smoothstep",
|
||
|
|
};
|
||
|
|
});
|
||
|
|
|
||
|
|
return { nodes, edges };
|
||
|
|
}
|