Image Pipeline Playground
feat(frontend/store): add local project library and session project id
Mohammad Rajaei77f8c2875f23
diff --git a/frontend/src/hooks/useWorkflowPersistence.ts b/frontend/src/hooks/useWorkflowPersistence.tsindex 4fdc5e0..c5cb1ed 100644--- a/frontend/src/hooks/useWorkflowPersistence.ts+++ b/frontend/src/hooks/useWorkflowPersistence.ts@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from 'react'import { useGraphStore } from '../store/graphStore'import { materializeSampleImages } from '../workflow/materializeSampleImages'import { loadWorkflowSession, saveWorkflowSession } from '../workflow/persist'+import { getProject } from '../workflow/projectLibrary'const SAVE_DEBOUNCE_MS = 400@@ -24,8 +25,24 @@ export function useWorkflowPersistence() {if (restoredRef.current || nodeCatalog.length === 0) returnrestoredRef.current = trueconst saved = loadWorkflowSession()- if (saved && saved.graph.nodes.length > 0) {- loadWorkflow(saved)+ if (saved && saved.document.graph.nodes.length > 0) {+ const libraryProject =+ saved.activeProjectId != null ? getProject(saved.activeProjectId) : null+ const doc = libraryProject+ ? {+ ...saved.document,+ id: libraryProject.id,+ name: libraryProject.name,+ description: libraryProject.description,+ createdAt: libraryProject.createdAt,+ updatedAt: libraryProject.updatedAt,+ }+ : {+ ...saved.document,+ // File/session drafts are not library entries until Save As.+ id: undefined,+ }+ loadWorkflow(doc)void materializeSampleImages().catch(() => {// Session may already have real upload paths; ignore sample staging failures.})@@ -36,14 +53,18 @@ export function useWorkflowPersistence() {useEffect(() => {if (!sessionReady) returnconst timer = window.setTimeout(() => {- saveWorkflowSession(toWorkflowDocument())+ const doc = toWorkflowDocument()+ saveWorkflowSession(doc, doc.id ?? null)}, SAVE_DEBOUNCE_MS)return () => window.clearTimeout(timer)}, [sessionReady, nodes, edges, seed, sampleCount, toWorkflowDocument])useEffect(() => {if (!sessionReady) return- const flush = () => saveWorkflowSession(toWorkflowDocument())+ const flush = () => {+ const doc = toWorkflowDocument()+ saveWorkflowSession(doc, doc.id ?? null)+ }window.addEventListener('beforeunload', flush)return () => window.removeEventListener('beforeunload', flush)}, [sessionReady, toWorkflowDocument])diff --git a/frontend/src/workflow/persist.ts b/frontend/src/workflow/persist.tsindex bd0ec25..4e854a5 100644--- a/frontend/src/workflow/persist.ts+++ b/frontend/src/workflow/persist.ts@@ -7,19 +7,66 @@ import {export const WORKFLOW_SESSION_KEY = 'image-pipes.workflow.v1'-export function saveWorkflowSession(doc: WorkflowDocument): void {+export interface WorkflowSession {+ activeProjectId: string | null+ document: WorkflowDocument+}++function isRecord(value: unknown): value is Record<string, unknown> {+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value)+}++function readOptionalString(value: unknown): string | null {+ if (typeof value !== 'string') return null+ const trimmed = value.trim()+ return trimmed.length > 0 ? trimmed : null+}++/** Accept the new session envelope or a legacy bare WorkflowDocument. */+export function coerceWorkflowSession(value: unknown): WorkflowSession | null {+ if (!isRecord(value)) return null++ if (isRecord(value.document)) {+ try {+ return {+ activeProjectId: readOptionalString(value.activeProjectId),+ document: coerceWorkflowDocument(value.document),+ }+ } catch {+ return null+ }+ }++ try {+ return {+ activeProjectId: null,+ document: coerceWorkflowDocument(value),+ }+ } catch {+ return null+ }+}++export function saveWorkflowSession(+ doc: WorkflowDocument,+ activeProjectId: string | null = doc.id ?? null,+): void {try {- localStorage.setItem(WORKFLOW_SESSION_KEY, JSON.stringify(doc))+ const session: WorkflowSession = {+ activeProjectId,+ document: doc,+ }+ localStorage.setItem(WORKFLOW_SESSION_KEY, JSON.stringify(session))} catch {// Quota or private mode — ignore; export still works.}}-export function loadWorkflowSession(): WorkflowDocument | null {+export function loadWorkflowSession(): WorkflowSession | null {try {const raw = localStorage.getItem(WORKFLOW_SESSION_KEY)if (!raw) return null- return coerceWorkflowDocument(JSON.parse(raw) as unknown)+ return coerceWorkflowSession(JSON.parse(raw) as unknown)} catch {return null}diff --git a/frontend/src/workflow/projectLibrary.ts b/frontend/src/workflow/projectLibrary.tsnew file mode 100644index 0000000..eadd4a0--- /dev/null+++ b/frontend/src/workflow/projectLibrary.ts@@ -0,0 +1,123 @@+/** Local multi-project library backed by localStorage. */++import {+ coerceWorkflowDocument,+ DEFAULT_PROJECT_NAME,+ type WorkflowDocument,+} from './io'++export const PROJECTS_LIBRARY_KEY = 'image-pipes.projects.v1'++export type ProjectRecord = WorkflowDocument & {+ id: string+ createdAt: string+ updatedAt: string+}++interface ProjectLibraryStore {+ version: 1+ projects: ProjectRecord[]+}++function isRecord(value: unknown): value is Record<string, unknown> {+ return Boolean(value) && typeof value === 'object' && !Array.isArray(value)+}++export function createProjectId(): string {+ if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {+ return crypto.randomUUID()+ }+ return `proj-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`+}++function emptyLibrary(): ProjectLibraryStore {+ return { version: 1, projects: [] }+}++function readLibrary(): ProjectLibraryStore {+ try {+ const raw = localStorage.getItem(PROJECTS_LIBRARY_KEY)+ if (!raw) return emptyLibrary()+ const parsed = JSON.parse(raw) as unknown+ if (!isRecord(parsed) || parsed.version !== 1 || !Array.isArray(parsed.projects)) {+ return emptyLibrary()+ }+ const projects: ProjectRecord[] = []+ for (const entry of parsed.projects) {+ try {+ const doc = coerceWorkflowDocument(entry)+ if (!doc.id) continue+ const now = new Date().toISOString()+ projects.push({+ ...doc,+ id: doc.id,+ name: doc.name || DEFAULT_PROJECT_NAME,+ createdAt: doc.createdAt ?? now,+ updatedAt: doc.updatedAt ?? now,+ })+ } catch {+ // Skip corrupt entries.+ }+ }+ return { version: 1, projects }+ } catch {+ return emptyLibrary()+ }+}++function writeLibrary(store: ProjectLibraryStore): void {+ try {+ localStorage.setItem(PROJECTS_LIBRARY_KEY, JSON.stringify(store))+ } catch {+ // Quota or private mode — ignore; export still works.+ }+}++export function listProjects(): ProjectRecord[] {+ return [...readLibrary().projects].sort((a, b) =>+ b.updatedAt.localeCompare(a.updatedAt),+ )+}++export function getProject(id: string): ProjectRecord | null {+ return readLibrary().projects.find((project) => project.id === id) ?? null+}++export function upsertProject(doc: WorkflowDocument): ProjectRecord {+ const now = new Date().toISOString()+ const id = doc.id?.trim() || createProjectId()+ const store = readLibrary()+ const existing = store.projects.find((project) => project.id === id)+ const record: ProjectRecord = {+ ...doc,+ id,+ name: doc.name.trim() || DEFAULT_PROJECT_NAME,+ createdAt: existing?.createdAt ?? doc.createdAt ?? now,+ updatedAt: now,+ }+ const next = store.projects.filter((project) => project.id !== id)+ next.push(record)+ writeLibrary({ version: 1, projects: next })+ return record+}++export function deleteProject(id: string): boolean {+ const store = readLibrary()+ const next = store.projects.filter((project) => project.id !== id)+ if (next.length === store.projects.length) return false+ writeLibrary({ version: 1, projects: next })+ return true+}++export function renameProject(+ id: string,+ meta: { name: string; description?: string },+): ProjectRecord | null {+ const existing = getProject(id)+ if (!existing) return null+ return upsertProject({+ ...existing,+ name: meta.name.trim() || DEFAULT_PROJECT_NAME,+ description: meta.description?.trim() || undefined,+ })+}