feat(grid): persist column layouts and improve drag-to-reorder
This commit is contained in:
1 parent
f5c29f08f1
commit
44f748d3a1
24 files changed
+1532
-276
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { safeLocalStorageGet, safeLocalStorageRemove, safeLocalStorageSet } from "@/lib/backend/safeStorage";
|
||||
import { loadDataGridColumnLayout, saveDataGridColumnLayout } from "@/lib/dataGrid/dataGridColumnLayoutStorage";
|
||||
|
||||
const STORAGE_PREFIX = "dbx-document-grid-column-visibility:v1:";
|
||||
|
||||
export interface DocumentGridColumnVisibilityScope {
|
||||
databaseType?: string;
|
||||
connectionId: string;
|
||||
database: string;
|
||||
collection: string;
|
||||
}
|
||||
|
||||
export function documentGridColumnVisibilityScopeKey(scope: DocumentGridColumnVisibilityScope): string {
|
||||
return ["document-column-visibility", scope.databaseType ?? "", scope.connectionId, scope.database, scope.collection].join("\u0001");
|
||||
}
|
||||
|
||||
export function loadDocumentGridHiddenColumnKeys(scopeKey: string): string[] {
|
||||
const raw = safeLocalStorageGet(`${STORAGE_PREFIX}${scopeKey}`);
|
||||
if (!raw) return [];
|
||||
|
||||
try {
|
||||
const parsed: unknown = JSON.parse(raw);
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
return [...new Set(parsed.filter((key): key is string => typeof key === "string"))];
|
||||
} catch (error) {
|
||||
console.warn(`[DBX][document-grid-column-visibility:parse] ${scopeKey}`, error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function saveDocumentGridHiddenColumnKeys(scopeKey: string, hiddenColumnKeys: readonly string[]) {
|
||||
const normalizedKeys = [...new Set(hiddenColumnKeys)];
|
||||
if (normalizedKeys.length === 0) {
|
||||
safeLocalStorageRemove(`${STORAGE_PREFIX}${scopeKey}`);
|
||||
return;
|
||||
}
|
||||
safeLocalStorageSet(`${STORAGE_PREFIX}${scopeKey}`, JSON.stringify(normalizedKeys));
|
||||
}
|
||||
|
||||
export function migrateDocumentGridColumnVisibilityToLayout(legacyScopeKey: string, layoutScopeKey: string) {
|
||||
const legacyStorageKey = `${STORAGE_PREFIX}${legacyScopeKey}`;
|
||||
if (loadDataGridColumnLayout(layoutScopeKey)) {
|
||||
safeLocalStorageRemove(legacyStorageKey);
|
||||
return;
|
||||
}
|
||||
const hiddenKeys = loadDocumentGridHiddenColumnKeys(legacyScopeKey);
|
||||
if (hiddenKeys.length === 0) return;
|
||||
saveDataGridColumnLayout(layoutScopeKey, { orderKeys: [], hiddenKeys });
|
||||
if (loadDataGridColumnLayout(layoutScopeKey)) safeLocalStorageRemove(legacyStorageKey);
|
||||
}
|
||||
Reference in new issue
Block a user