use-config.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import { useCallback, useMemo } 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. HandleAddSubVariableCondition,
  14. HandleRemoveCondition,
  15. HandleToggleConditionLogicalOperator,
  16. HandleToggleSubVariableConditionLogicalOperator,
  17. HandleUpdateCondition,
  18. HandleUpdateSubVariableCondition,
  19. IfElseNodeType,
  20. } from './types'
  21. import {
  22. branchNameCorrect,
  23. getOperators,
  24. } from './utils'
  25. import useIsVarFileAttribute from './use-is-var-file-attribute'
  26. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  27. import {
  28. useEdgesInteractions,
  29. useNodesReadOnly,
  30. } from '@/app/components/workflow/hooks'
  31. import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list'
  32. const useConfig = (id: string, payload: IfElseNodeType) => {
  33. const updateNodeInternals = useUpdateNodeInternals()
  34. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  35. const { handleEdgeDeleteByDeleteBranch } = useEdgesInteractions()
  36. const { inputs, setInputs } = useNodeCrud<IfElseNodeType>(id, payload)
  37. const filterVar = useCallback(() => {
  38. return true
  39. }, [])
  40. const {
  41. availableVars,
  42. availableNodesWithParent,
  43. } = useAvailableVarList(id, {
  44. onlyLeafNodeVar: false,
  45. filterVar,
  46. })
  47. const filterNumberVar = useCallback((varPayload: Var) => {
  48. return varPayload.type === VarType.number
  49. }, [])
  50. const {
  51. getIsVarFileAttribute,
  52. } = useIsVarFileAttribute({
  53. nodeId: id,
  54. isInIteration: payload.isInIteration,
  55. })
  56. const varsIsVarFileAttribute = useMemo(() => {
  57. const conditions: Record<string, boolean> = {}
  58. inputs.cases?.forEach((c) => {
  59. c.conditions.forEach((condition) => {
  60. conditions[condition.id] = getIsVarFileAttribute(condition.variable_selector!)
  61. })
  62. })
  63. return conditions
  64. }, [inputs.cases, getIsVarFileAttribute])
  65. const {
  66. availableVars: availableNumberVars,
  67. availableNodesWithParent: availableNumberNodesWithParent,
  68. } = useAvailableVarList(id, {
  69. onlyLeafNodeVar: false,
  70. filterVar: filterNumberVar,
  71. })
  72. const handleAddCase = useCallback(() => {
  73. const newInputs = produce(inputs, (draft) => {
  74. if (draft.cases) {
  75. const case_id = uuid4()
  76. draft.cases.push({
  77. case_id,
  78. logical_operator: LogicalOperator.and,
  79. conditions: [],
  80. })
  81. if (draft._targetBranches) {
  82. const elseCaseIndex = draft._targetBranches.findIndex(branch => branch.id === 'false')
  83. if (elseCaseIndex > -1) {
  84. draft._targetBranches = branchNameCorrect([
  85. ...draft._targetBranches.slice(0, elseCaseIndex),
  86. {
  87. id: case_id,
  88. name: '',
  89. },
  90. ...draft._targetBranches.slice(elseCaseIndex),
  91. ])
  92. }
  93. }
  94. }
  95. })
  96. setInputs(newInputs)
  97. }, [inputs, setInputs])
  98. const handleRemoveCase = useCallback((caseId: string) => {
  99. const newInputs = produce(inputs, (draft) => {
  100. draft.cases = draft.cases?.filter(item => item.case_id !== caseId)
  101. if (draft._targetBranches)
  102. draft._targetBranches = branchNameCorrect(draft._targetBranches.filter(branch => branch.id !== caseId))
  103. handleEdgeDeleteByDeleteBranch(id, caseId)
  104. })
  105. setInputs(newInputs)
  106. }, [inputs, setInputs, id, handleEdgeDeleteByDeleteBranch])
  107. const handleSortCase = useCallback((newCases: (CaseItem & { id: string })[]) => {
  108. const newInputs = produce(inputs, (draft) => {
  109. draft.cases = newCases.filter(Boolean).map(item => ({
  110. id: item.id,
  111. case_id: item.case_id,
  112. logical_operator: item.logical_operator,
  113. conditions: item.conditions,
  114. }))
  115. draft._targetBranches = branchNameCorrect([
  116. ...newCases.filter(Boolean).map(item => ({ id: item.case_id, name: '' })),
  117. { id: 'false', name: '' },
  118. ])
  119. })
  120. setInputs(newInputs)
  121. updateNodeInternals(id)
  122. }, [inputs, setInputs])
  123. const handleAddCondition = useCallback<HandleAddCondition>((caseId, valueSelector, varItem) => {
  124. const newInputs = produce(inputs, (draft) => {
  125. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  126. if (targetCase) {
  127. targetCase.conditions.push({
  128. id: uuid4(),
  129. varType: varItem.type,
  130. variable_selector: valueSelector,
  131. comparison_operator: getOperators(varItem.type, getIsVarFileAttribute(valueSelector) ? { key: valueSelector.slice(-1)[0] } : undefined)[0],
  132. value: '',
  133. })
  134. }
  135. })
  136. setInputs(newInputs)
  137. }, [getIsVarFileAttribute, inputs, setInputs])
  138. const handleRemoveCondition = useCallback<HandleRemoveCondition>((caseId, conditionId) => {
  139. const newInputs = produce(inputs, (draft) => {
  140. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  141. if (targetCase)
  142. targetCase.conditions = targetCase.conditions.filter(item => item.id !== conditionId)
  143. })
  144. setInputs(newInputs)
  145. }, [inputs, setInputs])
  146. const handleUpdateCondition = useCallback<HandleUpdateCondition>((caseId, conditionId, newCondition) => {
  147. const newInputs = produce(inputs, (draft) => {
  148. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  149. if (targetCase) {
  150. const targetCondition = targetCase.conditions.find(item => item.id === conditionId)
  151. if (targetCondition)
  152. Object.assign(targetCondition, newCondition)
  153. }
  154. })
  155. setInputs(newInputs)
  156. }, [inputs, setInputs])
  157. const handleToggleConditionLogicalOperator = useCallback<HandleToggleConditionLogicalOperator>((caseId) => {
  158. const newInputs = produce(inputs, (draft) => {
  159. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  160. if (targetCase)
  161. targetCase.logical_operator = targetCase.logical_operator === LogicalOperator.and ? LogicalOperator.or : LogicalOperator.and
  162. })
  163. setInputs(newInputs)
  164. }, [inputs, setInputs])
  165. const handleAddSubVariableCondition = useCallback<HandleAddSubVariableCondition>((caseId: string, conditionId: string, key?: string) => {
  166. const newInputs = produce(inputs, (draft) => {
  167. const condition = draft.cases?.find(item => item.case_id === caseId)?.conditions.find(item => item.id === conditionId)
  168. if (!condition)
  169. return
  170. if (!condition?.sub_variable_condition) {
  171. condition.sub_variable_condition = {
  172. case_id: uuid4(),
  173. logical_operator: LogicalOperator.and,
  174. conditions: [],
  175. }
  176. }
  177. const subVarCondition = condition.sub_variable_condition
  178. if (subVarCondition) {
  179. if (!subVarCondition.conditions)
  180. subVarCondition.conditions = []
  181. subVarCondition.conditions.push({
  182. id: uuid4(),
  183. key: key || '',
  184. varType: VarType.string,
  185. comparison_operator: undefined,
  186. value: '',
  187. })
  188. }
  189. })
  190. setInputs(newInputs)
  191. }, [inputs, setInputs])
  192. const handleRemoveSubVariableCondition = useCallback((caseId: string, conditionId: string, subConditionId: string) => {
  193. const newInputs = produce(inputs, (draft) => {
  194. const condition = draft.cases?.find(item => item.case_id === caseId)?.conditions.find(item => item.id === conditionId)
  195. if (!condition)
  196. return
  197. if (!condition?.sub_variable_condition)
  198. return
  199. const subVarCondition = condition.sub_variable_condition
  200. if (subVarCondition)
  201. subVarCondition.conditions = subVarCondition.conditions.filter(item => item.id !== subConditionId)
  202. })
  203. setInputs(newInputs)
  204. }, [inputs, setInputs])
  205. const handleUpdateSubVariableCondition = useCallback<HandleUpdateSubVariableCondition>((caseId, conditionId, subConditionId, newSubCondition) => {
  206. const newInputs = produce(inputs, (draft) => {
  207. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  208. if (targetCase) {
  209. const targetCondition = targetCase.conditions.find(item => item.id === conditionId)
  210. if (targetCondition && targetCondition.sub_variable_condition) {
  211. const targetSubCondition = targetCondition.sub_variable_condition.conditions.find(item => item.id === subConditionId)
  212. if (targetSubCondition)
  213. Object.assign(targetSubCondition, newSubCondition)
  214. }
  215. }
  216. })
  217. setInputs(newInputs)
  218. }, [inputs, setInputs])
  219. const handleToggleSubVariableConditionLogicalOperator = useCallback<HandleToggleSubVariableConditionLogicalOperator>((caseId, conditionId) => {
  220. const newInputs = produce(inputs, (draft) => {
  221. const targetCase = draft.cases?.find(item => item.case_id === caseId)
  222. if (targetCase) {
  223. const targetCondition = targetCase.conditions.find(item => item.id === conditionId)
  224. if (targetCondition && targetCondition.sub_variable_condition)
  225. targetCondition.sub_variable_condition.logical_operator = targetCondition.sub_variable_condition.logical_operator === LogicalOperator.and ? LogicalOperator.or : LogicalOperator.and
  226. }
  227. })
  228. setInputs(newInputs)
  229. }, [inputs, setInputs])
  230. return {
  231. readOnly,
  232. inputs,
  233. filterVar,
  234. filterNumberVar,
  235. handleAddCase,
  236. handleRemoveCase,
  237. handleSortCase,
  238. handleAddCondition,
  239. handleRemoveCondition,
  240. handleUpdateCondition,
  241. handleToggleConditionLogicalOperator,
  242. handleAddSubVariableCondition,
  243. handleUpdateSubVariableCondition,
  244. handleRemoveSubVariableCondition,
  245. handleToggleSubVariableConditionLogicalOperator,
  246. nodesOutputVars: availableVars,
  247. availableNodes: availableNodesWithParent,
  248. nodesOutputNumberVars: availableNumberVars,
  249. availableNumberNodes: availableNumberNodesWithParent,
  250. varsIsVarFileAttribute,
  251. }
  252. }
  253. export default useConfig