use-config.ts 3.7 KB

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