/** * Node lifecycle: nodes report error state so the graph can show the right * connection status (edge colors). "Updating" is time-bound from triggers, not per-node. * * Priority for edge status: error > updating (pulse) > default. */ import { useEffect, useRef } from 'react' import { dispatchCanvasCommand } from '@/app/canvas/canvasStore' export type NodeLifecyclePhase = 'idle' | 'trigger' | 'on-path' | 'error' /** * State that drives connection status for this node. * Only error is synced to the path; updating is shown via time-bound pulse from triggers. */ export type NodeConnectionStatusState = { /** Ignored for path; kept for API compatibility (e.g. loading spinner). */ updating?: boolean /** Node has an error. Incoming edges to this node show red. */ error?: boolean } /** * Syncs this node's error state to the canvas store so connection status (edge colors) * is correct. Call once per node: useSyncConnectionStatus(id, { error: !!error }). */ export function useSyncConnectionStatus( nodeId: string, state: NodeConnectionStatusState ): void { const { error } = state const prevRef = useRef(false) useEffect(() => { const nowError = Boolean(error) if (prevRef.current !== nowError) { dispatchCanvasCommand({ type: 'path/setError', payload: { nodeId, error: nowError } }) prevRef.current = nowError } return () => { if (prevRef.current) { dispatchCanvasCommand({ type: 'path/setError', payload: { nodeId, error: false } }) prevRef.current = false } } }, [nodeId, error]) }