feat: code var sync

This commit is contained in:
Joel 2024-03-21 16:43:11 +08:00
parent fd7fded6e5
commit fe1168d15a
3 changed files with 12 additions and 2 deletions

View File

@ -218,7 +218,6 @@ export const useWorkflow = () => {
const effectNodes = findUsedVarNodes(oldValeSelector, afterNodes) const effectNodes = findUsedVarNodes(oldValeSelector, afterNodes)
// console.log(effectNodes) // console.log(effectNodes)
if (effectNodes.length > 0) { if (effectNodes.length > 0) {
// debugger
const newNodes = getNodes().map((node) => { const newNodes = getNodes().map((node) => {
if (effectNodes.find(n => n.id === node.id)) if (effectNodes.find(n => n.id === node.id))
return updateNodeVars(node, oldValeSelector, newVarSelector) return updateNodeVars(node, oldValeSelector, newVarSelector)

View File

@ -2,8 +2,12 @@ import { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import { type OutputVar } from '../../code/types' import { type OutputVar } from '../../code/types'
import { VarType } from '@/app/components/workflow/types' import { VarType } from '@/app/components/workflow/types'
import {
useWorkflow,
} from '@/app/components/workflow/hooks'
type Params<T> = { type Params<T> = {
id: string
inputs: T inputs: T
setInputs: (newInputs: T) => void setInputs: (newInputs: T) => void
varKey?: string varKey?: string
@ -11,12 +15,15 @@ type Params<T> = {
onOutputKeyOrdersChange: (newOutputKeyOrders: string[]) => void onOutputKeyOrdersChange: (newOutputKeyOrders: string[]) => void
} }
function useOutputVarList<T>({ function useOutputVarList<T>({
id,
inputs, inputs,
setInputs, setInputs,
varKey = 'outputs', varKey = 'outputs',
outputKeyOrders = [], outputKeyOrders = [],
onOutputKeyOrdersChange, onOutputKeyOrdersChange,
}: Params<T>) { }: Params<T>) {
const { handleOutVarRenameChange } = useWorkflow()
const handleVarsChange = useCallback((newVars: OutputVar, changedIndex?: number, newKey?: string) => { const handleVarsChange = useCallback((newVars: OutputVar, changedIndex?: number, newKey?: string) => {
const newInputs = produce(inputs, (draft: any) => { const newInputs = produce(inputs, (draft: any) => {
draft[varKey] = newVars draft[varKey] = newVars
@ -29,7 +36,10 @@ function useOutputVarList<T>({
}) })
onOutputKeyOrdersChange(newOutputKeyOrders) onOutputKeyOrdersChange(newOutputKeyOrders)
} }
}, [inputs, setInputs, varKey, outputKeyOrders, onOutputKeyOrdersChange])
if (newKey)
handleOutVarRenameChange(id, [id, outputKeyOrders[changedIndex!]], [id, newKey])
}, [inputs, setInputs, handleOutVarRenameChange, id, outputKeyOrders, varKey, onOutputKeyOrdersChange])
const handleAddVariable = useCallback(() => { const handleAddVariable = useCallback(() => {
const newKey = `var-${Object.keys((inputs as any)[varKey]).length + 1}` const newKey = `var-${Object.keys((inputs as any)[varKey]).length + 1}`

View File

@ -86,6 +86,7 @@ const useConfig = (id: string, payload: CodeNodeType) => {
}, [allLanguageDefault, inputs, setInputs]) }, [allLanguageDefault, inputs, setInputs])
const { handleVarsChange, handleAddVariable: handleAddOutputVariable, handleRemoveVariable } = useOutputVarList<CodeNodeType>({ const { handleVarsChange, handleAddVariable: handleAddOutputVariable, handleRemoveVariable } = useOutputVarList<CodeNodeType>({
id,
inputs, inputs,
setInputs, setInputs,
outputKeyOrders, outputKeyOrders,