refactoring: graph state
This commit is contained in:
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user