| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  | import { useCallback } from 'react' | 
					
						
							|  |  |  | import { useReactFlow } from 'reactflow' | 
					
						
							|  |  |  | import { useWorkflowStore } from '../store' | 
					
						
							|  |  |  | import { WORKFLOW_DATA_UPDATE } from '../constants' | 
					
						
							|  |  |  | import type { WorkflowDataUpdator } from '../types' | 
					
						
							|  |  |  | import { | 
					
						
							|  |  |  |   initialEdges, | 
					
						
							|  |  |  |   initialNodes, | 
					
						
							|  |  |  | } from '../utils' | 
					
						
							|  |  |  | import { useEdgesInteractions } from './use-edges-interactions' | 
					
						
							|  |  |  | import { useNodesInteractions } from './use-nodes-interactions' | 
					
						
							|  |  |  | import { useEventEmitterContextContext } from '@/context/event-emitter' | 
					
						
							| 
									
										
										
										
											2024-05-10 14:48:20 +08:00
										 |  |  | import { fetchWorkflowDraft } from '@/service/workflow' | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | export const useWorkflowInteractions = () => { | 
					
						
							|  |  |  |   const workflowStore = useWorkflowStore() | 
					
						
							|  |  |  |   const { handleNodeCancelRunningStatus } = useNodesInteractions() | 
					
						
							|  |  |  |   const { handleEdgeCancelRunningStatus } = useEdgesInteractions() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const handleCancelDebugAndPreviewPanel = useCallback(() => { | 
					
						
							|  |  |  |     workflowStore.setState({ | 
					
						
							|  |  |  |       showDebugAndPreviewPanel: false, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  |       workflowRunningData: undefined, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |     }) | 
					
						
							|  |  |  |     handleNodeCancelRunningStatus() | 
					
						
							|  |  |  |     handleEdgeCancelRunningStatus() | 
					
						
							|  |  |  |   }, [workflowStore, handleNodeCancelRunningStatus, handleEdgeCancelRunningStatus]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 14:48:20 +08:00
										 |  |  |   return { | 
					
						
							|  |  |  |     handleCancelDebugAndPreviewPanel, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useWorkflowUpdate = () => { | 
					
						
							|  |  |  |   const reactflow = useReactFlow() | 
					
						
							|  |  |  |   const workflowStore = useWorkflowStore() | 
					
						
							|  |  |  |   const { eventEmitter } = useEventEmitterContextContext() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   const handleUpdateWorkflowCanvas = useCallback((payload: WorkflowDataUpdator) => { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       nodes, | 
					
						
							|  |  |  |       edges, | 
					
						
							|  |  |  |       viewport, | 
					
						
							|  |  |  |     } = payload | 
					
						
							|  |  |  |     const { setViewport } = reactflow | 
					
						
							|  |  |  |     eventEmitter?.emit({ | 
					
						
							|  |  |  |       type: WORKFLOW_DATA_UPDATE, | 
					
						
							|  |  |  |       payload: { | 
					
						
							|  |  |  |         nodes: initialNodes(nodes, edges), | 
					
						
							|  |  |  |         edges: initialEdges(edges, nodes), | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     } as any) | 
					
						
							|  |  |  |     setViewport(viewport) | 
					
						
							|  |  |  |   }, [eventEmitter, reactflow]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 14:48:20 +08:00
										 |  |  |   const handleRefreshWorkflowDraft = useCallback(() => { | 
					
						
							|  |  |  |     const { | 
					
						
							|  |  |  |       appId, | 
					
						
							|  |  |  |       setSyncWorkflowDraftHash, | 
					
						
							|  |  |  |     } = workflowStore.getState() | 
					
						
							|  |  |  |     fetchWorkflowDraft(`/apps/${appId}/workflows/draft`).then((response) => { | 
					
						
							|  |  |  |       handleUpdateWorkflowCanvas(response.graph as WorkflowDataUpdator) | 
					
						
							|  |  |  |       setSyncWorkflowDraftHash(response.hash) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   }, [handleUpdateWorkflowCanvas, workflowStore]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   return { | 
					
						
							|  |  |  |     handleUpdateWorkflowCanvas, | 
					
						
							| 
									
										
										
										
											2024-05-10 14:48:20 +08:00
										 |  |  |     handleRefreshWorkflowDraft, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:09:56 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | } |