/** * Automation2 Flow Editor - Data flow context for Data Picker and DynamicValueField. */ import React, { createContext, useContext, useMemo } from 'react'; import type { CanvasNode, CanvasConnection } from '../editor/FlowCanvas'; import { getAvailableSources } from '../nodes/shared/dataFlowGraph'; import type { NodeType } from '../../../api/automation2Api'; export interface Automation2DataFlowContextValue { currentNodeId: string; nodes: CanvasNode[]; connections: CanvasConnection[]; nodeOutputsPreview: Record; nodeTypes: NodeType[]; language: string; getNodeLabel: (node: { id: string; title?: string; label?: string; type?: string }) => string; getAvailableSourceIds: () => string[]; } const Automation2DataFlowContext = createContext(null); export function useAutomation2DataFlow(): Automation2DataFlowContextValue | null { return useContext(Automation2DataFlowContext); } interface Automation2DataFlowProviderProps { node: CanvasNode | null; nodes: CanvasNode[]; connections: CanvasConnection[]; nodeOutputsPreview: Record; nodeTypes: NodeType[]; language: string; children: React.ReactNode; } export const Automation2DataFlowProvider: React.FC = ({ node, nodes, connections, nodeOutputsPreview, nodeTypes, language, children, }) => { const value = useMemo((): Automation2DataFlowContextValue | null => { if (!node) return null; return { currentNodeId: node.id, nodes, connections, nodeOutputsPreview, nodeTypes, language, getNodeLabel: (n: { id: string; title?: string; label?: string; type?: string }) => n.title ?? n.label ?? n.type ?? n.id, getAvailableSourceIds: () => getAvailableSources(node.id, nodes, connections), }; }, [node?.id, nodes, connections, nodeOutputsPreview, nodeTypes, language]); return ( {children} ); };