1
0
Fork 0

fix(npc-dialogue): re-route edges live while dragging cards

Edges in the dialogue flow editor bent around where cards used to be until
the page was saved and refreshed, because RoutedFlowEdge memoized its
obstacle rectangles on the React Flow store's nodeLookup Map. That Map is
mutated in place during a drag, so the memo key never changed and the cached
rects only refreshed on remount.

Compute the card rectangles in the editor from its own live `nodes` state and
hand them to each edge through `data`, which is how the tech tree already
drives its routed edges. Obstacles stay render-only: serialize() persists
deriveEdges(clean) separately, so nothing new reaches stored flow data.

Verified by dragging a non-endpoint card over an unrelated edge and sampling
that edge's path mid-drag: 4 of 5 held positions re-routed with the fix, 0 of
5 without it (same test, same document).
This commit is contained in:
Jason Fraley 2026-10-14 15:00:08 -04:00
parent 35a88ccd8f
commit 95a2412206
2 changed files with 39 additions and 32 deletions

View file

@ -34,6 +34,7 @@ import type {
NpcDialogueNodeType,
} from "@/lib/npc-dialogue/dialogue";
import { deriveEdges, removeHopFromNode, validateGraph } from "@/lib/npc-dialogue/dialogue";
import type { RouteRect } from "@/lib/flow-edge-routing";
import { NpcDialogueNode } from "./NpcDialogueNode";
import { NodeEditDialog } from "./NodeEditDialog";
import { RoutedFlowEdge } from "./RoutedFlowEdge";
@ -131,19 +132,30 @@ function FlowEditor() {
// Edges are a derived projection of the node data, never edited directly.
// Every edge renders through the routed edge type so lines bend around
// unrelated cards instead of slicing through them.
const syncedEdges = useMemo<Edge[]>(
() =>
deriveEdges(nodes as DialogueNode[])
.map((edge) => ({
...edge,
type: "routed",
animated: true,
markerEnd: EDGE_MARKER,
selected: edge.id === selectedEdgeId,
})),
[nodes, selectedEdgeId],
);
// unrelated cards instead of slicing through them. The card rectangles used
// for obstacle avoidance are computed here from the live `nodes` state and
// handed to each edge via `data`: React Flow mutates its store's nodeLookup
// Map in place, so reading it inside the edge yields a stale snapshot that
// only refreshes on remount (edges kept bending around where cards used to
// be until save+refresh). Deriving from `nodes` re-routes edges as you drag.
const syncedEdges = useMemo<Edge[]>(() => {
const obstacles: RouteRect[] = [];
for (const n of nodes) {
const width = n.measured?.width ?? 0;
const height = n.measured?.height ?? 0;
if (!width || !height) continue;
obstacles.push({ id: n.id, x: n.position.x, y: n.position.y, width, height });
}
return deriveEdges(nodes as DialogueNode[])
.map((edge) => ({
...edge,
type: "routed",
animated: true,
markerEnd: EDGE_MARKER,
selected: edge.id === selectedEdgeId,
data: { obstacles },
}));
}, [nodes, selectedEdgeId]);
const serialize = useCallback((nds: Node[]) => {
// Volatile interaction flags (selection, in-progress drags) are not part

View file

@ -7,13 +7,17 @@
* line), and plain Bezier connectors sliced straight through unrelated
* cards, which made return paths impossible to trace. All shape logic lives
* in the shared `routeEdge` (`@/lib/flow-edge-routing`, also used by the
* tech tree); this component gathers the card rectangles from the React Flow
* store and renders the result with rounded corners, an arrowhead, and the
* condition label as a chip at the path midpoint so text stays readable
* over other geometry.
* tech tree); the editor passes the current card rectangles in through edge
* `data` (same pattern as the tech tree's RoutedEdge). This component renders
* the result with rounded corners, an arrowhead, and the condition label as a
* chip at the path midpoint so text stays readable over other geometry.
*
* Obstacles arrive via props rather than being read from the React Flow
* store: the store's `nodeLookup` Map is mutated in place, so a memo keyed on
* it never recomputes and edges would keep bending around where cards *used
* to be* until the editor remounted.
*/
import { useMemo } from "react";
import { BaseEdge, EdgeLabelRenderer, useStore, type EdgeProps } from "@xyflow/react";
import { BaseEdge, EdgeLabelRenderer, type EdgeProps } from "@xyflow/react";
import {
buildRoutePath,
@ -37,20 +41,11 @@ export function RoutedFlowEdge({
markerEnd,
label,
selected,
data,
}: EdgeProps) {
const nodeLookup = useStore((s) => s.nodeLookup);
const nodes = useMemo(() => {
const rects: RouteRect[] = [];
nodeLookup.forEach((node) => {
const width = node.measured?.width ?? 0;
const height = node.measured?.height ?? 0;
if (!width || !height) return;
const { x, y } = node.internals.positionAbsolute;
rects.push({ id: node.id, x, y, width, height });
});
return rects;
}, [nodeLookup]);
// Card rectangles computed by the editor from its live node state, so a
// drag re-renders this edge with fresh obstacles instead of a stale cache.
const nodes = (data?.obstacles as RouteRect[] | undefined) ?? [];
const start: RoutePoint = { x: sourceX, y: sourceY };
const end: RoutePoint = { x: targetX, y: targetY };