您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

use-config.ts 7.6KB

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