use-config.ts 9.8 KB

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