| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import { useCallback } from 'react' | 
					
						
							|  |  |  | import produce from 'immer' | 
					
						
							|  |  |  | import { useStoreApi } from 'reactflow' | 
					
						
							|  |  |  | import { useNodesSyncDraft } from './use-nodes-sync-draft' | 
					
						
							|  |  |  | import { useNodesReadOnly } from './use-workflow' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type NodeDataUpdatePayload = { | 
					
						
							|  |  |  |   id: string | 
					
						
							|  |  |  |   data: Record<string, any> | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useNodeDataUpdate = () => { | 
					
						
							|  |  |  |   const store = useStoreApi() | 
					
						
							|  |  |  |   const { handleSyncWorkflowDraft } = useNodesSyncDraft() | 
					
						
							|  |  |  |   const { getNodesReadOnly } = useNodesReadOnly() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleNodeDataUpdate = useCallback(({ id, data }: NodeDataUpdatePayload) => { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       getNodes, | 
					
						
							|  |  |  |       setNodes, | 
					
						
							|  |  |  |     } = store.getState() | 
					
						
							|  |  |  |     const newNodes = produce(getNodes(), (draft) => { | 
					
						
							|  |  |  |       const currentNode = draft.find(node => node.id === id)! | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-06-14 17:08:11 +08:00
										 |  |  |       if (currentNode) | 
					
						
							|  |  |  |         currentNode.data = { ...currentNode.data, ...data } | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     }) | 
					
						
							|  |  |  |     setNodes(newNodes) | 
					
						
							|  |  |  |   }, [store]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleNodeDataUpdateWithSyncDraft = useCallback((payload: NodeDataUpdatePayload) => { | 
					
						
							|  |  |  |     if (getNodesReadOnly()) | 
					
						
							|  |  |  |       return | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     handleNodeDataUpdate(payload) | 
					
						
							|  |  |  |     handleSyncWorkflowDraft() | 
					
						
							|  |  |  |   }, [handleSyncWorkflowDraft, handleNodeDataUpdate, getNodesReadOnly]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     handleNodeDataUpdate, | 
					
						
							|  |  |  |     handleNodeDataUpdateWithSyncDraft, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |