Image Pipeline Playground
feat(frontend): persist workflow to localStorage across reloads
Mohammad Rajaeiaa00e2a273f7
diff --git a/frontend/src/app/App.tsx b/frontend/src/app/App.tsxindex e1044bf..8248cd5 100644--- a/frontend/src/app/App.tsx+++ b/frontend/src/app/App.tsx@@ -13,6 +13,7 @@ import {requestCodegen,useExecutionSocket,} from '../hooks/useExecutionSocket'+import { useWorkflowPersistence } from '../hooks/useWorkflowPersistence'import { notifyError, notifyInfo, notifySuccess } from '../notify'import { downloadWorkflowJson, parseWorkflowJson } from '../workflow/io'import type { WorkflowTemplate } from '../workflow/templates'@@ -31,6 +32,7 @@ export default function App() {const [sideTab, setSideTab] = useState(0)useEffect(() => bindExecutionRunner(run), [run])+ useWorkflowPersistence()const onExportPython = async () => {try {diff --git a/frontend/src/hooks/useWorkflowPersistence.ts b/frontend/src/hooks/useWorkflowPersistence.tsnew file mode 100644index 0000000..f9e4338--- /dev/null+++ b/frontend/src/hooks/useWorkflowPersistence.ts@@ -0,0 +1,46 @@+import { useEffect, useRef, useState } from 'react'+import { useGraphStore } from '../store/graphStore'+import { loadWorkflowSession, saveWorkflowSession } from '../workflow/persist'++const SAVE_DEBOUNCE_MS = 400++/**+ * Restore the last workflow from localStorage once the node catalog is ready,+ * then autosave graph edits so a reload does not wipe the canvas.+ */+export function useWorkflowPersistence() {+ const nodeCatalog = useGraphStore((s) => s.nodeCatalog)+ const loadWorkflow = useGraphStore((s) => s.loadWorkflow)+ const toWorkflowDocument = useGraphStore((s) => s.toWorkflowDocument)+ const nodes = useGraphStore((s) => s.nodes)+ const edges = useGraphStore((s) => s.edges)+ const seed = useGraphStore((s) => s.seed)+ const sampleCount = useGraphStore((s) => s.sampleCount)+ const [sessionReady, setSessionReady] = useState(false)+ const restoredRef = useRef(false)++ useEffect(() => {+ if (restoredRef.current || nodeCatalog.length === 0) return+ restoredRef.current = true+ const saved = loadWorkflowSession()+ if (saved && saved.graph.nodes.length > 0) {+ loadWorkflow(saved)+ }+ setSessionReady(true)+ }, [nodeCatalog, loadWorkflow])++ useEffect(() => {+ if (!sessionReady) return+ const timer = window.setTimeout(() => {+ saveWorkflowSession(toWorkflowDocument())+ }, SAVE_DEBOUNCE_MS)+ return () => window.clearTimeout(timer)+ }, [sessionReady, nodes, edges, seed, sampleCount, toWorkflowDocument])++ useEffect(() => {+ if (!sessionReady) return+ const flush = () => saveWorkflowSession(toWorkflowDocument())+ window.addEventListener('beforeunload', flush)+ return () => window.removeEventListener('beforeunload', flush)+ }, [sessionReady, toWorkflowDocument])+}diff --git a/frontend/src/workflow/persist.ts b/frontend/src/workflow/persist.tsnew file mode 100644index 0000000..bd0ec25--- /dev/null+++ b/frontend/src/workflow/persist.ts@@ -0,0 +1,34 @@+/** Browser session persistence for the current workflow. */++import {+ coerceWorkflowDocument,+ type WorkflowDocument,+} from './io'++export const WORKFLOW_SESSION_KEY = 'image-pipes.workflow.v1'++export function saveWorkflowSession(doc: WorkflowDocument): void {+ try {+ localStorage.setItem(WORKFLOW_SESSION_KEY, JSON.stringify(doc))+ } catch {+ // Quota or private mode — ignore; export still works.+ }+}++export function loadWorkflowSession(): WorkflowDocument | null {+ try {+ const raw = localStorage.getItem(WORKFLOW_SESSION_KEY)+ if (!raw) return null+ return coerceWorkflowDocument(JSON.parse(raw) as unknown)+ } catch {+ return null+ }+}++export function clearWorkflowSession(): void {+ try {+ localStorage.removeItem(WORKFLOW_SESSION_KEY)+ } catch {+ // ignore+ }+}