use-config.ts 5.6 KB

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