fix(sidebar): separate root connection filtering

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-07-23 17:29:52 +08:00
1 parent ad2d52e3ca
commit b9c1feab82
11 files changed
+158 -11

No files matched your search

@@ -0,0 +1,40 @@
import { describe, expect, it } from "vitest";
import { filterSidebarTreeToConnectedConnections } from "@/lib/sidebar/sidebarSearchTree";
import type { TreeNode } from "@/types/database";
function connection(id: string): TreeNode {
return {
id,
label: id,
type: "connection",
connectionId: id,
isExpanded: true,
children: [{ id: `${id}:database`, label: "database", type: "database", connectionId: id }],
};
}
function group(id: string, children: TreeNode[]): TreeNode {
return { id, label: id, type: "connection-group", isExpanded: true, children };
}
describe("connected sidebar connection filter", () => {
it("keeps connected connections and their nested groups while removing disconnected connections and empty groups", () => {
const connected = connection("connected");
const tree = [connection("disconnected"), group("team", [group("production", [connected]), group("inactive", [connection("other")])])];
const result = filterSidebarTreeToConnectedConnections(tree, new Set(["connected"]));
expect(result).toHaveLength(1);
expect(result[0]?.id).toBe("team");
expect(result[0]?.children?.[0]?.id).toBe("production");
expect(result[0]?.children?.[0]?.children?.[0]).toBe(connected);
expect(result[0]?.children?.some((node) => node.id === "inactive")).toBe(false);
});
it("preserves the existing tree when every displayed connection is active", () => {
const first = connection("first");
const tree = [group("team", [first])];
expect(filterSidebarTreeToConnectedConnections(tree, new Set(["first"]))).toBe(tree);
});
});
@@ -0,0 +1,31 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
import { resolveSidebarFilterGuards } from "@/lib/sidebar/sidebarSearchTree";
describe("sidebar filter guards", () => {
it.each([
{ connectedOnly: false, query: "", scoped: false, treeSearch: false, rootPartial: false },
{ connectedOnly: true, query: "", scoped: false, treeSearch: false, rootPartial: true },
{ connectedOnly: false, query: "table", scoped: false, treeSearch: true, rootPartial: true },
{ connectedOnly: true, query: "table", scoped: false, treeSearch: true, rootPartial: true },
{ connectedOnly: false, query: " ", scoped: true, treeSearch: true, rootPartial: true },
{ connectedOnly: true, query: " ", scoped: true, treeSearch: true, rootPartial: true },
{ connectedOnly: false, query: "table", scoped: true, treeSearch: true, rootPartial: true },
{ connectedOnly: true, query: "table", scoped: true, treeSearch: true, rootPartial: true },
])("separates connected-only=$connectedOnly query=$query scoped=$scoped", ({ connectedOnly, query, scoped, treeSearch, rootPartial }) => {
expect(resolveSidebarFilterGuards(connectedOnly, query, scoped)).toEqual({
isTreeSearchFiltering: treeSearch,
isRootListPartial: rootPartial,
});
});
it("keeps descendant-local features separate from partial-root operations", () => {
const source = readFileSync(new URL("../../../components/sidebar/ConnectionTree.vue", import.meta.url), "utf8");
expect(source).toContain("sidebarTableSearchEnabled && !isTreeSearchFiltering.value");
expect(source).toContain("!useVirtualTree.value || isTreeSearchFiltering.value");
expect(source.match(/if \(isRootListPartial\.value\)/g)).toHaveLength(2);
expect(source.match(/:drag-disabled="isRootListPartial \|\| isConnectionListAlphabeticallySorted"/g)).toHaveLength(2);
expect(source).not.toContain("isFiltering");
});
});
@@ -79,3 +79,51 @@ export function filterSidebarSearchRootsByConnectionState(nodes: TreeNode[], con
return node.connectionId ? connectedIds.has(node.connectionId) : true;
});
}
export function resolveSidebarFilterGuards(showConnectedConnectionsOnly: boolean, searchQuery: string, hasSearchScopeFilter: boolean) {
const isTreeSearchFiltering = !!searchQuery.trim() || hasSearchScopeFilter;
return {
isTreeSearchFiltering,
isRootListPartial: showConnectedConnectionsOnly || isTreeSearchFiltering,
};
}
/**
* Produces a display-only connection tree containing connected connections and
* the groups that contain them. Connection descendants stay intact because
* this filter controls the connection list, not database-object visibility.
*/
export function filterSidebarTreeToConnectedConnections(nodes: readonly TreeNode[], connectedIds: ReadonlySet<string>): TreeNode[] {
let changed = false;
const filtered: TreeNode[] = [];
for (const node of nodes) {
if (node.type === "connection") {
if (node.connectionId && connectedIds.has(node.connectionId)) {
filtered.push(node);
} else {
changed = true;
}
continue;
}
if (node.type !== "connection-group") {
filtered.push(node);
continue;
}
const children = filterSidebarTreeToConnectedConnections(node.children ?? [], connectedIds);
if (children.length === 0) {
changed = true;
continue;
}
if (children !== node.children) {
changed = true;
filtered.push({ ...node, children });
} else {
filtered.push(node);
}
}
return changed ? filtered : (nodes as TreeNode[]);
}