feat(tabs): add connection database table hierarchy
This commit is contained in:
21 files changed
+637
-39
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { buildConnectionDatabaseTabGroupStripEntries } from "@/lib/tabs/tabGroupTree";
|
||||
import type { QueryTab } from "@/types/database";
|
||||
|
||||
function tab(id: string, connectionId: string, database: string, catalog?: string): QueryTab {
|
||||
return { id, connectionId, database, catalog, title: id } as QueryTab;
|
||||
}
|
||||
|
||||
describe("buildConnectionDatabaseTabGroupStripEntries", () => {
|
||||
it("builds connection, database and tab levels without sidebar folders", () => {
|
||||
const entries = buildConnectionDatabaseTabGroupStripEntries([tab("users", "mysql", "app"), tab("orders", "mysql", "app"), tab("audit", "mysql", "logs"), tab("users", "pg", "app")], false);
|
||||
|
||||
expect(entries.map((entry) => (entry.kind === "header" ? `${entry.level}:${entry.label}` : `tab:${entry.tab.id}`))).toEqual(["connection:mysql", "database:app", "tab:users", "tab:orders", "database:logs", "tab:audit", "connection:pg", "database:app", "tab:users"]);
|
||||
expect(entries.filter((entry) => entry.kind === "header").every((entry) => entry.level === "connection" || entry.level === "database")).toBe(true);
|
||||
const mysqlConnection = entries[0];
|
||||
expect(mysqlConnection?.kind === "header" && mysqlConnection.treeGuides).toEqual([{ depth: 0, kind: "start" }]);
|
||||
const firstDatabase = entries[1];
|
||||
expect(firstDatabase?.kind === "header" && firstDatabase.treeGuides).toEqual([
|
||||
{ depth: 0, kind: "branch" },
|
||||
{ depth: 1, kind: "start" },
|
||||
]);
|
||||
const firstDatabaseLastTab = entries[3];
|
||||
expect(firstDatabaseLastTab?.kind === "tab" && firstDatabaseLastTab.treeGuides).toEqual([
|
||||
{ depth: 0, kind: "through" },
|
||||
{ depth: 1, kind: "last-branch" },
|
||||
]);
|
||||
const postgresConnection = entries[6];
|
||||
expect(postgresConnection?.kind === "header" && postgresConnection.treeGuides).toEqual([{ depth: 0, kind: "start" }]);
|
||||
});
|
||||
|
||||
it("keeps pinned and regular hierarchy IDs separate", () => {
|
||||
const regular = buildConnectionDatabaseTabGroupStripEntries([tab("users", "mysql", "app")], false);
|
||||
const pinned = buildConnectionDatabaseTabGroupStripEntries([tab("users", "mysql", "app")], true);
|
||||
|
||||
expect(regular[0]?.groupId).not.toBe(pinned[0]?.groupId);
|
||||
expect(regular[1]?.groupId).not.toBe(pinned[1]?.groupId);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,146 @@
|
||||
import type { QueryTab } from "@/types/database";
|
||||
|
||||
export type TabHierarchyLevel = "connection" | "database";
|
||||
|
||||
export interface TabGroupTreeGuide {
|
||||
depth: number;
|
||||
kind: "start" | "through" | "branch" | "last-branch";
|
||||
}
|
||||
|
||||
export interface ConnectionDatabaseTabGroupHeaderEntry {
|
||||
kind: "header";
|
||||
key: string;
|
||||
groupId: string;
|
||||
label: string;
|
||||
level: TabHierarchyLevel;
|
||||
depth: number;
|
||||
count: number;
|
||||
pinned: boolean;
|
||||
tab: QueryTab;
|
||||
tabIds: string[];
|
||||
ancestorIds: string[];
|
||||
treeGuides: TabGroupTreeGuide[];
|
||||
}
|
||||
|
||||
export interface ConnectionDatabaseTabGroupTabEntry {
|
||||
kind: "tab";
|
||||
key: string;
|
||||
groupId: string;
|
||||
tab: QueryTab;
|
||||
depth: number;
|
||||
groupFirst: boolean;
|
||||
groupLast: boolean;
|
||||
grouping: true;
|
||||
ancestorIds: string[];
|
||||
treeGuides: TabGroupTreeGuide[];
|
||||
}
|
||||
|
||||
export type ConnectionDatabaseTabGroupStripEntry = ConnectionDatabaseTabGroupHeaderEntry | ConnectionDatabaseTabGroupTabEntry;
|
||||
|
||||
const CONNECTION_GROUP_ID_PREFIX = "sidebar-connection:";
|
||||
const DATABASE_GROUP_ID_PREFIX = "sidebar-database:";
|
||||
|
||||
function sectionPrefix(pinned: boolean): string {
|
||||
return pinned ? "fixed:" : "regular:";
|
||||
}
|
||||
|
||||
function connectionGroupId(connectionId: string, pinned: boolean): string {
|
||||
return `${sectionPrefix(pinned)}${CONNECTION_GROUP_ID_PREFIX}${connectionId}`;
|
||||
}
|
||||
|
||||
function databaseGroupKey(tab: QueryTab): string {
|
||||
return JSON.stringify([tab.connectionId, tab.catalog || "", tab.database || ""]);
|
||||
}
|
||||
|
||||
function databaseGroupId(tab: QueryTab, pinned: boolean): string {
|
||||
return `${sectionPrefix(pinned)}${DATABASE_GROUP_ID_PREFIX}${databaseGroupKey(tab)}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Projects the current pane into a stable three-level hierarchy:
|
||||
* connection -> database -> table/query tab.
|
||||
*
|
||||
* The sidebar's user-created folders are deliberately ignored. Tabs without
|
||||
* a database still receive a database bucket so every tab follows the same
|
||||
* shape and remains reachable through the connection branch.
|
||||
*/
|
||||
export function buildConnectionDatabaseTabGroupStripEntries(tabs: QueryTab[], pinned: boolean): ConnectionDatabaseTabGroupStripEntry[] {
|
||||
if (tabs.length === 0) return [];
|
||||
|
||||
const tabsByConnection = new Map<string, QueryTab[]>();
|
||||
for (const tab of tabs) {
|
||||
const bucket = tabsByConnection.get(tab.connectionId) ?? [];
|
||||
bucket.push(tab);
|
||||
tabsByConnection.set(tab.connectionId, bucket);
|
||||
}
|
||||
|
||||
const entries: ConnectionDatabaseTabGroupStripEntry[] = [];
|
||||
for (const [connectionId, connectionTabs] of tabsByConnection) {
|
||||
const connectionIdValue = connectionGroupId(connectionId, pinned);
|
||||
const databaseBuckets = new Map<string, QueryTab[]>();
|
||||
for (const tab of connectionTabs) {
|
||||
const key = databaseGroupKey(tab);
|
||||
const bucket = databaseBuckets.get(key) ?? [];
|
||||
bucket.push(tab);
|
||||
databaseBuckets.set(key, bucket);
|
||||
}
|
||||
const databaseEntries = [...databaseBuckets.values()];
|
||||
entries.push({
|
||||
kind: "header",
|
||||
key: `header:${connectionIdValue}`,
|
||||
groupId: connectionIdValue,
|
||||
label: connectionId,
|
||||
level: "connection",
|
||||
depth: 0,
|
||||
count: connectionTabs.length,
|
||||
pinned,
|
||||
tab: connectionTabs[0]!,
|
||||
tabIds: connectionTabs.map((tab) => tab.id),
|
||||
ancestorIds: [],
|
||||
treeGuides: [{ depth: 0, kind: "start" }],
|
||||
});
|
||||
|
||||
databaseEntries.forEach((databaseTabs, databaseIndex) => {
|
||||
const lastDatabase = databaseIndex === databaseEntries.length - 1;
|
||||
const representativeTab = databaseTabs[0]!;
|
||||
const databaseId = databaseGroupId(representativeTab, pinned);
|
||||
entries.push({
|
||||
kind: "header",
|
||||
key: `header:${databaseId}`,
|
||||
groupId: databaseId,
|
||||
label: representativeTab.database || representativeTab.catalog || connectionId,
|
||||
level: "database",
|
||||
depth: 1,
|
||||
count: databaseTabs.length,
|
||||
pinned,
|
||||
tab: representativeTab,
|
||||
tabIds: databaseTabs.map((tab) => tab.id),
|
||||
ancestorIds: [connectionIdValue],
|
||||
treeGuides: [
|
||||
{ depth: 0, kind: lastDatabase ? "last-branch" : "branch" },
|
||||
{ depth: 1, kind: "start" },
|
||||
],
|
||||
});
|
||||
|
||||
databaseTabs.forEach((tab, index) => {
|
||||
entries.push({
|
||||
kind: "tab",
|
||||
key: tab.id,
|
||||
groupId: databaseId,
|
||||
tab,
|
||||
depth: 2,
|
||||
groupFirst: index === 0,
|
||||
groupLast: index === databaseTabs.length - 1,
|
||||
grouping: true,
|
||||
ancestorIds: [connectionIdValue, databaseId],
|
||||
// Continue the connection rail across this database's tabs only
|
||||
// when another database follows. Every table owns a centered branch
|
||||
// to its own database rail; only the final table ends that rail.
|
||||
treeGuides: [...(lastDatabase ? [] : [{ depth: 0, kind: "through" as const }]), { depth: 1, kind: index === databaseTabs.length - 1 ? "last-branch" : "branch" }],
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return entries;
|
||||
}
|
||||
Reference in new issue
Block a user