feat: var assigner data logic

This commit is contained in:
Joel 2024-02-26 11:18:22 +08:00
parent 6e2611c86c
commit 31930159b8
2 changed files with 61 additions and 0 deletions

View File

@ -0,0 +1,28 @@
import { useCallback, useState } from 'react'
import produce from 'immer'
import useVarList from './use-var-list'
import type { VariableAssignerNodeType } from './types'
const useConfig = (initInputs: VariableAssignerNodeType) => {
const [inputs, setInputs] = useState<VariableAssignerNodeType>(initInputs)
const handleOutputTypeChange = useCallback((outputType: string) => {
const newInputs = produce(inputs, (draft) => {
draft.output_type = outputType
})
setInputs(newInputs)
}, [inputs, setInputs])
const { handleVarListChange, handleAddVariable } = useVarList({
inputs,
setInputs,
})
return {
inputs,
handleOutputTypeChange,
handleVarListChange,
handleAddVariable,
}
}
export default useConfig

View File

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