use-workflow-interactions.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { useCallback } from 'react'
  2. import { useReactFlow } from 'reactflow'
  3. import { useWorkflowStore } from '../store'
  4. import { WORKFLOW_DATA_UPDATE } from '../constants'
  5. import type { WorkflowDataUpdator } from '../types'
  6. import {
  7. initialEdges,
  8. initialNodes,
  9. } from '../utils'
  10. import { useEdgesInteractions } from './use-edges-interactions'
  11. import { useNodesInteractions } from './use-nodes-interactions'
  12. import { useEventEmitterContextContext } from '@/context/event-emitter'
  13. export const useWorkflowInteractions = () => {
  14. const reactflow = useReactFlow()
  15. const workflowStore = useWorkflowStore()
  16. const { handleNodeCancelRunningStatus } = useNodesInteractions()
  17. const { handleEdgeCancelRunningStatus } = useEdgesInteractions()
  18. const { eventEmitter } = useEventEmitterContextContext()
  19. const handleCancelDebugAndPreviewPanel = useCallback(() => {
  20. workflowStore.setState({
  21. showDebugAndPreviewPanel: false,
  22. workflowRunningData: undefined,
  23. })
  24. handleNodeCancelRunningStatus()
  25. handleEdgeCancelRunningStatus()
  26. }, [workflowStore, handleNodeCancelRunningStatus, handleEdgeCancelRunningStatus])
  27. const handleUpdateWorkflowCanvas = useCallback((payload: WorkflowDataUpdator) => {
  28. const {
  29. nodes,
  30. edges,
  31. viewport,
  32. } = payload
  33. const { setViewport } = reactflow
  34. eventEmitter?.emit({
  35. type: WORKFLOW_DATA_UPDATE,
  36. payload: {
  37. nodes: initialNodes(nodes, edges),
  38. edges: initialEdges(edges, nodes),
  39. },
  40. } as any)
  41. setViewport(viewport)
  42. }, [eventEmitter, reactflow])
  43. return {
  44. handleCancelDebugAndPreviewPanel,
  45. handleUpdateWorkflowCanvas,
  46. }
  47. }