feat(sidebar): apply type-only search filters
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,23 +23,32 @@ function normalizedLabel(node: TreeNode): string {
|
||||
}
|
||||
|
||||
export function filterSidebarTree(nodes: TreeNode[], query: string, collapsedIds: ReadonlySet<string>, searchableNodeTypes?: ReadonlySet<TreeNodeType>): 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<string>, searchableNodeTypes?: ReadonlySet<TreeNodeType>): TreeNode[] {
|
||||
function filterSidebarTreeWithMatcher(nodes: TreeNode[], matchLabel: SidebarLabelMatcher | undefined, collapsedIds: ReadonlySet<string>, searchableNodeTypes?: ReadonlySet<TreeNodeType>): 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)) {
|
||||
|
||||
+100
@@ -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[] = [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user