use-workflow-interactions.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. })
  23. handleNodeCancelRunningStatus()
  24. handleEdgeCancelRunningStatus()
  25. }, [workflowStore, handleNodeCancelRunningStatus, handleEdgeCancelRunningStatus])
  26. const handleUpdateWorkflowCanvas = useCallback((payload: WorkflowDataUpdator) => {
  27. const {
  28. nodes,
  29. edges,
  30. viewport,
  31. } = payload
  32. const { setViewport } = reactflow
  33. eventEmitter?.emit({
  34. type: WORKFLOW_DATA_UPDATE,
  35. payload: {
  36. nodes: initialNodes(nodes, edges),
  37. edges: initialEdges(edges, nodes),
  38. },
  39. } as any)
  40. setViewport(viewport)
  41. }, [eventEmitter, reactflow])
  42. return {
  43. handleCancelDebugAndPreviewPanel,
  44. handleUpdateWorkflowCanvas,
  45. }
  46. }