From f468eb8f475a961587c6737332c3393ff0672d16 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Tue, 21 Jul 2026 12:08:39 +0800 Subject: [PATCH] feat(sidebar): apply type-only search filters --- .../src/components/sidebar/ConnectionTree.vue | 2 +- .../src/lib/sidebar/sidebarSearchTree.ts | 19 +++- packages/app-tests/sidebarSearchTree.test.ts | 100 ++++++++++++++++++ 3 files changed, 115 insertions(+), 6 deletions(-) diff --git a/apps/desktop/src/components/sidebar/ConnectionTree.vue b/apps/desktop/src/components/sidebar/ConnectionTree.vue index afab9270c..38a9a5abb 100644 --- a/apps/desktop/src/components/sidebar/ConnectionTree.vue +++ b/apps/desktop/src/components/sidebar/ConnectionTree.vue @@ -325,8 +325,8 @@ const filteredNodes = computed(() => { let nodes = store.treeNodes; const q = deferredSearchQuery.value; + nodes = filterSidebarTree(nodes, q, searchCollapsedIds.value, searchableNodeTypes.value); if (q) { - nodes = filterSidebarTree(nodes, q, searchCollapsedIds.value, searchableNodeTypes.value); nodes = filterSidebarSearchRootsByConnectionState(nodes, store.connectedIds); } diff --git a/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts b/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts index 73f8d6bb3..926d6e334 100644 --- a/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts +++ b/apps/desktop/src/lib/sidebar/sidebarSearchTree.ts @@ -23,23 +23,32 @@ function normalizedLabel(node: TreeNode): string { } export function filterSidebarTree(nodes: TreeNode[], query: string, collapsedIds: ReadonlySet, searchableNodeTypes?: ReadonlySet): TreeNode[] { - return filterSidebarTreeWithMatcher(nodes, createSidebarLabelMatcher(query), collapsedIds, searchableNodeTypes); + const matchLabel = query ? createSidebarLabelMatcher(query) : undefined; + if (!matchLabel && searchableNodeTypes === undefined) return nodes; + return filterSidebarTreeWithMatcher(nodes, matchLabel, collapsedIds, searchableNodeTypes); } -function filterSidebarTreeWithMatcher(nodes: TreeNode[], matchLabel: SidebarLabelMatcher, collapsedIds: ReadonlySet, searchableNodeTypes?: ReadonlySet): TreeNode[] { +function filterSidebarTreeWithMatcher(nodes: TreeNode[], matchLabel: SidebarLabelMatcher | undefined, collapsedIds: ReadonlySet, searchableNodeTypes?: ReadonlySet): TreeNode[] { const filteredNodes: { node: TreeNode; score: number }[] = []; for (const node of nodes) { if (node.type === "object-browser" && node.hiddenChildren) { - const matches = node.hiddenChildren.map((child) => ({ node: child, score: matchLabel(normalizedLabel(child))?.score ?? 0 })).filter((match) => match.score > 0); + const matches = node.hiddenChildren.flatMap((child) => { + if (searchableNodeTypes && !searchableNodeTypes.has(child.type)) return []; + const match = matchLabel?.(normalizedLabel(child)); + if (matchLabel && !match) return []; + return [{ node: child, score: match?.score ?? 0 }]; + }); filteredNodes.push(...matches); continue; } const label = normalizedLabel(node); const canSelfMatch = !searchableNodeTypes || searchableNodeTypes.has(node.type); - const selfMatch = canSelfMatch ? bestMatch(matchLabel, label, node.comment) : null; - const preservesSubtree = !!selfMatch && preserveMatchedSubtreeTypes.has(node.type); + const selfMatch = canSelfMatch ? (matchLabel ? bestMatch(matchLabel, label, node.comment) : { score: 0 }) : null; + // Type-only filtering keeps matching rows and their ancestor path, but not + // unrelated descendants that would make the selected type appear ignored. + const preservesSubtree = !!matchLabel && !!selfMatch && preserveMatchedSubtreeTypes.has(node.type); const filteredChildren = preservesSubtree ? node.children : node.children ? filterSidebarTreeWithMatcher(node.children, matchLabel, collapsedIds, searchableNodeTypes) : undefined; if (selfMatch || (filteredChildren && filteredChildren.length > 0)) { diff --git a/packages/app-tests/sidebarSearchTree.test.ts b/packages/app-tests/sidebarSearchTree.test.ts index 7f4843c11..e3e1b25f3 100644 --- a/packages/app-tests/sidebarSearchTree.test.ts +++ b/packages/app-tests/sidebarSearchTree.test.ts @@ -274,6 +274,106 @@ test("search scope excludes non-selected node self matches", () => { assert.equal(filtered.length, 0); }); +function scopedSearchNodes(): TreeNode[] { + return [ + { + id: "conn:1", + label: "warehouse", + type: "connection", + connectionId: "conn:1", + isExpanded: true, + children: [ + { + id: "conn:1:db", + label: "inventory", + type: "database", + connectionId: "conn:1", + database: "inventory", + isExpanded: true, + children: [ + { + id: "conn:1:db:sales-order", + label: "sales_order", + type: "schema", + connectionId: "conn:1", + database: "inventory", + schema: "sales_order", + isExpanded: true, + children: [ + { + id: "conn:1:db:sales-order:orders", + label: "orders", + type: "table", + connectionId: "conn:1", + database: "inventory", + schema: "sales_order", + }, + ], + }, + { + id: "conn:1:db:audit", + label: "audit", + type: "schema", + connectionId: "conn:1", + database: "inventory", + schema: "audit", + isExpanded: true, + children: [ + { + id: "conn:1:db:audit:order-log", + label: "order_log", + type: "table", + connectionId: "conn:1", + database: "inventory", + schema: "audit", + }, + ], + }, + ], + }, + ], + }, + ]; +} + +test("filters the sidebar by node type without a text query", () => { + const filtered = filterSidebarTree(scopedSearchNodes(), "", new Set(), new Set(["schema"])); + + const schemas = filtered[0]?.children?.[0]?.children; + assert.deepEqual( + schemas?.map((node) => node.label), + ["sales_order", "audit"], + ); + assert.deepEqual( + schemas?.map((node) => node.children), + [[], []], + ); +}); + +test("combines text search with the selected node types", () => { + const filtered = filterSidebarTree(scopedSearchNodes(), "order", new Set(), new Set(["schema"])); + + assert.deepEqual( + filtered[0]?.children?.[0]?.children?.map((node) => node.label), + ["sales_order"], + ); +}); + +test("clearing the type filter restores default text search", () => { + const filtered = filterSidebarTree(scopedSearchNodes(), "order", new Set()); + + assert.deepEqual( + filtered[0]?.children?.[0]?.children?.map((node) => node.label), + ["sales_order", "audit"], + ); +}); + +test("clearing all search criteria preserves the original tree", () => { + const nodes = scopedSearchNodes(); + + assert.equal(filterSidebarTree(nodes, "", new Set()), nodes); +}); + test("connection search results stay visible before connecting", () => { const nodes: TreeNode[] = [ {