Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

fix(graph): reconcile controlled selection and preserve routing groundwork

Rahman52ca1b4b6833
diff --git a/components/shared/graph-route-cache.ts b/components/shared/graph-route-cache.ts
new file mode 100644
index 00000000..6614908f
--- /dev/null
+++ b/components/shared/graph-route-cache.ts
@@ -0,0 +1,86 @@
+import { orthogonalRoute, routeLabel, routePath, segmentBlocked, type RoutePoint, type RouteRect } from "./graph-route";
+
+export type RouteSide = "left" | "right" | "top" | "bottom";
+export type RoutingNode = RouteRect & { id: string };
+export type RoutingEdge = { id: string; source: string; target: string };
+export type ConnectorRequest = RoutingEdge & { a: RoutePoint; b: RoutePoint; sourceSide: RouteSide; targetSide: RouteSide };
+export type ConnectorGeometry = { points: RoutePoint[]; path: string; label: RoutePoint; blocked: boolean };
+export type RoutingScene = {
+ rects: Map<string, RouteRect>; obstacles: RouteRect[]; lanes: Map<string, number>;
+ changes: WeakMap<RoutingScene, RouteRect[]>;
+};
+const PAD = 10;
+const direction = (side: RouteSide): RoutePoint => side === "left" ? { x: -1, y: 0 } : side === "right" ? { x: 1, y: 0 } : side === "top" ? { x: 0, y: -1 } : { x: 0, y: 1 };
+const unpad = (r: RouteRect): RouteRect => ({ x: r.x + PAD, y: r.y + PAD, width: r.width - PAD * 2, height: r.height - PAD * 2 });
+const sameRect = (a: RouteRect, b: RouteRect) => a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
+const intersects = (a: RouteRect, b: RouteRect) => a.x <= b.x + b.width && a.x + a.width >= b.x && a.y <= b.y + b.height && a.y + a.height >= b.y;
+
+/** One measured obstacle set and sibling index per graph geometry/topology revision. */
+export function createRoutingScene(nodes: RoutingNode[], edges: RoutingEdge[]): RoutingScene {
+ const rects = new Map(nodes.map((n) => [n.id, { x: n.x - PAD, y: n.y - PAD, width: n.width + PAD * 2, height: n.height + PAD * 2 }]));
+ const outgoing = new Map<string, string[]>(), incoming = new Map<string, string[]>();
+ for (const e of edges) { outgoing.set(e.source, [...(outgoing.get(e.source) ?? []), e.id]); incoming.set(e.target, [...(incoming.get(e.target) ?? []), e.id]); }
+ const lanes = new Map(edges.map((e) => [e.id, [...new Set([...(outgoing.get(e.source) ?? []), ...(incoming.get(e.target) ?? [])])].sort().indexOf(e.id)]));
+ return { rects, obstacles: [...rects.values()], lanes, changes: new WeakMap() };
+}
+function changedRects(before: RoutingScene, after: RoutingScene): RouteRect[] {
+ const known = after.changes.get(before); if (known) return known;
+ const changed: RouteRect[] = [];
+ for (const [id, r] of before.rects) { const next = after.rects.get(id); if (!next || !sameRect(r, next)) { changed.push(r); if (next) changed.push(next); } }
+ for (const [id, r] of after.rects) if (!before.rects.has(id)) changed.push(r);
+ after.changes.set(before, changed); return changed;
+}
+function escapeDistance(point: RoutePoint, vector: RoutePoint, maximum: number, obstacles: RouteRect[]): number {
+ let result = maximum;
+ for (const r of obstacles) {
+ const across = vector.x ? point.y > r.y && point.y < r.y + r.height : point.x > r.x && point.x < r.x + r.width;
+ if (!across) continue;
+ const gap = vector.x > 0 ? r.x - point.x : vector.x < 0 ? point.x - r.x - r.width : vector.y > 0 ? r.y - point.y : point.y - r.y - r.height;
+ if (gap >= 0) result = Math.min(result, Math.max(0, gap - 0.5));
+ }
+ return result;
+}
+function geometry(points: RoutePoint[], blocked = false): ConnectorGeometry { return { points, path: routePath(points), label: routeLabel(points), blocked }; }
+
+export function computeConnectorRoute(scene: RoutingScene, request: ConnectorRequest): ConnectorGeometry {
+ const { a, b, source, target, sourceSide, targetSide } = request;
+ const sd = direction(sourceSide), td = direction(targetSide), lane = scene.lanes.get(request.id) ?? 0;
+ const dx = b.x - a.x, dy = b.y - a.y;
+ // A clear facing-port segment is valid even when the two endpoint padding zones meet.
+ const facing = sd.x === -td.x && sd.y === -td.y && dx * sd.x + dy * sd.y > 0 && Math.abs(dx * sd.y - dy * sd.x) < 0.01;
+ const directObstacles = [...scene.rects].map(([id, r]) => id === source || id === target ? unpad(r) : r);
+ if (facing && !segmentBlocked(a, b, directObstacles)) return geometry([a, b]);
+ const sourceOthers = [...scene.rects].filter(([id]) => id !== source).map(([, r]) => r);
+ const targetOthers = [...scene.rects].filter(([id]) => id !== target).map(([, r]) => r);
+ const maximum = 26 + (lane % 5) * 4;
+ const se = escapeDistance(a, sd, maximum, sourceOthers), te = escapeDistance(b, td, maximum, targetOthers);
+ const start = { x: a.x + sd.x * se, y: a.y + sd.y * se }, end = { x: b.x + td.x * te, y: b.y + td.y * te };
+ const escapes = !segmentBlocked(a, start, sourceOthers) && !segmentBlocked(end, b, targetOthers);
+ const middle = escapes ? orthogonalRoute(start, end, scene.obstacles, lane % 7) : null;
+ if (middle) return geometry([a, ...middle, b]);
+ // A bounded visible fallback is marked honestly, not reported as obstacle-free.
+ const x = (a.x + b.x) / 2;
+ return geometry([a, { x, y: a.y }, { x, y: b.y }, b], true);
+}
+
+type Entry = { scene: RoutingScene; key: string; geometry: ConnectorGeometry; bounds: RouteRect };
+/** Per-canvas bounded memo; unchanged routes survive unrelated node movement. */
+export class GraphRouteCache {
+ private entries = new Map<string, Entry>();
+ computations = 0;
+ hits = 0;
+ resolve(scene: RoutingScene, request: ConnectorRequest): ConnectorGeometry {
+ const key = JSON.stringify([request.source, request.target, request.a.x, request.a.y, request.b.x, request.b.y, request.sourceSide, request.targetSide, scene.lanes.get(request.id)]);
+ const old = this.entries.get(request.id);
+ if (old?.key === key && (old.scene === scene || (!old.geometry.blocked && !changedRects(old.scene, scene).some((r) => intersects(old.bounds, r))))) {
+ this.hits++; this.entries.set(request.id, { ...old, scene }); return old.geometry;
+ }
+ this.computations++;
+ const result = computeConnectorRoute(scene, request), xs = result.points.map((p) => p.x), ys = result.points.map((p) => p.y);
+ const x = Math.min(...xs), y = Math.min(...ys);
+ this.entries.delete(request.id);
+ this.entries.set(request.id, { scene, key, geometry: result, bounds: { x, y, width: Math.max(...xs) - x, height: Math.max(...ys) - y } });
+ if (this.entries.size > 400) this.entries.delete(this.entries.keys().next().value!);
+ return result;
+ }
+}
diff --git a/components/shared/use-graph-projection.ts b/components/shared/use-graph-projection.ts
index 13ce0a87..303fc12f 100644
--- a/components/shared/use-graph-projection.ts
+++ b/components/shared/use-graph-projection.ts
@@ -1,23 +1,28 @@
"use client";
-import { useCallback, useMemo, useState } from "react";
+import { useCallback, useState } from "react";
import { applyEdgeChanges, applyNodeChanges, type Edge, type EdgeChange, type Node, type NodeChange } from "@xyflow/react";
export type GraphProjection<N extends Node, E extends Edge> = { nodes: N[]; edges: E[] };
type Snapshot<N extends Node, E extends Edge> = GraphProjection<N, E> & { source: GraphProjection<N, E> };
+const selectedKey = (items: { id: string; selected?: boolean }[]) => JSON.stringify(items.filter((item) => item.selected).map((item) => item.id).sort());
-/** One owner for transient selection/measurement/drag state. No effect-to-selection feedback. */
+/** Controlled changes win; data-only updates retain transient box/ctrl selections. */
export function reconcileGraphProjection<N extends Node, E extends Edge>(source: GraphProjection<N, E>, previous: Snapshot<N, E>): Snapshot<N, E> {
if (source === previous.source) return previous;
const oldNodes = new Map(previous.nodes.map((node) => [node.id, node]));
- const addedSelection = source.nodes.some((node) => node.selected && !oldNodes.has(node.id));
+ const nodeSelectionChanged = selectedKey(source.nodes) !== selectedKey(previous.source.nodes);
+ const edgeSelectionChanged = selectedKey(source.edges) !== selectedKey(previous.source.edges);
const oldEdges = new Map(previous.edges.map((edge) => [edge.id, edge]));
return { source,
- nodes: source.nodes.map((node) => ({ ...node, measured: oldNodes.get(node.id)?.measured ?? node.measured, selected: addedSelection ? Boolean(node.selected) : oldNodes.get(node.id)?.selected ?? node.selected })),
- edges: source.edges.map((edge) => ({ ...edge, selected: oldEdges.get(edge.id)?.selected ?? edge.selected })),
+ nodes: source.nodes.map((node) => ({ ...node, measured: oldNodes.get(node.id)?.measured ?? node.measured, selected: nodeSelectionChanged ? Boolean(node.selected) : oldNodes.get(node.id)?.selected ?? node.selected })),
+ edges: source.edges.map((edge) => ({ ...edge, selected: edgeSelectionChanged ? Boolean(edge.selected) : oldEdges.get(edge.id)?.selected ?? edge.selected })),
};
}
export function useGraphProjection<N extends Node, E extends Edge>(source: GraphProjection<N, E>) {
const [snapshot, setSnapshot] = useState<Snapshot<N, E>>(() => ({ ...source, source }));
- const current = useMemo(() => reconcileGraphProjection(source, snapshot), [source, snapshot]);
+ const current = reconcileGraphProjection(source, snapshot);
+ // Record each controlled transition, including select→clear without a node event.
+ // The caller memoizes source; this guarded self-update settles before children render.
+ if (current !== snapshot) setSnapshot(current);
const onNodesChange = useCallback((changes: NodeChange<N>[]) => setSnapshot((previous) => {
const next = reconcileGraphProjection(source, previous); return { ...next, nodes: applyNodeChanges(changes, next.nodes) };
}), [source]);
@@ -25,5 +30,9 @@ export function useGraphProjection<N extends Node, E extends Edge>(source: Graph
const next = reconcileGraphProjection(source, previous); return { ...next, edges: applyEdgeChanges(changes, next.edges) };
}), [source]);
const reset = useCallback(() => setSnapshot({ ...source, source }), [source]);
- return { ...current, onNodesChange, onEdgesChange, reset };
+ const clearSelection = useCallback(() => setSnapshot((previous) => {
+ const next = reconcileGraphProjection(source, previous);
+ return { ...next, nodes: next.nodes.map((node) => ({ ...node, selected: false })), edges: next.edges.map((edge) => ({ ...edge, selected: false })) };
+ }), [source]);
+ return { ...current, onNodesChange, onEdgesChange, reset, clearSelection };
}
diff --git a/scripts/graph-selection-review.test.ts b/scripts/graph-selection-review.test.ts
new file mode 100644
index 00000000..6149421a
--- /dev/null
+++ b/scripts/graph-selection-review.test.ts
@@ -0,0 +1,21 @@
+import { describe, expect, it } from "vitest";
+import { reconcileGraphProjection } from "../components/shared/use-graph-projection";
+import type { Edge, Node } from "@xyflow/react";
+const make = (selected: string[]) => ({ nodes: ["a", "b", "c"].map((id): Node => ({ id, position: { x: 0, y: 0 }, data: {}, selected: selected.includes(id) })), edges: [] as Edge[] });
+describe("review: controlled graph selection", () => {
+ it("selects an existing node when the parent changes selection without a node event", () => {
+ const source = make([]);
+ const next = reconcileGraphProjection(make(["b"]), { ...source, source });
+ expect(next.nodes.filter((n) => n.selected).map((n) => n.id)).toEqual(["b"]);
+ });
+ it("clears stale transient selection when the parent explicitly clears", () => {
+ const source = make(["a"]);
+ const previous = { source, ...make(["a", "b"]) };
+ expect(reconcileGraphProjection(make([]), previous).nodes.some((n) => n.selected)).toBe(false);
+ });
+ it("preserves ctrl/box multi-selection across data-only updates", () => {
+ const source = make(["a"]);
+ const previous = { source, ...make(["a", "b"]) };
+ expect(reconcileGraphProjection(make(["a"]), previous).nodes.filter((n) => n.selected).map((n) => n.id)).toEqual(["a", "b"]);
+ });
+});