fix(connection): share connection group path map

This commit is contained in:
vrustx authored and GitHub committed 2026-07-15 19:29:38 +08:00
1 parent 8311a88b07
commit 1c34b2b568
12 files changed
+114 -15

No files matched your search

@@ -0,0 +1,54 @@
import { createPinia, setActivePinia, storeToRefs } from "pinia";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { SidebarLayout } from "@/types/database";
const groupedLayout: SidebarLayout = {
groups: [{ id: "project", name: "Project", collapsed: false }],
order: [
{
type: "group",
id: "project",
children: [{ type: "connection", id: "grouped" }],
},
{ type: "connection", id: "root" },
],
};
describe("connectionStore connection group paths", () => {
beforeEach(() => {
vi.stubGlobal("localStorage", {
getItem: vi.fn(() => null),
setItem: vi.fn(),
removeItem: vi.fn(),
});
setActivePinia(createPinia());
});
it("shares one cached path map across store consumers", async () => {
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
store.sidebarLayout = groupedLayout;
const firstConsumer = storeToRefs(store).connectionGroupPaths;
const secondConsumer = storeToRefs(store).connectionGroupPaths;
expect(firstConsumer.value).toBe(secondConsumer.value);
expect(firstConsumer.value.get("grouped")).toEqual(["Project"]);
expect(firstConsumer.value.get("root")).toEqual([]);
});
it("rebuilds the shared map when the sidebar layout changes", async () => {
const { useConnectionStore } = await import("@/stores/connectionStore");
const store = useConnectionStore();
store.sidebarLayout = groupedLayout;
const previousPaths = store.connectionGroupPaths;
store.sidebarLayout = {
groups: [],
order: [{ type: "connection", id: "grouped" }],
};
expect(store.connectionGroupPaths).not.toBe(previousPaths);
expect(store.connectionGroupPaths.get("grouped")).toEqual([]);
});
});
@@ -34,6 +34,7 @@ import {
moveConnectionToGroup as moveConnectionToGroupOp,
remapSidebarLayoutConnectionIds,
reorderEntry as reorderEntryOp,
buildConnectionGroupPathMap,
type DropPosition,
} from "@/lib/sidebar/sidebarLayout";
import type { SqlCompletionColumn, SqlCompletionForeignKey, SqlCompletionObject, SqlCompletionTable } from "@/lib/sql/sqlCompletion";
@@ -327,6 +328,7 @@ export const useConnectionStore = defineStore("connection", () => {
allDatabases?: boolean;
} | null>(null);
const sidebarLayout = ref<SidebarLayout>(emptyLayout());
const connectionGroupPaths = computed(() => buildConnectionGroupPathMap(sidebarLayout.value));
let layoutPersistTimer: ReturnType<typeof setTimeout> | null = null;
const staleTreeRefreshIds = new Set<string>();
const metadataLoadCoordinator = new MetadataLoadCoordinator((event) => {
@@ -5318,6 +5320,7 @@ export const useConnectionStore = defineStore("connection", () => {
markConnectionLost,
recordConnectionLostError,
sidebarLayout,
connectionGroupPaths,
getConfig,
connectionIdentifierQuote,
isTreeNodePinned,