Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions ui/src/hooks/useFlowNavigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,22 @@ describe("useFlowNavigation", () => {
});

it("onNodeClick calls setFlow, setViewMode('flow'), and fitView", async () => {
const frames: FrameRequestCallback[] = [];
const raf = vi
.spyOn(globalThis, "requestAnimationFrame")
.mockImplementation((cb: FrameRequestCallback) => frames.push(cb));
const { result } = setup();
await act(async () => {
await result.current.onNodeClick({} as any, { id: "a", type: "FUNCTION" } as any);
});
expect(mockSetFlow).toHaveBeenCalled();
expect(mockSetViewMode).toHaveBeenCalledWith("flow");
// Not in the same tick: React Flow has not rendered the new nodes yet, and a
// synchronous fitView measured the old view, zooming to the 2x cap.
expect(mockFitView).not.toHaveBeenCalled();
frames.forEach((cb) => cb(0));
expect(mockFitView).toHaveBeenCalledWith({ padding: 0.15, duration: 250 });
raf.mockRestore();
});

it("cache is keyed by graphVersion — same node, same version uses cache", async () => {
Expand Down
4 changes: 3 additions & 1 deletion ui/src/hooks/useFlowNavigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ export function useFlowNavigation(allowedEdgeTypes: string[] = DEFAULT_ALLOWED)

setFlow(layoutedNodes, flowEdges);
setViewMode("flow");
fitView({ padding: 0.15, duration: 250 });
// After the frame that renders the new nodes: called synchronously, fitView
// measures the previous view and zooms to the 2x cap on the clicked node.
requestAnimationFrame(() => void fitView({ padding: 0.15, duration: 250 }));
},
[rawNodes, rawEdges, graphVersion, allowedEdgeTypes, setFlow, setViewMode, fitView]
);
Expand Down
3 changes: 2 additions & 1 deletion ui/src/hooks/useLayoutComputation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ export function useLayoutComputation(): void {
if (generationRef.current !== gen) return; // stale async, discard

setLayout(layoutedNodes, layoutedEdges);
fitView({ padding: 0.15, duration: 250 });
// After the frame that renders the new layout, not against the old one.
requestAnimationFrame(() => void fitView({ padding: 0.15, duration: 250 }));
})();
}, [
rawNodes,
Expand Down
Loading