feat: code support vars

This commit is contained in:
Joel
2024-02-20 18:42:21 +08:00
parent bb87a350ac
commit d58a1b1359
11 changed files with 149 additions and 20 deletions

View File

@ -0,0 +1,37 @@
import { useCallback } from 'react'
import produce from 'immer'
import type { Variable } from '@/app/components/workflow/types'
type Params<T> = {
inputs: T
setInputs: (newInputs: T) => void
varKey?: string
}
function useVarList<T>({
inputs,
setInputs,
varKey = 'variables',
}: Params<T>) {
const handleVarListChange = useCallback((newList: Variable[]) => {
const newInputs = produce(inputs, (draft: any) => {
draft[varKey] = newList
})
setInputs(newInputs)
}, [inputs, setInputs, varKey])
const handleAddVariable = useCallback(() => {
const newInputs = produce(inputs, (draft: any) => {
draft[varKey].push({
variable: '',
value_selector: [],
})
})
setInputs(newInputs)
}, [inputs, setInputs, varKey])
return {
handleVarListChange,
handleAddVariable,
}
}
export default useVarList