// External Dependencies import dotProp from 'dot-prop'; import { useCallback } from 'react'; // Signal Management and Utilities import { toXYFlow } from '../utilities/workflow'; /** * Custom hook to manage flow nodes state and operations * Handles YAML parsing, node changes, and updates * * @param {string} value - YAML string containing node data * @param {Function} setNodes - Callback for node changes * @param {Function} updateNodeInternals - ReactFlow node update function * @returns {Object} Node management utilities */ const useFlowNodes = (nodes = [], setNodes, updateNodeInternals) => { // Handler for node changes const onNodeChange = useCallback((id, key, value) => { // Find the node being modified const actualNode = (nodes || []).find((n) => n.id === id); if (!actualNode) return; // Update the node's property using dot notation dotProp.set(actualNode, key, value); // Convert all nodes to ReactFlow format and update setNodes((nodes) => { // check found const found = nodes.find((n) => n.id === id); // if not found if (!found) { // return nodes return [ ...nodes, toXYFlow(actualNode, onNodeChange, updateNodeInternals) ]; } // replace nodes return nodes.map((node) => { if (node.id === id) { return toXYFlow(actualNode, onNodeChange, updateNodeInternals); } return node; }); }); }, [nodes, setNodes, updateNodeInternals]); return { onNodeChange }; }; /** * Custom hook to manage flow nodes state and operations */ export default useFlowNodes;