Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(sqlFiles): keep rows mounted while a folder re-scans

弈秋6b608a0b390b
diff --git a/apps/desktop/src/components/layout/SqlFilePanel.vue b/apps/desktop/src/components/layout/SqlFilePanel.vue
index d4bee8f752..7f6fd4c084 100644
--- a/apps/desktop/src/components/layout/SqlFilePanel.vue
+++ b/apps/desktop/src/components/layout/SqlFilePanel.vue
@@ -41,6 +41,7 @@ interface FolderState {
entries: SqlFileEntry[];
expanded: Set<string>;
loading: boolean;
+ refreshing: boolean;
collapsed: boolean;
}
@@ -214,6 +215,7 @@ async function addFolder(folderPath: string) {
entries: [],
expanded: new Set(),
loading: true,
+ refreshing: false,
collapsed: false,
};
folders.value.push(folder);
@@ -257,10 +259,18 @@ async function locateCurrentFile() {
// Re-scan a single top-level folder and replace its entries. Mutated via the
// reactive proxy (folders.value[idx]) so Vue tracks the change — see the note
// in addFolder above.
+//
+// `loading` only goes true when the folder has nothing to show yet, because it
+// swaps the row list for a "loading" line and the list lives inside the panel's
+// only scroll container: tearing the rows down resets `scrollTop` to 0, which is
+// what made the panel jump back to the top after every add/rename/refresh
+// (#13038). A folder that already has entries stays rendered and is swapped in
+// place, so the scroll offset survives; `refreshing` only drives the spinner.
async function loadFolderEntries(folderPath: string): Promise<string | null> {
const idx = folders.value.findIndex((f) => f.path === folderPath);
if (idx === -1) return null;
- folders.value[idx].loading = true;
+ folders.value[idx].loading = folders.value[idx].entries.length === 0;
+ folders.value[idx].refreshing = true;
try {
const entries = await api.listSqlFilesInFolder(folderPath, getSqlFileFilter(), true);
const target = folders.value.findIndex((f) => f.path === folderPath);
@@ -289,6 +299,7 @@ async function loadFolderEntries(folderPath: string): Promise<string | null> {
const target = folders.value.findIndex((f) => f.path === folderPath);
if (target !== -1) {
folders.value[target].loading = false;
+ folders.value[target].refreshing = false;
}
}
return null;
@@ -1036,7 +1047,7 @@ function clearContextTarget() {
</HelpTooltip>
<span class="flex-1" />
<LightTooltip :text="t('sqlFileTree.locateActiveFile')" side="bottom" :delay="0" :close-delay="0" nowrap>
- <SidebarLocateButton class="disabled:pointer-events-none disabled:opacity-50" :label="t('sqlFileTree.locateActiveFile')" :disabled="!currentFilePath || locatingFile || folders.some((folder) => folder.loading)" @locate="locateCurrentFile" />
+ <SidebarLocateButton class="disabled:pointer-events-none disabled:opacity-50" :label="t('sqlFileTree.locateActiveFile')" :disabled="!currentFilePath || locatingFile || folders.some((folder) => folder.loading || folder.refreshing)" @locate="locateCurrentFile" />
</LightTooltip>
<LightTooltip :text="t('sqlFileTree.panelSettings')" side="bottom" :delay="0" :close-delay="0" nowrap>
<Button
@@ -1139,7 +1150,7 @@ function clearContextTarget() {
<span class="truncate flex-1 text-[10px] text-muted-foreground/50" :title="folder.path">{{ folder.path }}</span>
<LightTooltip :text="t('sqlFileTree.refreshFolder')" side="bottom" :delay="0" :close-delay="0" nowrap>
<Button variant="ghost" size="icon" class="h-4 w-4 shrink-0 text-muted-foreground hover:text-foreground" @click.stop="refreshFolder(folder.path)">
- <RefreshCw class="h-3 w-3" :class="folder.loading ? 'animate-spin' : ''" />
+ <RefreshCw class="h-3 w-3" :class="folder.loading || folder.refreshing ? 'animate-spin' : ''" />
</Button>
</LightTooltip>
<LightTooltip :text="t('sqlFileTree.newSqlFile')" side="bottom" :delay="0" :close-delay="0" nowrap>
diff --git a/apps/desktop/src/components/layout/__tests__/SqlFilePanel.refreshScroll.spec.ts b/apps/desktop/src/components/layout/__tests__/SqlFilePanel.refreshScroll.spec.ts
new file mode 100644
index 0000000000..3f23db3989
--- /dev/null
+++ b/apps/desktop/src/components/layout/__tests__/SqlFilePanel.refreshScroll.spec.ts
@@ -0,0 +1,104 @@
+// @vitest-environment happy-dom
+
+import { createApp, type App } from "vue";
+import { createPinia, setActivePinia } from "pinia";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+import i18n from "@/i18n";
+
+const mocks = vi.hoisted(() => ({
+ listSqlFilesInFolder: vi.fn(),
+}));
+
+vi.mock("@/lib/sqlFile/sqlFileFolders", () => ({
+ getSqlFileFilter: () => "*.sql",
+ getSqlFileFolderPaths: () => ["/sql"],
+ saveSqlFileFilter: vi.fn(),
+ saveSqlFileFolderPaths: vi.fn(),
+ notifySqlFileFoldersChanged: vi.fn(),
+}));
+vi.mock("@/lib/backend/tauriRuntime", () => ({ isTauriRuntime: () => false }));
+vi.mock("@/lib/backend/api", async (importOriginal) => ({
+ ...(await importOriginal<typeof import("@/lib/backend/api")>()),
+ listSqlFilesInFolder: mocks.listSqlFilesInFolder,
+}));
+
+import SqlFilePanel from "../SqlFilePanel.vue";
+
+const file = (name: string) => ({ name, path: `/sql/${name}`, is_dir: false, children: [] });
+
+const mounted: Array<{ app: App; host: HTMLElement }> = [];
+
+beforeEach(() => {
+ mocks.listSqlFilesInFolder.mockReset();
+ mocks.listSqlFilesInFolder.mockImplementation(async () => [file("a.sql")]);
+});
+
+afterEach(() => {
+ for (const { app, host } of mounted.splice(0)) {
+ app.unmount();
+ host.remove();
+ }
+ vi.restoreAllMocks();
+});
+
+async function mountPanel() {
+ const pinia = createPinia();
+ setActivePinia(pinia);
+ const host = document.createElement("div");
+ document.body.appendChild(host);
+ const app = createApp(SqlFilePanel);
+ app.use(pinia);
+ app.use(i18n);
+ app.mount(host);
+ mounted.push({ app, host });
+ await vi.waitFor(() => expect(host.querySelector('[data-sql-file-path="/sql/a.sql"]')).not.toBeNull());
+ return host;
+}
+
+describe("SQL file panel refresh keeps the list mounted", () => {
+ // Regression for #13038: `loadFolderEntries` used to flip `folder.loading` on
+ // for every re-scan, which swapped the row list for a "loading" line. The rows
+ // live in the panel's only scroll container, so tearing them down reset
+ // `scrollTop` and the panel jumped back to the top after every add/rename.
+ it("reuses the existing rows while a folder re-scan is in flight", async () => {
+ const host = await mountPanel();
+ const firstRow = host.querySelector<HTMLElement>('[data-sql-file-path="/sql/a.sql"]')!;
+ expect(mocks.listSqlFilesInFolder).toHaveBeenCalledTimes(1);
+
+ // The refresh button is the first button in the sticky folder header.
+ const header = host.querySelector<HTMLElement>(".sticky")!;
+ const refreshButton = header.querySelectorAll<HTMLButtonElement>("button")[0];
+ refreshButton.click();
+
+ // Mid-flight the row list must still be on screen: the "loading" placeholder
+ // may only replace rows for a folder that has never been scanned.
+ await vi.waitFor(() => expect(mocks.listSqlFilesInFolder).toHaveBeenCalledTimes(2));
+ expect(host.querySelector('[data-sql-file-path="/sql/a.sql"]')).toBe(firstRow);
+ expect(firstRow.isConnected).toBe(true);
+
+ mocks.listSqlFilesInFolder.mockImplementation(async () => [file("a.sql"), file("b.sql")]);
+ refreshButton.click();
+ await vi.waitFor(() => expect(host.querySelector('[data-sql-file-path="/sql/b.sql"]')).not.toBeNull());
+ expect(host.querySelector('[data-sql-file-path="/sql/a.sql"]')).toBe(firstRow);
+ expect(firstRow.isConnected).toBe(true);
+ });
+
+ it("still shows the loading placeholder for a folder that has never been scanned", async () => {
+ // A never-resolving scan keeps the folder in its initial state, so the
+ // placeholder must stay visible instead of flashing "no SQL files".
+ mocks.listSqlFilesInFolder.mockImplementation(() => new Promise(() => {}));
+ const pinia = createPinia();
+ setActivePinia(pinia);
+ const host = document.createElement("div");
+ document.body.appendChild(host);
+ const app = createApp(SqlFilePanel);
+ app.use(pinia);
+ app.use(i18n);
+ app.mount(host);
+ mounted.push({ app, host });
+
+ await vi.waitFor(() => expect(host.textContent).toContain(i18n.global.t("sqlFileTree.loading")));
+ expect(host.textContent).not.toContain(i18n.global.t("sqlFileTree.noSqlFiles"));
+ expect(host.querySelector('[data-sql-file-path="/sql/a.sql"]')).toBeNull();
+ });
+});