refactor: stop persisting workspace state in the browser

This commit is contained in:
2026-08-14 17:43:10 +02:00
parent 3978008aed
commit fd3b62ce6f
11 changed files with 101 additions and 310 deletions
+16 -21
View File
@@ -5,36 +5,31 @@ export interface WorkspacePreference {
thinking?: string;
}
const STORAGE_KEY = "thothii.workspace-registry.v1.preferences";
let current: WorkspacePreference | undefined;
function storage(): Storage | undefined {
try { return window.localStorage; } catch { return undefined; }
function snapshot(value: WorkspacePreference | undefined): WorkspacePreference {
return value === undefined ? {} : { ...value };
}
function parse(raw: string | null): WorkspacePreference | undefined {
if (raw === null) return undefined;
try {
const value = JSON.parse(raw);
return value && typeof value === "object" && !Array.isArray(value) ? value as WorkspacePreference : undefined;
} catch {
return undefined;
}
}
/** Anonymous, non-secret browser choices for the workspace session composer. */
/** Ephemeral workspace choices for the current application process. */
export const workspacePreferences = {
load(): WorkspacePreference {
return parse(storage()?.getItem(STORAGE_KEY) ?? null) ?? {};
return snapshot(current);
},
save(value: WorkspacePreference): WorkspacePreference {
try { storage()?.setItem(STORAGE_KEY, JSON.stringify(value)); } catch { /* storage is optional */ }
return value;
current = { ...value };
return snapshot(current);
},
/** Copy the pre-local-storage server values only when this browser has no saved choice. */
migrate(legacy: WorkspacePreference): WorkspacePreference {
const current = storage()?.getItem(STORAGE_KEY);
return current === null || current === undefined ? this.save(legacy) : this.load();
/** Seed current memory from backend installation settings only before the first local choice. */
migrate(settings: WorkspacePreference): WorkspacePreference {
if (current === undefined) current = { ...settings };
return snapshot(current);
},
/** Clear ephemeral state on application/test lifecycle reset. */
reset(): void {
current = undefined;
},
};