fix(document): keep collection filter and sort across tab switches

Refs: #8035
This commit is contained in:
t8y2 committed 2026-09-05 20:00:04 +08:00
1 parent 290576cb4f
commit da03c1f0c4
5 files changed
+458 -6

No files matched your search

@@ -0,0 +1,59 @@
import { describe, expect, it } from "vitest";
import { clearDocumentBrowserState, restoreDocumentBrowserState, saveDocumentBrowserState, type DocumentBrowserStateSnapshot } from "@/lib/tabs/documentBrowserStateCache";
function snapshot(page: number): DocumentBrowserStateSnapshot {
return {
filterInput: `{"page":${page}}`,
sortInput: `{"f":${page}}`,
appliedDocumentFilter: page % 2 === 0 ? { status: "active" } : null,
documentFilterRules: page % 2 === 0 ? [{ id: `rule-${page}`, fieldName: "status", mode: "equals", rawValue: "active", conjunction: "AND" }] : [],
page,
};
}
describe("documentBrowserStateCache", () => {
it("round-trips a snapshot for the same key", () => {
saveDocumentBrowserState("round-trip", snapshot(3));
expect(restoreDocumentBrowserState("round-trip")).toEqual(snapshot(3));
});
it("returns undefined for an unknown key", () => {
expect(restoreDocumentBrowserState("missing")).toBeUndefined();
});
it("keeps the most recent write for a key", () => {
saveDocumentBrowserState("overwrite", snapshot(1));
saveDocumentBrowserState("overwrite", snapshot(2));
expect(restoreDocumentBrowserState("overwrite")).toEqual(snapshot(2));
});
it("evicts the oldest entry beyond 32 keys", () => {
for (let index = 0; index < 33; index += 1) {
saveDocumentBrowserState(`evict-${index}`, snapshot(index));
}
expect(restoreDocumentBrowserState("evict-0")).toBeUndefined();
expect(restoreDocumentBrowserState("evict-1")).toBeDefined();
expect(restoreDocumentBrowserState("evict-32")).toBeDefined();
});
it("treats a restore as recency so a touched entry survives eviction", () => {
for (let index = 0; index < 32; index += 1) {
saveDocumentBrowserState(`touch-${index}`, snapshot(index));
}
expect(restoreDocumentBrowserState("touch-0")).toEqual(snapshot(0));
saveDocumentBrowserState("touch-new", snapshot(99));
expect(restoreDocumentBrowserState("touch-0")).toBeDefined();
expect(restoreDocumentBrowserState("touch-1")).toBeUndefined();
});
it("clears a key on demand", () => {
saveDocumentBrowserState("clear-me", snapshot(4));
clearDocumentBrowserState("clear-me");
expect(restoreDocumentBrowserState("clear-me")).toBeUndefined();
});
});
@@ -0,0 +1,45 @@
import type { DocumentFilterRule } from "@/lib/app/documentStoreProvider";
export interface DocumentBrowserStateSnapshot {
/** Manual JSON filter text from the collection filter input. */
filterInput: string;
/** Manual JSON sort text from the collection sort input. */
sortInput: string;
/** Structured-filter contribution applied on top of the manual filter. */
appliedDocumentFilter: Record<string, unknown> | null;
/** Rules backing the structured filter builder. */
documentFilterRules: DocumentFilterRule[];
/** Zero-based page position; only meaningful for skip-based stores. */
page: number;
}
// ContentArea renders only the active tab, so DocumentBrowser is unmounted on
// every tab switch and remounted when the user comes back. Query conditions
// survive that round trip through this session-scoped cache keyed by tab id —
// the same role tab.whereInput/tab.orderByInput play for SQL data tabs. The
// bound keeps state for long tab sessions from accumulating forever.
const MAX_ENTRIES = 32;
const cache = new Map<string, DocumentBrowserStateSnapshot>();
export function restoreDocumentBrowserState(stateKey: string): DocumentBrowserStateSnapshot | undefined {
const snapshot = cache.get(stateKey);
if (!snapshot) return undefined;
// Refresh LRU recency.
cache.delete(stateKey);
cache.set(stateKey, snapshot);
return snapshot;
}
export function saveDocumentBrowserState(stateKey: string, snapshot: DocumentBrowserStateSnapshot): void {
cache.delete(stateKey);
cache.set(stateKey, snapshot);
while (cache.size > MAX_ENTRIES) {
const oldest = cache.keys().next().value;
if (oldest === undefined) break;
cache.delete(oldest);
}
}
export function clearDocumentBrowserState(stateKey: string): void {
cache.delete(stateKey);
}