use-config.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { useCallback } from 'react'
  2. import produce from 'immer'
  3. import { v4 as uuid4 } from 'uuid'
  4. import { useUpdateNodeInternals } from 'reactflow'
  5. import type {
  6. Var,
  7. } from '../../types'
  8. import { VarType } from '../../types'
  9. import { LogicalOperator } from './types'
  10. import type {
  11. CaseItem,
  12. HandleAddCondition,
  13. HandleRemoveCondition,
  14. HandleUpdateCondition,
  15. HandleUpdateConditionLogicalOperator,
  16. IfElseNodeType,
  17. } from './types'
  18. import {
  19. branchNameCorrect,
  20. getOperators,
  21. } from './utils'
  22. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  23. import {
  24. useEdgesInteractions,
  25. useNodesReadOnly,
  26. } from '@/app/components/workflow/hooks'
  27. import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list'
  28. const useConfig = (id: string, payload: IfElseNodeType) => {
  29. const updateNodeInternals = useUpdateNodeInternals()
  30. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  31. const { handleEdgeDeleteByDeleteBranch } = useEdgesInteractions()
  32. const { inputs, setInputs } = useNodeCrud<IfElseNodeType>(id, payload)
  33. const filterVar = useCallback((varPayload: Var) => {
  34. return varPayload.type !== VarType.arrayFile
  35. }, [])
  36. const {
  37. availableVars,
  38. availableNodesWithParent,
  39. } = useAvailableVarList(id, {
  40. onlyLeafNodeVar: false,
  41. filterVar,
  42. })
  43. const filterNumberVar = useCallback((varPayload: Var) => {
  44. return varPayload.type === VarType.number
  45. }, [])
  46. const {
  47. availableVars: availableNumberVars,
  48. availableNodesWithParent: availableNumberNodesWithParent,
  49. } = useAvailableVarList(id, {
  50. onlyLeafNodeVar: false,
  51. filterVar: filterNumberVar,
  52. })
  53. const handleAddCase = useCallback(() => {
  54. const newInputs = produce(inputs, () => {
  55. if (inputs.cases) {
  56. const case_id = uuid4()
  57. inputs.cases.push({
  58. case_id,
  59. logical_operator: LogicalOperator.and,
  60. conditions: [],
  61. })
  62. if (inputs._targetBranches) {
  63. const elseCaseIndex = inputs._targetBranches.findIndex(branch => branch.id === 'false')
  64. if (elseCaseIndex > -1) {
  65. inputs._targetBranches = branchNameCorrect([
  66. ...inputs._targetBranches.slice(0, elseCaseIndex),
  67. {
  68. id: case_id,
  69. name: '',
  70. },
  71. ...inputs._targetBranches.slice(elseCaseIndex),
  72. ])
  73. }
  74. }
  75. }
  76. })
  77. setInputs(newInputs)
  78. }, [inputs, setInputs])
  79. const handleRemoveCase = useCallback((caseId: string) => {
  80. const newInputs = produce(inputs, (draft) => {
  81. draft.cases = draft.cases?.filter(item => item.case_id !== caseId)
  82. if (draft._targetBranches)
  83. draft._targetBranches = branchNameCorrect(draft._targetBranches.filter(branch => branch.id !== caseId))
  84. handleEdgeDeleteByDeleteBranch(id, caseId)
  85. })
  86. setInputs(newInputs)
  87. }, [inputs, setInputs, id, handleEdgeDeleteByDeleteBranch])
  88. const handleSortCase = useCallback((newCases: (CaseItem & { id: string })[]) => {
  89. const newInputs = produce(inputs, (draft) => {
  90. draft.cases = newCases.filter(Boolean).map(item => ({
  91. id: item.id,
  92. case_id: item.case_id,
  93. logical_operator: item.logical_operator,
  94. conditions: item.conditions,
  95. }))
  96. draft._targetBranches = branchNameCorrect([
  97. ...newCases.filter(Boolean).map(item => ({ id: item.case_id, name: '' })),
  98. { id: 'false', name: '' },
  99. ])
  100. })
  101. setInputs(newInputs)
  102. updateNodeInternals(id)
  103. }, [inputs, setInputs])
  104. const handleAddCondition = useCallback<HandleAddCondition>((caseId, valueSelector, varItem) => {
  105. const newInputs = produce(inputs, (draft) => {
  106. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  107. if (targetCase) {
  108. targetCase.conditions.push({
  109. id: uuid4(),
  110. varType: varItem.type,
  111. variable_selector: valueSelector,
  112. comparison_operator: getOperators(varItem.type)[0],
  113. value: '',
  114. })
  115. }
  116. })
  117. setInputs(newInputs)
  118. }, [inputs, setInputs])
  119. const handleRemoveCondition = useCallback<HandleRemoveCondition>((caseId, conditionId) => {
  120. const newInputs = produce(inputs, (draft) => {
  121. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  122. if (targetCase)
  123. targetCase.conditions = targetCase.conditions.filter(item => item.id !== conditionId)
  124. })
  125. setInputs(newInputs)
  126. }, [inputs, setInputs])
  127. const handleUpdateCondition = useCallback<HandleUpdateCondition>((caseId, conditionId, newCondition) => {
  128. const newInputs = produce(inputs, (draft) => {
  129. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  130. if (targetCase) {
  131. const targetCondition = targetCase.conditions.find(item => item.id === conditionId)
  132. if (targetCondition)
  133. Object.assign(targetCondition, newCondition)
  134. }
  135. })
  136. setInputs(newInputs)
  137. }, [inputs, setInputs])
  138. const handleUpdateConditionLogicalOperator = useCallback<HandleUpdateConditionLogicalOperator>((caseId, value) => {
  139. const newInputs = produce(inputs, (draft) => {
  140. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  141. if (targetCase)
  142. targetCase.logical_operator = value
  143. })
  144. setInputs(newInputs)
  145. }, [inputs, setInputs])
  146. return {
  147. readOnly,
  148. inputs,
  149. filterVar,
  150. filterNumberVar,
  151. handleAddCase,
  152. handleRemoveCase,
  153. handleSortCase,
  154. handleAddCondition,
  155. handleRemoveCondition,
  156. handleUpdateCondition,
  157. handleUpdateConditionLogicalOperator,
  158. nodesOutputVars: availableVars,
  159. availableNodes: availableNodesWithParent,
  160. nodesOutputNumberVars: availableNumberVars,
  161. availableNumberNodes: availableNumberNodesWithParent,
  162. }
  163. }
  164. export default useConfig