fix(desktop): preserve sidebar groups on reload

This commit is contained in:
Guoyu Su authored and GitHub committed 2026-07-09 11:47:13 +08:00
1 parent 1317301bcd
commit 65a92afce8
2 files changed
+60 -1

No files matched your search

+2 -1
View File
@@ -4926,9 +4926,10 @@ export const useConnectionStore = defineStore("connection", () => {
const saved = await api.loadConnections(); const saved = await api.loadConnections();
connections.value = saved.map(normalizeConnection); connections.value = saved.map(normalizeConnection);
const savedLayout = await api.loadSidebarLayout(); const savedLayout = await api.loadSidebarLayout();
const currentLayout = sidebarLayout.value.groups.length || sidebarLayout.value.order.length ? sidebarLayout.value : null;
sidebarLayout.value = reconcileLayout( sidebarLayout.value = reconcileLayout(
connections.value.map((c) => c.id), connections.value.map((c) => c.id),
savedLayout, savedLayout ?? currentLayout,
); );
rebuildTreeNodes(); rebuildTreeNodes();
})().finally(() => { })().finally(() => {
@@ -153,6 +153,64 @@ test("duplicating a grouped connection keeps the copy in the same group", async
} }
}); });
test("reloading connections preserves the current grouped layout when the saved layout is temporarily unavailable", async () => {
const originalFetch = globalThis.fetch;
const storage = installMemoryStorage();
const savedConnections: ConnectionConfig[] = [
conn("pg", "pg"),
conn("pg2", "pg2"),
conn("pg3", "pg3"),
conn("pg4", "pg4"),
];
let savedLayout: SidebarLayout | null = {
groups: [
{ id: "group-a", name: "dir[a]", collapsed: false },
{ id: "group-b", name: "dir[b]", collapsed: false },
],
order: [
{ type: "group", id: "group-a", connectionIds: ["pg", "pg2"] },
{ type: "group", id: "group-b", connectionIds: ["pg3", "pg4"] },
],
};
globalThis.fetch = (async (input, init) => {
const url = String(input);
if (url === "/api/connection/list") {
return new Response(JSON.stringify(savedConnections), { status: 200, headers: { "Content-Type": "application/json" } });
}
if (url === "/api/layout/sidebar") {
if (init?.method === "POST") {
savedLayout = JSON.parse(String(init.body ?? "null"));
return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } });
}
return new Response(JSON.stringify(savedLayout), { status: 200, headers: { "Content-Type": "application/json" } });
}
return new Response("null", { status: 200, headers: { "Content-Type": "application/json" } });
}) as typeof fetch;
try {
setActivePinia(createPinia());
const store = useConnectionStore();
await store.initFromDisk();
assert.deepEqual(
store.treeNodes.map((node) => node.label),
["dir[a]", "dir[b]"],
);
savedLayout = null;
await store.initFromDisk();
assert.deepEqual(
store.treeNodes.map((node) => node.label),
["dir[a]", "dir[b]"],
);
} finally {
globalThis.fetch = originalFetch;
storage.restore();
}
});
test("importing grouped dbx connections remaps exported layout to new connection ids", async () => { test("importing grouped dbx connections remaps exported layout to new connection ids", async () => {
const originalFetch = globalThis.fetch; const originalFetch = globalThis.fetch;
const storage = installMemoryStorage(); const storage = installMemoryStorage();