You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

use-nodes-interactions-without-sync.ts 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import { useStoreApi } from 'reactflow'
  4. import { NodeRunningStatus } from '../types'
  5. export const useNodesInteractionsWithoutSync = () => {
  6. const store = useStoreApi()
  7. const handleNodeCancelRunningStatus = useCallback(() => {
  8. const {
  9. getNodes,
  10. setNodes,
  11. } = store.getState()
  12. const nodes = getNodes()
  13. const newNodes = produce(nodes, (draft) => {
  14. draft.forEach((node) => {
  15. node.data._runningStatus = undefined
  16. node.data._waitingRun = false
  17. })
  18. })
  19. setNodes(newNodes)
  20. }, [store])
  21. const handleCancelAllNodeSuccessStatus = useCallback(() => {
  22. const {
  23. getNodes,
  24. setNodes,
  25. } = store.getState()
  26. const nodes = getNodes()
  27. const newNodes = produce(nodes, (draft) => {
  28. draft.forEach((node) => {
  29. if(node.data._runningStatus === NodeRunningStatus.Succeeded)
  30. node.data._runningStatus = undefined
  31. })
  32. })
  33. setNodes(newNodes)
  34. }, [store])
  35. const handleCancelNodeSuccessStatus = useCallback((nodeId: string) => {
  36. const {
  37. getNodes,
  38. setNodes,
  39. } = store.getState()
  40. const newNodes = produce(getNodes(), (draft) => {
  41. const node = draft.find(n => n.id === nodeId)
  42. if (node && node.data._runningStatus === NodeRunningStatus.Succeeded) {
  43. node.data._runningStatus = undefined
  44. node.data._waitingRun = false
  45. }
  46. })
  47. setNodes(newNodes)
  48. }, [store])
  49. return {
  50. handleNodeCancelRunningStatus,
  51. handleCancelAllNodeSuccessStatus,
  52. handleCancelNodeSuccessStatus,
  53. }
  54. }