use-config.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { useCallback, useState } from 'react'
  2. import produce from 'immer'
  3. import { useBoolean } from 'ahooks'
  4. import type { StartNodeType } from './types'
  5. import { ChangeType } from '@/app/components/workflow/types'
  6. import type { InputVar, MoreInfo, ValueSelector } from '@/app/components/workflow/types'
  7. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  8. import {
  9. useIsChatMode,
  10. useNodesReadOnly,
  11. useWorkflow,
  12. } from '@/app/components/workflow/hooks'
  13. const useConfig = (id: string, payload: StartNodeType) => {
  14. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  15. const { handleOutVarRenameChange, isVarUsedInNodes, removeUsedVarInNodes } = useWorkflow()
  16. const isChatMode = useIsChatMode()
  17. const { inputs, setInputs } = useNodeCrud<StartNodeType>(id, payload)
  18. const [isShowAddVarModal, {
  19. setTrue: showAddVarModal,
  20. setFalse: hideAddVarModal,
  21. }] = useBoolean(false)
  22. const [isShowRemoveVarConfirm, {
  23. setTrue: showRemoveVarConfirm,
  24. setFalse: hideRemoveVarConfirm,
  25. }] = useBoolean(false)
  26. const [removedVar, setRemovedVar] = useState<ValueSelector>([])
  27. const [removedIndex, setRemoveIndex] = useState(0)
  28. const handleVarListChange = useCallback((newList: InputVar[], moreInfo?: { index: number; payload: MoreInfo }) => {
  29. if (moreInfo?.payload?.type === ChangeType.remove) {
  30. if (isVarUsedInNodes([id, moreInfo?.payload?.payload?.beforeKey || ''])) {
  31. showRemoveVarConfirm()
  32. setRemovedVar([id, moreInfo?.payload?.payload?.beforeKey || ''])
  33. setRemoveIndex(moreInfo?.index as number)
  34. return
  35. }
  36. }
  37. const newInputs = produce(inputs, (draft: any) => {
  38. draft.variables = newList
  39. })
  40. setInputs(newInputs)
  41. if (moreInfo?.payload?.type === ChangeType.changeVarName) {
  42. const changedVar = newList[moreInfo.index]
  43. handleOutVarRenameChange(id, [id, inputs.variables[moreInfo.index].variable], [id, changedVar.variable])
  44. }
  45. }, [handleOutVarRenameChange, id, inputs, isVarUsedInNodes, setInputs, showRemoveVarConfirm])
  46. const removeVarInNode = useCallback(() => {
  47. const newInputs = produce(inputs, (draft) => {
  48. draft.variables.splice(removedIndex, 1)
  49. })
  50. setInputs(newInputs)
  51. removeUsedVarInNodes(removedVar)
  52. hideRemoveVarConfirm()
  53. }, [hideRemoveVarConfirm, inputs, removeUsedVarInNodes, removedIndex, removedVar, setInputs])
  54. const handleAddVariable = useCallback((payload: InputVar) => {
  55. const newInputs = produce(inputs, (draft: StartNodeType) => {
  56. draft.variables.push(payload)
  57. })
  58. setInputs(newInputs)
  59. }, [inputs, setInputs])
  60. return {
  61. readOnly,
  62. isChatMode,
  63. inputs,
  64. isShowAddVarModal,
  65. showAddVarModal,
  66. hideAddVarModal,
  67. handleVarListChange,
  68. handleAddVariable,
  69. isShowRemoveVarConfirm,
  70. hideRemoveVarConfirm,
  71. onRemoveVarConfirm: removeVarInNode,
  72. }
  73. }
  74. export default useConfig