fix(grid): preserve current page on data refresh instead of resetting to page 1

* @
fix(grid): preserve current page on data refresh instead of resetting to page 1

When refreshing table data (toolbar button, Ctrl+R, or auto-refresh), the
offset was hardcoded to 0, causing pagination to always jump back to the
first page. This was frustrating for users viewing later pages.

Use the current page offset (currentPage - 1) * pageSize so the user
stays on the same page after refresh. In infinite-scroll mode, the
offset still evaluates to 0 because resetInfiniteScrollState() sets
currentPage = 1, so behavior is unchanged.
@

* @
fix(grid): auto-redirect to last page when refreshed page no longer exists

Extends the refresh pagination fix with a safety net: when data is deleted
while viewing a later page (e.g. page 5 with only 2 pages remaining), the
grid auto-navigates to the last available page instead of showing an empty
page.

Uses a transient isRefreshingData flag to distinguish refresh/rollback
from normal pagination navigation, avoiding false triggers.
@

* Revert "@"

This reverts commit ff0bf0bb31.

* @
fix(grid): auto-redirect to last page when refreshed page no longer exists

When data is deleted while viewing a later page, the grid now auto-navigates
to the last available page instead of showing an empty page.

Uses a transient isRefreshingData flag to distinguish refresh/rollback
from normal pagination, and a loading-transition watcher (true->false)
placed after displayedTotalRowCount declaration to avoid TDZ errors.
@

* @
test(grid): add pagination offset-preservation and auto-redirect tests

Cover the two new behaviors added to DataGrid.vue:

1. dataGridPagination.test.ts — auto-redirect page calculation:
   - page beyond last page after data deletion → triggers redirect
   - page still valid → no redirect
   - fewer rows than one page → redirects to page 1
   - total=0 / total=undefined → guard prevents redirect

2. useDataGridActions.test.ts — offset preserved on reload:
   - onReloadData(offset=400) passes offset=400 to build-table-select-sql
   - resultPageOffset stored as 400 (not reset to 0)
@
This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-07-08 13:28:36 +08:00
1 parent 2929b1c035
commit 0901f176b0
3 files changed
+126 -2

No files matched your search

@@ -120,3 +120,53 @@ test("data reload executes before slow metadata refresh completes", async () =>
restoreStorage();
}
});
test("data reload preserves current page offset instead of resetting to page 1", async () => {
// Verifies the fix: onReloadData called with offset=400 (page 5, pageSize=100)
// should pass offset=400 to build-table-select-sql and store it in resultPageOffset,
// rather than defaulting to offset=0.
const restoreStorage = installMemoryStorage();
const originalFetch = globalThis.fetch;
const { useDataGridActions } = await import("../../apps/desktop/src/composables/useDataGridActions.ts");
let buildSqlOptions: any;
let executePagination: any;
globalThis.fetch = (async (input, init) => {
const url = new URL(String(input), "http://localhost");
if (url.pathname === "/api/connection/check-health") return Response.json(null);
if (url.pathname === "/api/schema/columns") return Response.json([]);
if (url.pathname === "/api/schema/indexes") return Response.json([]);
if (url.pathname === "/api/query/build-table-select-sql") {
buildSqlOptions = JSON.parse(String(init?.body ?? "{}"))?.options;
return Response.json(`SELECT * FROM \`orders\` LIMIT 100 OFFSET ${buildSqlOptions?.offset ?? 0}`);
}
if (url.pathname === "/api/query/execute-multi") {
const body = JSON.parse(String(init?.body ?? "{}"));
executePagination = body?.page_offset;
return Response.json([{ columns: ["id"], rows: [[42]], affected_rows: 0, execution_time_ms: 1 }]);
}
return new Response(`unexpected ${url.pathname}`, { status: 500 });
}) as typeof fetch;
try {
setActivePinia(createPinia());
const connectionStore = useConnectionStore();
const queryStore = useQueryStore();
connectionStore.addEphemeralConnection(conn("mysql-1"));
const tabId = queryStore.createTab("mysql-1", "app", "orders", "data");
queryStore.setTableMeta(tabId, { tableName: "orders", tableType: "TABLE", columns: [], primaryKeys: [] });
const tab = queryStore.tabs.find((item) => item.id === tabId);
assert.ok(tab);
const actions = useDataGridActions(computed(() => tab));
// Simulate refresh from page 5 with pageSize=100 → offset=400
await actions.onReloadData(undefined, undefined, undefined, undefined, 100, 400);
assert.equal(buildSqlOptions?.offset, 400, "build-table-select-sql must receive offset=400 to stay on page 5");
assert.equal(buildSqlOptions?.limit, 100, "limit must be 100");
assert.equal(tab.resultPageOffset, 400, "resultPageOffset must be 400 after reload to keep DataGrid on page 5");
} finally {
globalThis.fetch = originalFetch;
restoreStorage();
}
});