From d889e1b23345be9de79c8189886ab97746b87e56 Mon Sep 17 00:00:00 2001 From: Joel Date: Tue, 2 Jul 2024 13:02:59 +0800 Subject: [PATCH] fix: output variable name may be duplicate (#5845) --- .../workflow/nodes/_base/hooks/use-output-var-list.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts b/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts index 3f238b44e..c7bce2ef0 100644 --- a/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts +++ b/web/app/components/workflow/nodes/_base/hooks/use-output-var-list.ts @@ -43,8 +43,14 @@ function useOutputVarList({ handleOutVarRenameChange(id, [id, outputKeyOrders[changedIndex!]], [id, newKey]) }, [inputs, setInputs, handleOutVarRenameChange, id, outputKeyOrders, varKey, onOutputKeyOrdersChange]) + const generateNewKey = useCallback(() => { + let keyIndex = Object.keys((inputs as any)[varKey]).length + 1 + while (((inputs as any)[varKey])[`var_${keyIndex}`]) + keyIndex++ + return `var_${keyIndex}` + }, [inputs, varKey]) const handleAddVariable = useCallback(() => { - const newKey = `var_${Object.keys((inputs as any)[varKey]).length + 1}` + const newKey = generateNewKey() const newInputs = produce(inputs, (draft: any) => { draft[varKey] = { ...draft[varKey], @@ -56,7 +62,7 @@ function useOutputVarList({ }) setInputs(newInputs) onOutputKeyOrdersChange([...outputKeyOrders, newKey]) - }, [inputs, setInputs, varKey, outputKeyOrders, onOutputKeyOrdersChange]) + }, [generateNewKey, inputs, setInputs, onOutputKeyOrdersChange, outputKeyOrders, varKey]) const [isShowRemoveVarConfirm, { setTrue: showRemoveVarConfirm,