Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(graph): declare every graph hook above the loading early-return (#2111)

Raghav Chamadiya64461a464f7c
diff --git a/packages/ui/__tests__/graph/graph-flow.test.tsx b/packages/ui/__tests__/graph/graph-flow.test.tsx
index 5a9e05446..7bb53e86c 100644
--- a/packages/ui/__tests__/graph/graph-flow.test.tsx
+++ b/packages/ui/__tests__/graph/graph-flow.test.tsx
@@ -592,3 +592,44 @@ describe("GraphFlow drill-down honesty", () => {
expect(screen.queryByText(/dead files/)).toBeNull();
});
});
+
+describe("GraphFlow hook order", () => {
+ it("keeps the same hooks on the loading pass and the loaded one", () => {
+ // The loading branch returns a skeleton before the render body, so a hook
+ // declared below it does not run on that pass. React then sees the hook
+ // count change on the next render and takes the whole canvas down with
+ // "change in the order of Hooks called by GraphFlow" — recoverable only by
+ // a page refresh.
+ const errors: unknown[][] = [];
+ const spy = vi
+ .spyOn(console, "error")
+ .mockImplementation((...args: unknown[]) => {
+ errors.push(args);
+ });
+ try {
+ const { rerender } = render(
+ <GraphFlow {...baseProps} viewMode="full" isLoadingFullGraph={true} />,
+ );
+ rerender(
+ <GraphFlow
+ {...baseProps}
+ viewMode="full"
+ isLoadingFullGraph={false}
+ fullGraph={
+ {
+ nodes: [fileNode("src/a.ts", "typescript")],
+ links: [],
+ } as never
+ }
+ />,
+ );
+ const hookComplaint = errors.find((e) =>
+ String(e[0] ?? "").includes("order of Hooks"),
+ );
+ expect(hookComplaint).toBeUndefined();
+ } finally {
+ spy.mockRestore();
+ }
+ });
+});
+
diff --git a/packages/ui/src/graph/graph-flow.tsx b/packages/ui/src/graph/graph-flow.tsx
index 80ccdd0b1..91d529036 100644
--- a/packages/ui/src/graph/graph-flow.tsx
+++ b/packages/ui/src/graph/graph-flow.tsx
@@ -1132,6 +1132,34 @@ export function GraphFlow(props: GraphFlowProps) {
setCtxMenu(null);
}, [ctxMenu, setCtxMenu]);
+ // Both of these are read far below, next to the key they feed. They are
+ // declared here because the skeleton branch that follows returns early, and a
+ // hook after it does not run on the loading pass — which changes the hook
+ // count between renders and takes the whole canvas down.
+
+ // What the key reports. `displayGraph.size` counts every edge in the graph
+ // including the kinds the edge filter is hiding, which is how a slice showing
+ // only its exits could still claim 553 edges. Reconciles the edge filter
+ // only: the ego filter hides nodes at the canvas rather than in the graph,
+ // and reports its own count in the status row beside this.
+ const visibleEdgeCount = useMemo(() => {
+ if (!displayGraph) return 0;
+ let n = 0;
+ displayGraph.forEachEdge((_, attrs) => {
+ if (visibleEdgeTypes.has(attrs.edgeKind)) n++;
+ });
+ return n;
+ }, [displayGraph, visibleEdgeTypes]);
+
+ // Members vs the one-hop stubs around them, from the same payload the banner
+ // counts, so the two figures on screen cannot disagree.
+ const sliceCounts = useMemo(() => {
+ if (!communitySlice) return undefined;
+ const members = communitySlice.nodes.filter((n) => !n.is_boundary).length;
+ return { members, boundary: communitySlice.nodes.length - members };
+ }, [communitySlice]);
+
+ // No hooks below this line.
if (
(isLoading || isAwaitingAsyncBuild || isBuildingGraph) &&
!displayGraph
@@ -1257,28 +1285,6 @@ export function GraphFlow(props: GraphFlowProps) {
return `${parts.join(", ")}.`;
})();
- // What the key reports. `displayGraph.size` counts every edge in the graph
- // including the kinds the edge filter is hiding, which is how a slice showing
- // only its exits could still claim 553 edges. Reconciles the edge filter
- // only: the ego filter hides nodes at the canvas rather than in the graph,
- // and reports its own count in the status row beside this.
- const visibleEdgeCount = useMemo(() => {
- if (!displayGraph) return 0;
- let n = 0;
- displayGraph.forEachEdge((_, attrs) => {
- if (visibleEdgeTypes.has(attrs.edgeKind)) n++;
- });
- return n;
- }, [displayGraph, visibleEdgeTypes]);
-
- // Members vs the one-hop stubs around them, from the same payload the banner
- // counts, so the two figures on screen cannot disagree.
- const sliceCounts = useMemo(() => {
- if (!communitySlice) return undefined;
- const members = communitySlice.nodes.filter((n) => !n.is_boundary).length;
- return { members, boundary: communitySlice.nodes.length - members };
- }, [communitySlice]);
-
const activeCommunityLabel =
activeCommunity !== null
? (communityLabels?.get(activeCommunity) ?? `Community ${activeCommunity}`)