use-workflow-node-iteration-finished.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { useCallback } from 'react'
  2. import { useStoreApi } from 'reactflow'
  3. import produce from 'immer'
  4. import type { IterationFinishedResponse } from '@/types/workflow'
  5. import { useWorkflowStore } from '@/app/components/workflow/store'
  6. import { DEFAULT_ITER_TIMES } from '@/app/components/workflow/constants'
  7. export const useWorkflowNodeIterationFinished = () => {
  8. const store = useStoreApi()
  9. const workflowStore = useWorkflowStore()
  10. const handleWorkflowNodeIterationFinished = useCallback((params: IterationFinishedResponse) => {
  11. const { data } = params
  12. const {
  13. workflowRunningData,
  14. setWorkflowRunningData,
  15. setIterTimes,
  16. } = workflowStore.getState()
  17. const {
  18. getNodes,
  19. setNodes,
  20. } = store.getState()
  21. const nodes = getNodes()
  22. setWorkflowRunningData(produce(workflowRunningData!, (draft) => {
  23. const currentIndex = draft.tracing!.findIndex(item => item.id === data.id)
  24. if (currentIndex > -1) {
  25. draft.tracing![currentIndex] = {
  26. ...draft.tracing![currentIndex],
  27. ...data,
  28. }
  29. }
  30. }))
  31. setIterTimes(DEFAULT_ITER_TIMES)
  32. const newNodes = produce(nodes, (draft) => {
  33. const currentNode = draft.find(node => node.id === data.node_id)!
  34. currentNode.data._runningStatus = data.status
  35. })
  36. setNodes(newNodes)
  37. }, [workflowStore, store])
  38. return {
  39. handleWorkflowNodeIterationFinished,
  40. }
  41. }