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