feat: code default value

This commit is contained in:
Joel 2024-03-15 21:54:16 +08:00
parent 6b9cc927c0
commit ec49da073e
1 changed files with 17 additions and 1 deletions

View File

@ -1,20 +1,36 @@
import { useCallback } from 'react'
import { useCallback, useEffect } from 'react'
import produce from 'immer'
import useVarList from '../_base/hooks/use-var-list'
import useOutputVarList from '../_base/hooks/use-output-var-list'
import { VarType } from '../../types'
import type { Var } from '../../types'
import { useStore } from '../../store'
import type { CodeLanguage, CodeNodeType } 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'
const useConfig = (id: string, payload: CodeNodeType) => {
const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
const { inputs, setInputs } = useNodeCrud<CodeNodeType>(id, payload)
const { handleVarListChange, handleAddVariable } = useVarList<CodeNodeType>({
inputs,
setInputs,
})
useEffect(() => {
if (inputs.code)
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((code: string) => {
const newInputs = produce(inputs, (draft) => {
draft.code = code