refactoring: graph state

This commit is contained in:
2026-03-12 17:40:05 +01:00
parent bfd1a40332
commit 4bd2f1b458
9 changed files with 176 additions and 58 deletions

View File

@@ -1,8 +1,11 @@
/**
* Per-project graph persistence (localStorage).
* Used by CanvasPage to load/save and by ProjectsTablePage for export.
* Saves and loads StoredGraphState (version + nodes + edges). Used by useCanvasGraph and export.
*/
import type { StoredGraphState } from '@/lib/graph/state'
export type { StoredGraphState }
export const PROJECT_FILE_EXT = '.zui.json'
export const PROJECT_VERSION = 1
@@ -12,22 +15,20 @@ 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 {
export function loadGraphFromStorage(projectId: string): StoredGraphState | 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))
if (!data || typeof data !== 'object' || !Array.isArray((data as StoredGraphState).nodes) || !Array.isArray((data as StoredGraphState).edges))
return null
return data as GraphState
return data as StoredGraphState
} catch {
return null
}
}
export function saveGraphToStorage(projectId: string, state: GraphState): void {
export function saveGraphToStorage(projectId: string, state: StoredGraphState): void {
localStorage.setItem(getGraphStorageKey(projectId), JSON.stringify(state))
}