import type { Node, Edge } from '@xyflow/react'; import type { DiagramDocument, DiagramEdge, DiagramNode, NodeKind } from './types'; /** * React Flow's runtime Node/Edge carry UI-only fields (measured, selected, * dragging, zIndex, ...). Only this subset gets persisted. */ export function serializeDocument(nodes: Node[], edges: Edge[]): DiagramDocument { return { nodes: nodes.map( (node): DiagramNode => ({ id: node.id, type: (node.type ?? 'task') as NodeKind, position: { x: node.position.x, y: node.position.y }, data: node.data as DiagramNode['data'], }), ), edges: edges.map( (edge): DiagramEdge => ({ id: edge.id, source: edge.source, target: edge.target, type: edge.type, data: edge.data as DiagramEdge['data'], }), ), }; } export function debounce(fn: (...args: Args) => void, delayMs: number) { let timer: ReturnType | undefined; return (...args: Args) => { if (timer) clearTimeout(timer); timer = setTimeout(() => fn(...args), delayMs); }; }