Files
zui/frontend/src/app/pleroma/projectGraphStorage.ts
2026-03-11 17:24:32 +01:00

37 lines
1.1 KiB
TypeScript

/**
* Per-project graph persistence (localStorage).
* Used by CanvasPage to load/save and by ProjectsTablePage for export.
*/
export const PROJECT_FILE_EXT = '.zui.json'
export const PROJECT_VERSION = 1
const GRAPH_KEY_PREFIX = 'zui_graph_'
export function getGraphStorageKey(projectId: string): string {
return `${GRAPH_KEY_PREFIX}${projectId}`
}
export type GraphState = { version: number; nodes: unknown[]; edges: unknown[] }
export function loadGraphFromStorage(projectId: string): GraphState | null {
try {
const raw = localStorage.getItem(getGraphStorageKey(projectId))
if (!raw) return null
const data = JSON.parse(raw) as unknown
if (!data || typeof data !== 'object' || !Array.isArray((data as GraphState).nodes) || !Array.isArray((data as GraphState).edges))
return null
return data as GraphState
} catch {
return null
}
}
export function saveGraphToStorage(projectId: string, state: GraphState): void {
localStorage.setItem(getGraphStorageKey(projectId), JSON.stringify(state))
}
export function removeGraphFromStorage(projectId: string): void {
localStorage.removeItem(getGraphStorageKey(projectId))
}