use-config.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { useCallback, useEffect, useRef } from 'react'
  2. import produce from 'immer'
  3. import useVarList from '../_base/hooks/use-var-list'
  4. import type { Var, Variable } from '../../types'
  5. import { VarType } from '../../types'
  6. import { useStore } from '../../store'
  7. import type { TemplateTransformNodeType } from './types'
  8. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  9. import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
  10. import {
  11. useNodesReadOnly,
  12. } from '@/app/components/workflow/hooks'
  13. import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list'
  14. const useConfig = (id: string, payload: TemplateTransformNodeType) => {
  15. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  16. const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
  17. const { inputs, setInputs: doSetInputs } = useNodeCrud<TemplateTransformNodeType>(id, payload)
  18. const inputsRef = useRef(inputs)
  19. const setInputs = useCallback((newPayload: TemplateTransformNodeType) => {
  20. doSetInputs(newPayload)
  21. inputsRef.current = newPayload
  22. }, [doSetInputs])
  23. const { availableVars } = useAvailableVarList(id, {
  24. onlyLeafNodeVar: false,
  25. filterVar: () => true,
  26. })
  27. const { handleAddVariable: handleAddEmptyVariable } = useVarList<TemplateTransformNodeType>({
  28. inputs,
  29. setInputs,
  30. })
  31. const handleVarListChange = useCallback((newList: Variable[]) => {
  32. const newInputs = produce(inputsRef.current, (draft: any) => {
  33. draft.variables = newList
  34. })
  35. setInputs(newInputs)
  36. }, [setInputs])
  37. const handleAddVariable = useCallback((payload: Variable) => {
  38. const newInputs = produce(inputsRef.current, (draft: any) => {
  39. draft.variables.push(payload)
  40. })
  41. setInputs(newInputs)
  42. }, [setInputs])
  43. // rename var in code
  44. const handleVarNameChange = useCallback((oldName: string, newName: string) => {
  45. const newInputs = produce(inputsRef.current, (draft: any) => {
  46. draft.template = draft.template.replaceAll(`{{ ${oldName} }}`, `{{ ${newName} }}`)
  47. })
  48. setInputs(newInputs)
  49. }, [setInputs])
  50. useEffect(() => {
  51. if (inputs.template)
  52. return
  53. const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
  54. if (isReady) {
  55. setInputs({
  56. ...inputs,
  57. ...defaultConfig,
  58. })
  59. }
  60. // eslint-disable-next-line react-hooks/exhaustive-deps
  61. }, [defaultConfig])
  62. const handleCodeChange = useCallback((template: string) => {
  63. const newInputs = produce(inputsRef.current, (draft: any) => {
  64. draft.template = template
  65. })
  66. setInputs(newInputs)
  67. }, [setInputs])
  68. // single run
  69. const {
  70. isShowSingleRun,
  71. hideSingleRun,
  72. toVarInputs,
  73. runningStatus,
  74. handleRun,
  75. handleStop,
  76. runInputData,
  77. setRunInputData,
  78. runResult,
  79. } = useOneStepRun<TemplateTransformNodeType>({
  80. id,
  81. data: inputs,
  82. defaultRunInputData: {},
  83. })
  84. const varInputs = toVarInputs(inputs.variables)
  85. const inputVarValues = (() => {
  86. const vars: Record<string, any> = {}
  87. Object.keys(runInputData)
  88. .forEach((key) => {
  89. vars[key] = runInputData[key]
  90. })
  91. return vars
  92. })()
  93. const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
  94. setRunInputData(newPayload)
  95. }, [setRunInputData])
  96. const filterVar = useCallback((varPayload: Var) => {
  97. return [VarType.string, VarType.number, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject].includes(varPayload.type)
  98. }, [])
  99. return {
  100. readOnly,
  101. inputs,
  102. availableVars,
  103. handleVarListChange,
  104. handleVarNameChange,
  105. handleAddVariable,
  106. handleAddEmptyVariable,
  107. handleCodeChange,
  108. filterVar,
  109. // single run
  110. isShowSingleRun,
  111. hideSingleRun,
  112. runningStatus,
  113. handleRun,
  114. handleStop,
  115. varInputs,
  116. inputVarValues,
  117. setInputVarValues,
  118. runResult,
  119. }
  120. }
  121. export default useConfig