feat(GlobalStateStore): implement storage loading and saving logic

This commit is contained in:
2026-02-07 22:48:12 +02:00
parent 552a1e5979
commit 2e23b259ab
5 changed files with 354 additions and 36 deletions

View File

@@ -2,7 +2,7 @@ import { get, set } from 'idb-keyval';
import type { GlobalState } from './GlobalStateStore.types';
const STORAGE_KEY = 'app-data';
const STORAGE_KEY = 'APP_GLO';
const SKIP_PATHS = new Set([
'app.controls',
@@ -43,50 +43,90 @@ const filterState = (state: unknown, prefix = ''): unknown => {
};
async function loadStorage(): Promise<Partial<GlobalState>> {
try {
if (typeof indexedDB !== 'undefined') {
const data = await get(STORAGE_KEY);
if (data) {
return JSON.parse(data) as Partial<GlobalState>;
const result: Partial<GlobalState> = {};
const keys: (keyof GlobalState)[] = ['layout', 'navigation', 'owner', 'program', 'session', 'user'];
for (const key of keys) {
const storageKey = `${STORAGE_KEY}:${key}`;
// Always use localStorage for session
if (key === 'session') {
try {
if (typeof localStorage !== 'undefined') {
const data = localStorage.getItem(storageKey);
if (data) {
result[key] = JSON.parse(data);
}
}
} catch (e) {
console.error(`Failed to load ${key} from localStorage:`, e);
}
continue;
}
try {
if (typeof indexedDB !== 'undefined') {
const data = await get(storageKey);
if (data) {
result[key] = JSON.parse(data);
continue;
}
}
} catch (e) {
console.error(`Failed to load ${key} from IndexedDB, falling back to localStorage:`, e);
}
try {
if (typeof localStorage !== 'undefined') {
const data = localStorage.getItem(storageKey);
if (data) {
result[key] = JSON.parse(data);
}
}
} catch (e) {
console.error(`Failed to load ${key} from localStorage:`, e);
}
} catch (e) {
console.error('Failed to load from IndexedDB, falling back to localStorage:', e);
}
try {
if (typeof localStorage !== 'undefined') {
const data = localStorage.getItem(STORAGE_KEY);
if (data) {
return JSON.parse(data) as Partial<GlobalState>;
}
}
} catch (e) {
console.error('Failed to load from localStorage:', e);
}
return {};
return result;
}
async function saveStorage(state: GlobalState): Promise<void> {
const filtered = filterState(state);
const serialized = JSON.stringify(filtered);
const keys: (keyof GlobalState)[] = ['layout', 'navigation', 'owner', 'program', 'session', 'user'];
try {
if (typeof indexedDB !== 'undefined') {
await set(STORAGE_KEY, serialized);
return;
}
} catch (e) {
console.error('Failed to save to IndexedDB, falling back to localStorage:', e);
}
for (const key of keys) {
const storageKey = `${STORAGE_KEY}:${key}`;
const filtered = filterState(state[key], key);
const serialized = JSON.stringify(filtered);
try {
if (typeof localStorage !== 'undefined') {
localStorage.setItem(STORAGE_KEY, serialized);
// Always use localStorage for session
if (key === 'session') {
try {
if (typeof localStorage !== 'undefined') {
localStorage.setItem(storageKey, serialized);
}
} catch (e) {
console.error(`Failed to save ${key} to localStorage:`, e);
}
continue;
}
try {
if (typeof indexedDB !== 'undefined') {
await set(storageKey, serialized);
continue;
}
} catch (e) {
console.error(`Failed to save ${key} to IndexedDB, falling back to localStorage:`, e);
}
try {
if (typeof localStorage !== 'undefined') {
localStorage.setItem(storageKey, serialized);
}
} catch (e) {
console.error(`Failed to save ${key} to localStorage:`, e);
}
} catch (e) {
console.error('Failed to save to localStorage:', e);
}
}