use-config.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { useCallback, useState } from 'react'
  2. import produce from 'immer'
  3. import { useBoolean } from 'ahooks'
  4. import { v4 as uuid4 } from 'uuid'
  5. import type { ValueSelector, Var } from '../../types'
  6. import { VarType } from '../../types'
  7. import type { VarGroupItem, VariableAssignerNodeType } from './types'
  8. import { useGetAvailableVars } from './hooks'
  9. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  10. import {
  11. useNodesReadOnly,
  12. useWorkflow,
  13. } from '@/app/components/workflow/hooks'
  14. const useConfig = (id: string, payload: VariableAssignerNodeType) => {
  15. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  16. const { handleOutVarRenameChange, isVarUsedInNodes, removeUsedVarInNodes } = useWorkflow()
  17. const { inputs, setInputs } = useNodeCrud<VariableAssignerNodeType>(id, payload)
  18. const isEnableGroup = !!inputs.advanced_settings?.group_enabled
  19. // Not Enable Group
  20. const handleListOrTypeChange = useCallback((payload: VarGroupItem) => {
  21. setInputs({
  22. ...inputs,
  23. ...payload,
  24. })
  25. }, [inputs, setInputs])
  26. const handleListOrTypeChangeInGroup = useCallback((groupId: string) => {
  27. return (payload: VarGroupItem) => {
  28. const index = inputs.advanced_settings.groups.findIndex(item => item.groupId === groupId)
  29. const newInputs = produce(inputs, (draft) => {
  30. draft.advanced_settings.groups[index] = {
  31. ...draft.advanced_settings.groups[index],
  32. ...payload,
  33. }
  34. })
  35. setInputs(newInputs)
  36. }
  37. }, [inputs, setInputs])
  38. const getAvailableVars = useGetAvailableVars()
  39. const filterVar = (varType: VarType) => {
  40. return (v: Var) => {
  41. if (varType === VarType.any)
  42. return true
  43. if (v.type === VarType.any)
  44. return true
  45. return v.type === varType
  46. }
  47. }
  48. const [isShowRemoveVarConfirm, {
  49. setTrue: showRemoveVarConfirm,
  50. setFalse: hideRemoveVarConfirm,
  51. }] = useBoolean(false)
  52. const [removedVars, setRemovedVars] = useState<ValueSelector[]>([])
  53. const [removeType, setRemoveType] = useState<'group' | 'enableChanged'>('group')
  54. const [removedGroupIndex, setRemovedGroupIndex] = useState<number>(-1)
  55. const handleGroupRemoved = useCallback((groupId: string) => {
  56. return () => {
  57. const index = inputs.advanced_settings.groups.findIndex(item => item.groupId === groupId)
  58. if (isVarUsedInNodes([id, inputs.advanced_settings.groups[index].group_name, 'output'])) {
  59. showRemoveVarConfirm()
  60. setRemovedVars([[id, inputs.advanced_settings.groups[index].group_name, 'output']])
  61. setRemoveType('group')
  62. setRemovedGroupIndex(index)
  63. return
  64. }
  65. const newInputs = produce(inputs, (draft) => {
  66. draft.advanced_settings.groups.splice(index, 1)
  67. })
  68. setInputs(newInputs)
  69. }
  70. }, [id, inputs, isVarUsedInNodes, setInputs, showRemoveVarConfirm])
  71. const handleGroupEnabledChange = useCallback((enabled: boolean) => {
  72. const newInputs = produce(inputs, (draft) => {
  73. if (!draft.advanced_settings)
  74. draft.advanced_settings = { group_enabled: false, groups: [] }
  75. if (enabled) {
  76. if (draft.advanced_settings.groups.length === 0) {
  77. const DEFAULT_GROUP_NAME = 'Group1'
  78. draft.advanced_settings.groups = [{
  79. output_type: draft.output_type,
  80. variables: draft.variables,
  81. group_name: DEFAULT_GROUP_NAME,
  82. groupId: uuid4(),
  83. }]
  84. handleOutVarRenameChange(id, [id, 'output'], [id, DEFAULT_GROUP_NAME, 'output'])
  85. }
  86. }
  87. else {
  88. if (draft.advanced_settings.groups.length > 0) {
  89. if (draft.advanced_settings.groups.length > 1) {
  90. const useVars = draft.advanced_settings.groups.filter((item, index) => index > 0 && isVarUsedInNodes([id, item.group_name, 'output']))
  91. if (useVars.length > 0) {
  92. showRemoveVarConfirm()
  93. setRemovedVars(useVars.map(item => [id, item.group_name, 'output']))
  94. setRemoveType('enableChanged')
  95. return
  96. }
  97. }
  98. draft.output_type = draft.advanced_settings.groups[0].output_type
  99. draft.variables = draft.advanced_settings.groups[0].variables
  100. handleOutVarRenameChange(id, [id, draft.advanced_settings.groups[0].group_name, 'output'], [id, 'output'])
  101. }
  102. }
  103. draft.advanced_settings.group_enabled = enabled
  104. })
  105. setInputs(newInputs)
  106. }, [handleOutVarRenameChange, id, inputs, isVarUsedInNodes, setInputs, showRemoveVarConfirm])
  107. const handleAddGroup = useCallback(() => {
  108. let maxInGroupName = 1
  109. inputs.advanced_settings.groups.forEach((item) => {
  110. const match = item.group_name.match(/(\d+)$/)
  111. if (match) {
  112. const num = parseInt(match[1], 10)
  113. if (num > maxInGroupName)
  114. maxInGroupName = num
  115. }
  116. })
  117. const newInputs = produce(inputs, (draft) => {
  118. draft.advanced_settings.groups.push({
  119. output_type: VarType.any,
  120. variables: [],
  121. group_name: `Group${maxInGroupName + 1}`,
  122. groupId: uuid4(),
  123. })
  124. })
  125. setInputs(newInputs)
  126. }, [inputs, setInputs])
  127. const handleVarGroupNameChange = useCallback((groupId: string) => {
  128. return (name: string) => {
  129. const index = inputs.advanced_settings.groups.findIndex(item => item.groupId === groupId)
  130. const newInputs = produce(inputs, (draft) => {
  131. draft.advanced_settings.groups[index].group_name = name
  132. })
  133. handleOutVarRenameChange(id, [id, inputs.advanced_settings.groups[index].group_name, 'output'], [id, name, 'output'])
  134. setInputs(newInputs)
  135. }
  136. }, [handleOutVarRenameChange, id, inputs, setInputs])
  137. const onRemoveVarConfirm = useCallback(() => {
  138. removedVars.forEach((v) => {
  139. removeUsedVarInNodes(v)
  140. })
  141. hideRemoveVarConfirm()
  142. if (removeType === 'group') {
  143. const newInputs = produce(inputs, (draft) => {
  144. draft.advanced_settings.groups.splice(removedGroupIndex, 1)
  145. })
  146. setInputs(newInputs)
  147. }
  148. else {
  149. // removeType === 'enableChanged' to enabled
  150. const newInputs = produce(inputs, (draft) => {
  151. draft.advanced_settings.group_enabled = false
  152. draft.output_type = draft.advanced_settings.groups[0].output_type
  153. draft.variables = draft.advanced_settings.groups[0].variables
  154. })
  155. setInputs(newInputs)
  156. }
  157. }, [removedVars, hideRemoveVarConfirm, removeType, removeUsedVarInNodes, inputs, setInputs, removedGroupIndex])
  158. return {
  159. readOnly,
  160. inputs,
  161. handleListOrTypeChange,
  162. isEnableGroup,
  163. handleGroupEnabledChange,
  164. handleAddGroup,
  165. handleListOrTypeChangeInGroup,
  166. handleGroupRemoved,
  167. handleVarGroupNameChange,
  168. isShowRemoveVarConfirm,
  169. hideRemoveVarConfirm,
  170. onRemoveVarConfirm,
  171. getAvailableVars,
  172. filterVar,
  173. }
  174. }
  175. export default useConfig