From b90ab18b720c33e9915baf79671f820177db6e96 Mon Sep 17 00:00:00 2001 From: Adeel Ahmad Date: Sat, 10 Oct 2026 22:39:16 +0000 Subject: [PATCH] fix(ui): open the graph on the namespace that has one With no namespace picked, the graph opened on all shared namespaces even when only a namespace that keeps its graph to itself had anything, so it showed empty until that namespace was picked in the switcher. It now opens there, waits for the namespaces before building a graph, and on a computer the Find in graph box has the focus. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_018GNzYTT6fQRMPesPRNan3u --- docs/graph.md | 4 +- .../__tests__/graph-opening.test.ts | 41 +++++++++++++++++++ nextjs-frontend/components/graph/filters.tsx | 16 +++++++- .../components/graph/graph-page.tsx | 22 ++++++---- nextjs-frontend/components/graph/model.ts | 11 +++++ 5 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 nextjs-frontend/__tests__/graph-opening.test.ts diff --git a/docs/graph.md b/docs/graph.md index bcb57f8ca..40cd8a0db 100644 --- a/docs/graph.md +++ b/docs/graph.md @@ -10,7 +10,9 @@ vocabulary (SKOS), apart from entities, shown on the explorer canvas as their ow ## The explorer -Graph in the sidebar shows the graph of the scope picked at the top (one namespace, or all shared). +Graph in the sidebar shows the graph of the scope picked at the top (one namespace, or all shared). With no namespace +picked it opens on all shared namespaces, or, when none of them has a graph yet, on the namespace that keeps its graph +to itself and has one. On a computer the Find in graph box has the focus, so typing a name finds it. - **Move around**: drag the background to pan, the wheel or the + and − buttons to zoom; on touch, drag with one finger and pinch with two. Drag a node to put it where you want; it stays there on this device until a layout or diff --git a/nextjs-frontend/__tests__/graph-opening.test.ts b/nextjs-frontend/__tests__/graph-opening.test.ts new file mode 100644 index 000000000..e68d870c4 --- /dev/null +++ b/nextjs-frontend/__tests__/graph-opening.test.ts @@ -0,0 +1,41 @@ +import { openingNamespace } from "@/components/graph/model"; + +describe("where the graph opens when no namespace is picked", () => { + it("stays all shared namespaces when one of them has a graph", () => { + expect( + openingNamespace([ + { name: "interviews", graph: "isolated", analyzed: 4, speakers: 3 }, + { name: "podcasts", graph: "shared", analyzed: 1, speakers: 0 }, + ]), + ).toBeNull(); + }); + + it("opens the namespace that keeps its graph to itself when only it has one", () => { + expect( + openingNamespace([ + { name: "interviews", graph: "isolated", analyzed: 2, speakers: 3 }, + { name: "podcasts", graph: "shared", analyzed: 0, speakers: 0 }, + ]), + ).toBe("interviews"); + }); + + it("picks the fullest when several keep theirs to themselves", () => { + expect( + openingNamespace([ + { name: "family", graph: "isolated", analyzed: 1, speakers: 1 }, + { name: "work", graph: "isolated", analyzed: 5, speakers: 2 }, + { name: "podcasts", graph: "shared" }, + ]), + ).toBe("work"); + }); + + it("changes nothing when there is nothing to see yet", () => { + expect( + openingNamespace([ + { name: "interviews", graph: "isolated", analyzed: 0, speakers: 0 }, + { name: "podcasts", graph: "shared", analyzed: 0, speakers: 0 }, + ]), + ).toBeNull(); + expect(openingNamespace([])).toBeNull(); + }); +}); diff --git a/nextjs-frontend/components/graph/filters.tsx b/nextjs-frontend/components/graph/filters.tsx index e361342bb..cd210c14b 100644 --- a/nextjs-frontend/components/graph/filters.tsx +++ b/nextjs-frontend/components/graph/filters.tsx @@ -10,7 +10,15 @@ import { count } from "@/lib/format"; import { cn } from "@/lib/utils"; /** "Find in graph": type to list matching nodes; Enter picks the best one. */ -export function GraphFinder({ nodes, onPick }: { nodes: GraphNode[]; onPick: (id: string) => void }) { +export function GraphFinder({ + nodes, + onPick, + autoFocus, +}: { + nodes: GraphNode[]; + onPick: (id: string) => void; + autoFocus?: boolean; +}) { const [q, setQ] = useState(""); const [active, setActive] = useState(0); const hits = findNodes(nodes, q).slice(0, 8); @@ -35,6 +43,7 @@ export function GraphFinder({ nodes, onPick }: { nodes: GraphNode[]; onPick: (id setQ(""); } else if (e.key === "Escape") setQ(""); }} + autoFocus={autoFocus} placeholder="Find in graph" aria-label="Find in graph" role="combobox" @@ -94,6 +103,7 @@ export function GraphFilters({ maxNodes, onPick, isolated, + focusFind, }: { nodes: GraphNode[]; scope: "ns" | "all"; @@ -112,6 +122,8 @@ export function GraphFilters({ maxNodes: number | null; onPick: (id: string) => void; isolated: string[]; + /** The find box takes the focus when it shows. */ + focusFind?: boolean; }) { const present = new Set(nodes.map(nodeGroup)); const toggle = (set: Set, v: string) => { @@ -122,7 +134,7 @@ export function GraphFilters({ }; return (
- +
Scope n.name); const focusParam = params.get("focus"); const urlNs = params.get("ns"); - const scopeMode: "ns" | "all" = params.get("scope") === "all" ? "all" : urlNs ? "ns" : topNs ? "ns" : "all"; + const allShared = params.get("scope") === "all"; + // with no namespace picked, open where there is something to see + const opening = urlNs || topNs || allShared ? null : openingNamespace(namespaces); + const scopeMode: "ns" | "all" = allShared ? "all" : urlNs || topNs || opening ? "ns" : "all"; const ns = urlNs && nsNames.includes(urlNs) ? urlNs - : (topNs ?? namespaces.find((n) => n.graph !== "isolated")?.name ?? nsNames[0] ?? null); + : (topNs ?? opening ?? namespaces.find((n) => n.graph !== "isolated")?.name ?? nsNames[0] ?? null); const scope = scopeMode === "all" || !ns ? "global" : `ns:${ns}`; const view = params.get("view") === "table" ? "table" : "graph"; const asOf = params.get("as_of"); @@ -98,6 +102,8 @@ export function GraphPage() { queryKey: ["graph", scope, asOf], queryFn: async () => (await data(Search.getGraph({ client, query: { scope, as_of: asOf } }))) as unknown as GraphData, + // the namespaces decide the scope, so wait for them rather than build a graph nobody looks at + enabled: loaded, staleTime: 60_000, }); const settings = useQuery({ @@ -201,7 +207,8 @@ export function GraphPage() { (j.steps ?? []).some((s) => (typeof s === "string" ? s : (s as { type?: string })?.type) === "analyze")), ).length; - const filters = ( + // the side column's find box takes the focus, so typing finds; the filters sheet on smaller screens doesn't + const filters = (focusFind: boolean) => ( select(id)} isolated={namespaces.filter((n) => n.graph === "isolated").map((n) => n.name)} + focusFind={focusFind} /> ); const viewSwitch = ( @@ -245,7 +253,7 @@ export function GraphPage() { aria-label="Graph filters" className="hidden min-h-0 flex-col gap-4 overflow-y-auto border-r border-border bg-surface p-4 lg:flex" > - {filters} + {filters(true)} Proposed changes to review @@ -276,7 +284,7 @@ export function GraphPage() { show, or be hidden. )} - {graph.isLoading && ( + {(!loaded || graph.isLoading) && (
@@ -397,7 +405,7 @@ export function GraphPage() {
- {filters} + {filters(false)}
); diff --git a/nextjs-frontend/components/graph/model.ts b/nextjs-frontend/components/graph/model.ts index 4cd1e15dd..0e0c4c5c4 100644 --- a/nextjs-frontend/components/graph/model.ts +++ b/nextjs-frontend/components/graph/model.ts @@ -228,6 +228,17 @@ export function summarize(nodes: GraphNode[], edges: GraphEdge[], scopeLabel: st return `${head} Most connected: ${top.map((n) => `${n.label} (${degree.get(n.id)} ${degree.get(n.id) === 1 ? "link" : "links"})`).join(", ")}.`; } +/** Where the graph opens when no namespace is picked: all shared namespaces (null), unless none of them has anything in + * its graph yet and a namespace that keeps its graph to itself does; then that one, the fullest if there are several. */ +export function openingNamespace( + namespaces: { name: string; graph?: string | null; analyzed?: number; speakers?: number }[], +): string | null { + const size = (n: { analyzed?: number; speakers?: number }) => (n.analyzed ?? 0) + (n.speakers ?? 0); + if (namespaces.some((n) => n.graph !== "isolated" && size(n) > 0)) return null; + const fullest = namespaces.filter((n) => size(n) > 0).sort((a, b) => size(b) - size(a))[0]; + return fullest?.name ?? null; +} + /** Find a node from a `?focus=` value: "s4" (speaker), "e12" (an entity id, also inside merged global nodes). */ export function findFocus(nodes: GraphNode[], focus: string | null | undefined): GraphNode | undefined { if (!focus) return undefined;