| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  | import { useCallback, useEffect, useRef } from 'react' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | import produce from 'immer' | 
					
						
							|  |  |  | import useVarList from '../_base/hooks/use-var-list' | 
					
						
							| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  | import type { Var, Variable } from '../../types' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 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' | 
					
						
							| 
									
										
										
										
											2024-05-10 18:14:05 +08:00
										 |  |  | import useAvailableVarList from '@/app/components/workflow/nodes/_base/hooks/use-available-var-list' | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const useConfig = (id: string, payload: TemplateTransformNodeType) => { | 
					
						
							|  |  |  |   const { nodesReadOnly: readOnly } = useNodesReadOnly() | 
					
						
							|  |  |  |   const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  |   const { inputs, setInputs: doSetInputs } = useNodeCrud<TemplateTransformNodeType>(id, payload) | 
					
						
							|  |  |  |   const inputsRef = useRef(inputs) | 
					
						
							|  |  |  |   const setInputs = useCallback((newPayload: TemplateTransformNodeType) => { | 
					
						
							|  |  |  |     doSetInputs(newPayload) | 
					
						
							|  |  |  |     inputsRef.current = newPayload | 
					
						
							|  |  |  |   }, [doSetInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-10 18:14:05 +08:00
										 |  |  |   const { availableVars } = useAvailableVarList(id, { | 
					
						
							|  |  |  |     onlyLeafNodeVar: false, | 
					
						
							|  |  |  |     filterVar: () => true, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 11:34:30 +08:00
										 |  |  |   const { handleAddVariable: handleAddEmptyVariable } = useVarList<TemplateTransformNodeType>({ | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     inputs, | 
					
						
							|  |  |  |     setInputs, | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 11:34:30 +08:00
										 |  |  |   const handleVarListChange = useCallback((newList: Variable[]) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputsRef.current, (draft: any) => { | 
					
						
							|  |  |  |       draft.variables = newList | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  |   const handleAddVariable = useCallback((payload: Variable) => { | 
					
						
							|  |  |  |     const newInputs = produce(inputsRef.current, (draft: any) => { | 
					
						
							|  |  |  |       draft.variables.push(payload) | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							|  |  |  |   }, [setInputs]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-29 11:34:30 +08:00
										 |  |  |   // 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]) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |   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) => { | 
					
						
							| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  |     const newInputs = produce(inputsRef.current, (draft: any) => { | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |       draft.template = template | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |     setInputs(newInputs) | 
					
						
							| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  |   }, [setInputs]) | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // 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, | 
					
						
							| 
									
										
										
										
											2024-05-10 18:14:05 +08:00
										 |  |  |     availableVars, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     handleVarListChange, | 
					
						
							| 
									
										
										
										
											2024-04-29 11:34:30 +08:00
										 |  |  |     handleVarNameChange, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     handleAddVariable, | 
					
						
							| 
									
										
										
										
											2024-04-28 17:51:58 +08:00
										 |  |  |     handleAddEmptyVariable, | 
					
						
							| 
									
										
										
										
											2024-04-08 18:51:46 +08:00
										 |  |  |     handleCodeChange, | 
					
						
							|  |  |  |     filterVar, | 
					
						
							|  |  |  |     // single run
 | 
					
						
							|  |  |  |     isShowSingleRun, | 
					
						
							|  |  |  |     hideSingleRun, | 
					
						
							|  |  |  |     runningStatus, | 
					
						
							|  |  |  |     handleRun, | 
					
						
							|  |  |  |     handleStop, | 
					
						
							|  |  |  |     varInputs, | 
					
						
							|  |  |  |     inputVarValues, | 
					
						
							|  |  |  |     setInputVarValues, | 
					
						
							|  |  |  |     runResult, | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default useConfig |