use-config.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. import { useCallback, useEffect, useState } from 'react'
  2. import produce from 'immer'
  3. import useVarList from '../_base/hooks/use-var-list'
  4. import useOutputVarList from '../_base/hooks/use-output-var-list'
  5. import { BlockEnum, VarType } from '../../types'
  6. import type { Var, Variable } from '../../types'
  7. import { useStore } from '../../store'
  8. import type { CodeNodeType, OutputVar } from './types'
  9. import { CodeLanguage } from './types'
  10. import useNodeCrud from '@/app/components/workflow/nodes/_base/hooks/use-node-crud'
  11. import useOneStepRun from '@/app/components/workflow/nodes/_base/hooks/use-one-step-run'
  12. import { fetchNodeDefault } from '@/service/workflow'
  13. import { useStore as useAppStore } from '@/app/components/app/store'
  14. import {
  15. useNodesReadOnly,
  16. } from '@/app/components/workflow/hooks'
  17. const useConfig = (id: string, payload: CodeNodeType) => {
  18. const { nodesReadOnly: readOnly } = useNodesReadOnly()
  19. const appId = useAppStore.getState().appDetail?.id
  20. const [allLanguageDefault, setAllLanguageDefault] = useState<Record<CodeLanguage, CodeNodeType> | null>(null)
  21. useEffect(() => {
  22. if (appId) {
  23. (async () => {
  24. const { config: javaScriptConfig } = await fetchNodeDefault(appId, BlockEnum.Code, { code_language: CodeLanguage.javascript }) as any
  25. const { config: pythonConfig } = await fetchNodeDefault(appId, BlockEnum.Code, { code_language: CodeLanguage.python3 }) as any
  26. setAllLanguageDefault({
  27. [CodeLanguage.javascript]: javaScriptConfig as CodeNodeType,
  28. [CodeLanguage.python3]: pythonConfig as CodeNodeType,
  29. } as any)
  30. })()
  31. }
  32. }, [appId])
  33. const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
  34. const { inputs, setInputs } = useNodeCrud<CodeNodeType>(id, payload)
  35. const { handleVarListChange, handleAddVariable } = useVarList<CodeNodeType>({
  36. inputs,
  37. setInputs,
  38. })
  39. const [outputKeyOrders, setOutputKeyOrders] = useState<string[]>([])
  40. const syncOutputKeyOrders = useCallback((outputs: OutputVar) => {
  41. setOutputKeyOrders(Object.keys(outputs))
  42. }, [])
  43. useEffect(() => {
  44. if (inputs.code) {
  45. if (inputs.outputs && Object.keys(inputs.outputs).length > 0)
  46. syncOutputKeyOrders(inputs.outputs)
  47. return
  48. }
  49. const isReady = defaultConfig && Object.keys(defaultConfig).length > 0
  50. if (isReady) {
  51. setInputs({
  52. ...inputs,
  53. ...defaultConfig,
  54. })
  55. syncOutputKeyOrders(defaultConfig.outputs)
  56. }
  57. // eslint-disable-next-line react-hooks/exhaustive-deps
  58. }, [defaultConfig])
  59. const handleCodeChange = useCallback((code: string) => {
  60. const newInputs = produce(inputs, (draft) => {
  61. draft.code = code
  62. })
  63. setInputs(newInputs)
  64. }, [inputs, setInputs])
  65. const handleCodeLanguageChange = useCallback((codeLanguage: CodeLanguage) => {
  66. const currDefaultConfig = allLanguageDefault?.[codeLanguage]
  67. const newInputs = produce(inputs, (draft) => {
  68. draft.code_language = codeLanguage
  69. if (!currDefaultConfig)
  70. return
  71. draft.code = currDefaultConfig.code
  72. draft.variables = currDefaultConfig.variables
  73. draft.outputs = currDefaultConfig.outputs
  74. })
  75. setInputs(newInputs)
  76. }, [allLanguageDefault, inputs, setInputs])
  77. const {
  78. handleVarsChange,
  79. handleAddVariable: handleAddOutputVariable,
  80. handleRemoveVariable,
  81. isShowRemoveVarConfirm,
  82. hideRemoveVarConfirm,
  83. onRemoveVarConfirm,
  84. } = useOutputVarList<CodeNodeType>({
  85. id,
  86. inputs,
  87. setInputs,
  88. outputKeyOrders,
  89. onOutputKeyOrdersChange: setOutputKeyOrders,
  90. })
  91. const filterVar = useCallback((varPayload: Var) => {
  92. return [VarType.string, VarType.number, VarType.secret, VarType.object, VarType.array, VarType.arrayNumber, VarType.arrayString, VarType.arrayObject].includes(varPayload.type)
  93. }, [])
  94. // single run
  95. const {
  96. isShowSingleRun,
  97. hideSingleRun,
  98. toVarInputs,
  99. runningStatus,
  100. isCompleted,
  101. handleRun,
  102. handleStop,
  103. runInputData,
  104. setRunInputData,
  105. runResult,
  106. } = useOneStepRun<CodeNodeType>({
  107. id,
  108. data: inputs,
  109. defaultRunInputData: {},
  110. })
  111. const varInputs = toVarInputs(inputs.variables)
  112. const inputVarValues = (() => {
  113. const vars: Record<string, any> = {}
  114. Object.keys(runInputData)
  115. .forEach((key) => {
  116. vars[key] = runInputData[key]
  117. })
  118. return vars
  119. })()
  120. const setInputVarValues = useCallback((newPayload: Record<string, any>) => {
  121. setRunInputData(newPayload)
  122. }, [setRunInputData])
  123. const handleCodeAndVarsChange = useCallback((code: string, inputVariables: Variable[], outputVariables: OutputVar) => {
  124. const newInputs = produce(inputs, (draft) => {
  125. draft.code = code
  126. draft.variables = inputVariables
  127. draft.outputs = outputVariables
  128. })
  129. setInputs(newInputs)
  130. syncOutputKeyOrders(outputVariables)
  131. }, [inputs, setInputs, syncOutputKeyOrders])
  132. return {
  133. readOnly,
  134. inputs,
  135. outputKeyOrders,
  136. handleVarListChange,
  137. handleAddVariable,
  138. handleRemoveVariable,
  139. handleCodeChange,
  140. handleCodeLanguageChange,
  141. handleVarsChange,
  142. filterVar,
  143. handleAddOutputVariable,
  144. isShowRemoveVarConfirm,
  145. hideRemoveVarConfirm,
  146. onRemoveVarConfirm,
  147. // single run
  148. isShowSingleRun,
  149. hideSingleRun,
  150. runningStatus,
  151. isCompleted,
  152. handleRun,
  153. handleStop,
  154. varInputs,
  155. inputVarValues,
  156. setInputVarValues,
  157. runResult,
  158. handleCodeAndVarsChange,
  159. }
  160. }
  161. export default useConfig