feat(editor): show connection group tree in toolbar

This commit is contained in:
Allen authored and GitHub committed 2026-08-19 15:31:24 +08:00
1 parent 45819df9e3
commit c10b4e92ce
6 files changed
+575 -31

No files matched your search

@@ -0,0 +1,95 @@
import { describe, expect, it } from "vitest";
import { buildConnectionPickerRows, connectionPickerSelectableRows, type ConnectionPickerRow } from "@/lib/connection/connectionPickerTree";
import type { SidebarLayout } from "@/types/database";
const connections = [
{ id: "c1", name: "生产 MySQL" },
{ id: "c2", name: "测试 MySQL" },
{ id: "c3", name: "本地 PG" },
{ id: "c4", name: "孤儿连接" },
];
function layout(): SidebarLayout {
return {
groups: [
{ id: "g1", name: "生产", collapsed: false },
{ id: "g2", name: "华东", collapsed: false },
],
order: [
{
type: "group",
id: "g1",
children: [
{ type: "group", id: "g2", children: [{ type: "connection", id: "c1" }] },
{ type: "connection", id: "c2" },
],
},
{ type: "connection", id: "c3" },
],
};
}
function kinds(rows: ConnectionPickerRow[]): string[] {
return rows.map((row) => `${row.kind}:${row.id}@${row.depth}`);
}
describe("buildConnectionPickerRows", () => {
it("flattens the layout tree with depths in sidebar order", () => {
const rows = buildConnectionPickerRows(layout(), connections, new Set(), "");
expect(kinds(rows)).toEqual(["group:g1@0", "group:g2@1", "connection:c1@2", "connection:c2@1", "connection:c3@0", "connection:c4@0"]);
});
it("appends connections missing from the layout at the top level", () => {
const rows = buildConnectionPickerRows({ groups: [], order: [] }, connections, new Set(), "");
expect(kinds(rows)).toEqual(["connection:c1@0", "connection:c2@0", "connection:c3@0", "connection:c4@0"]);
});
it("hides children of collapsed groups without dropping them from the fallback", () => {
const rows = buildConnectionPickerRows(layout(), connections, new Set(["g1"]), "");
expect(kinds(rows)).toEqual(["group:g1@0", "connection:c3@0", "connection:c4@0"]);
expect(rows[0]?.collapsed).toBe(true);
});
it("keeps deeper groups visible when only a nested group is collapsed", () => {
const rows = buildConnectionPickerRows(layout(), connections, new Set(["g2"]), "");
expect(kinds(rows)).toEqual(["group:g1@0", "group:g2@1", "connection:c2@1", "connection:c3@0", "connection:c4@0"]);
});
it("skips stale layout connection ids that no longer exist", () => {
const stale: SidebarLayout = { groups: [], order: [{ type: "connection", id: "gone" }] };
const rows = buildConnectionPickerRows(stale, connections, new Set(), "");
expect(kinds(rows)).toEqual(["connection:c1@0", "connection:c2@0", "connection:c3@0", "connection:c4@0"]);
});
it("filters connections by name and keeps ancestor groups expanded", () => {
const rows = buildConnectionPickerRows(layout(), connections, new Set(["g1", "g2"]), "本地");
expect(kinds(rows)).toEqual(["connection:c3@0"]);
});
it("matches connections by group path and keeps the hierarchy", () => {
const rows = buildConnectionPickerRows(layout(), connections, new Set(), "华东");
expect(kinds(rows)).toEqual(["group:g1@0", "group:g2@1", "connection:c1@2"]);
expect(rows.every((row) => !row.collapsed)).toBe(true);
});
it("matches fallback connections by name during search", () => {
const rows = buildConnectionPickerRows(layout(), connections, new Set(), "孤儿");
expect(kinds(rows)).toEqual(["connection:c4@0"]);
});
it("returns no rows when nothing matches the query", () => {
expect(buildConnectionPickerRows(layout(), connections, new Set(), "不存在")).toEqual([]);
});
it("falls back to the connection id when the name is empty", () => {
const rows = buildConnectionPickerRows({ groups: [], order: [] }, [{ id: "c9", name: "" }], new Set(), "");
expect(rows[0]?.label).toBe("c9");
});
});
describe("connectionPickerSelectableRows", () => {
it("keeps only connection rows for keyboard navigation", () => {
const rows = buildConnectionPickerRows(layout(), connections, new Set(), "");
expect(connectionPickerSelectableRows(rows).map((row) => row.id)).toEqual(["c1", "c2", "c3", "c4"]);
});
});
@@ -20,10 +20,9 @@ describe("SearchableSelect layout", () => {
expect(fixedConnectionIcons).toHaveLength(2);
});
it("wraps deep connection group paths without hiding connection names", () => {
expect(editorToolbarSource).toContain('item-class="min-h-9 h-auto py-1"');
expect(editorToolbarSource).toMatch(/<span class="[^"]*max-w-48[^"]*shrink-0[^"]*whitespace-normal[^"]*break-words[^"]*">\s*\{\{ connectionGroupLabel\(option\) \}\}\s*<\/span>/);
expect(editorToolbarSource).toContain('<TruncatedTextTooltip :text="label" class="block min-w-[7rem] flex-1 text-sm font-medium"');
expect(editorToolbarSource).not.toContain('<TruncatedTextTooltip :text="connectionGroupLabel(option)"');
it("renders the toolbar connection picker as a sidebar-like tree", () => {
expect(editorToolbarSource).toContain("<ConnectionTreeSelect");
expect(editorToolbarSource).toContain(':connections="connectionStore.connections"');
expect(editorToolbarSource).toContain(':layout="connectionStore.sidebarLayout"');
});
});
@@ -0,0 +1,118 @@
import type { SidebarLayout, SidebarOrderEntry } from "@/types/database";
export interface ConnectionPickerConnection {
id: string;
name: string;
}
export interface ConnectionPickerRow {
/** Stable key for list rendering. */
key: string;
kind: "group" | "connection";
id: string;
label: string;
/** Nesting depth used for indentation; top-level entries are 0. */
depth: number;
/** Only meaningful for group rows: whether the group is currently collapsed. */
collapsed: boolean;
}
function groupEntryChildren(entry: Extract<SidebarOrderEntry, { type: "group" }>): SidebarOrderEntry[] {
return entry.children ?? entry.connectionIds?.map((id) => ({ type: "connection" as const, id })) ?? [];
}
function collectLayoutConnectionIds(entries: SidebarOrderEntry[], into: Set<string>) {
for (const entry of entries) {
if (entry.type === "connection") {
into.add(entry.id);
} else {
collectLayoutConnectionIds(groupEntryChildren(entry), into);
}
}
}
function connectionMatchesQuery(name: string, groupPath: string[], normalizedQuery: string): boolean {
if (name.toLowerCase().includes(normalizedQuery)) return true;
return groupPath.join(" / ").toLowerCase().includes(normalizedQuery);
}
/**
* Flattens the sidebar layout into renderable rows for the toolbar connection
* picker. Groups are shown as non-selectable rows that mirror the sidebar tree;
* connections missing from the layout are appended at the top level so they
* stay reachable. With a search query the tree is fully expanded and only
* connections matching by name or group path (plus their ancestor groups) are
* kept.
*/
export function buildConnectionPickerRows(layout: SidebarLayout, connections: readonly ConnectionPickerConnection[], collapsedGroupIds: ReadonlySet<string>, searchQuery: string): ConnectionPickerRow[] {
const connectionById = new Map(connections.map((connection) => [connection.id, connection]));
const groupById = new Map(layout.groups.map((group) => [group.id, group]));
const normalizedQuery = searchQuery.trim().toLowerCase();
if (normalizedQuery) {
const rows: ConnectionPickerRow[] = [];
const visitSearch = (entries: SidebarOrderEntry[], depth: number, groupPath: string[]): ConnectionPickerRow[] => {
const result: ConnectionPickerRow[] = [];
for (const entry of entries) {
if (entry.type === "connection") {
const connection = connectionById.get(entry.id);
if (!connection) continue;
const label = connection.name || connection.id;
if (connectionMatchesQuery(label, groupPath, normalizedQuery)) {
result.push({ key: `connection:${connection.id}`, kind: "connection", id: connection.id, label, depth, collapsed: false });
}
continue;
}
const group = groupById.get(entry.id);
if (!group) continue;
const childRows = visitSearch(groupEntryChildren(entry), depth + 1, [...groupPath, group.name]);
if (childRows.length === 0) continue;
result.push({ key: `group:${group.id}`, kind: "group", id: group.id, label: group.name, depth, collapsed: false }, ...childRows);
}
return result;
};
rows.push(...visitSearch(layout.order, 0, []));
const layoutConnectionIds = new Set<string>();
collectLayoutConnectionIds(layout.order, layoutConnectionIds);
for (const connection of connections) {
if (layoutConnectionIds.has(connection.id)) continue;
const label = connection.name || connection.id;
if (connectionMatchesQuery(label, [], normalizedQuery)) {
rows.push({ key: `connection:${connection.id}`, kind: "connection", id: connection.id, label, depth: 0, collapsed: false });
}
}
return rows;
}
const rows: ConnectionPickerRow[] = [];
const visitTree = (entries: SidebarOrderEntry[], depth: number) => {
for (const entry of entries) {
if (entry.type === "connection") {
const connection = connectionById.get(entry.id);
if (!connection) continue;
rows.push({ key: `connection:${connection.id}`, kind: "connection", id: connection.id, label: connection.name || connection.id, depth, collapsed: false });
continue;
}
const group = groupById.get(entry.id);
if (!group) continue;
const collapsed = collapsedGroupIds.has(group.id);
rows.push({ key: `group:${group.id}`, kind: "group", id: group.id, label: group.name, depth, collapsed });
if (!collapsed) visitTree(groupEntryChildren(entry), depth + 1);
}
};
visitTree(layout.order, 0);
const layoutConnectionIds = new Set<string>();
collectLayoutConnectionIds(layout.order, layoutConnectionIds);
for (const connection of connections) {
if (layoutConnectionIds.has(connection.id)) continue;
rows.push({ key: `connection:${connection.id}`, kind: "connection", id: connection.id, label: connection.name || connection.id, depth: 0, collapsed: false });
}
return rows;
}
/** Selectable rows in display order, used for keyboard navigation. */
export function connectionPickerSelectableRows(rows: readonly ConnectionPickerRow[]): ConnectionPickerRow[] {
return rows.filter((row) => row.kind === "connection");
}