From ada632f9f5debbe9e01087a382583e4da731c98e Mon Sep 17 00:00:00 2001 From: twwu Date: Mon, 30 Jun 2025 15:28:55 +0800 Subject: [PATCH] feat: Enhance input field handling by adding allVariableNames prop and localizing error messages --- .../input-field/field-list/hooks.ts | 22 ++++++++++++++----- .../input-field/field-list/index.tsx | 9 +++++++- .../components/input-field/index.tsx | 6 +++++ web/i18n/en-US/dataset-pipeline.ts | 3 +++ web/i18n/zh-Hans/dataset-pipeline.ts | 3 +++ 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/web/app/components/rag-pipeline/components/input-field/field-list/hooks.ts b/web/app/components/rag-pipeline/components/input-field/field-list/hooks.ts index a2aff2ce3a..3fc85d55e7 100644 --- a/web/app/components/rag-pipeline/components/input-field/field-list/hooks.ts +++ b/web/app/components/rag-pipeline/components/input-field/field-list/hooks.ts @@ -11,14 +11,24 @@ import { ChangeType } from '@/app/components/workflow/types' import { useBoolean } from 'ahooks' import Toast from '@/app/components/base/toast' import { usePipeline } from '../../../hooks/use-pipeline' +import { useTranslation } from 'react-i18next' const VARIABLE_PREFIX = 'rag' -export const useFieldList = ( +type useFieldListProps = { initialInputFields: InputVar[], onInputFieldsChange: (value: InputVar[]) => void, nodeId: string, -) => { + allVariableNames: string[], +} + +export const useFieldList = ({ + initialInputFields, + onInputFieldsChange, + nodeId, + allVariableNames, +}: useFieldListProps) => { + const { t } = useTranslation() const [inputFields, setInputFields] = useState(initialInputFields) const inputFieldsRef = useRef(inputFields) const [removedVar, setRemovedVar] = useState([]) @@ -81,12 +91,12 @@ export const useFieldList = ( }, [removedIndex, handleInputFieldsChange, removeUsedVarInNodes, removedVar, hideRemoveVarConfirm]) const handleSubmitField = useCallback((data: InputVar, moreInfo?: MoreInfo) => { - const isDuplicate = inputFieldsRef.current.some(field => - field.variable === data.variable && field.variable !== editingField?.variable) + const isDuplicate = allVariableNames.some(name => + name === data.variable && name !== editingField?.variable) if (isDuplicate) { Toast.notify({ type: 'error', - message: 'Variable name already exists. Please choose a different name.', + message: t('datasetPipeline.inputFieldPanel.error.variableDuplicate'), }) return } @@ -103,7 +113,7 @@ export const useFieldList = ( if (moreInfo?.type === ChangeType.changeVarName) handleInputVarRename(nodeId, [VARIABLE_PREFIX, nodeId, moreInfo.payload?.beforeKey || ''], [VARIABLE_PREFIX, nodeId, moreInfo.payload?.afterKey || '']) handleCloseInputFieldEditor() - }, [editingField?.variable, handleCloseInputFieldEditor, handleInputFieldsChange, handleInputVarRename, nodeId]) + }, [allVariableNames, editingField?.variable, handleCloseInputFieldEditor, handleInputFieldsChange, handleInputVarRename, nodeId, t]) return { inputFields, diff --git a/web/app/components/rag-pipeline/components/input-field/field-list/index.tsx b/web/app/components/rag-pipeline/components/input-field/field-list/index.tsx index 23106c2763..d082534ce4 100644 --- a/web/app/components/rag-pipeline/components/input-field/field-list/index.tsx +++ b/web/app/components/rag-pipeline/components/input-field/field-list/index.tsx @@ -15,6 +15,7 @@ type FieldListProps = { handleInputFieldsChange: (key: string, value: InputVar[]) => void readonly?: boolean labelClassName?: string + allVariableNames: string[] } const FieldList = ({ @@ -24,6 +25,7 @@ const FieldList = ({ handleInputFieldsChange, readonly, labelClassName, + allVariableNames, }: FieldListProps) => { const onInputFieldsChange = useCallback((value: InputVar[]) => { handleInputFieldsChange(nodeId, value) @@ -41,7 +43,12 @@ const FieldList = ({ isShowRemoveVarConfirm, hideRemoveVarConfirm, onRemoveVarConfirm, - } = useFieldList(initialInputFields, onInputFieldsChange, nodeId) + } = useFieldList({ + initialInputFields, + onInputFieldsChange, + nodeId, + allVariableNames, + }) return (
diff --git a/web/app/components/rag-pipeline/components/input-field/index.tsx b/web/app/components/rag-pipeline/components/input-field/index.tsx index b3aa6a2ceb..ad4fef3639 100644 --- a/web/app/components/rag-pipeline/components/input-field/index.tsx +++ b/web/app/components/rag-pipeline/components/input-field/index.tsx @@ -100,6 +100,10 @@ const InputFieldDialog = ({ setPreviewPanelOpen(prev => !prev) }, []) + const allVariableNames = useMemo(() => { + return ragPipelineVariables?.map(variable => variable.variable) || [] + }, [ragPipelineVariables]) + return ( <> ) }) @@ -173,6 +178,7 @@ const InputFieldDialog = ({ readonly={readonly} labelClassName='pt-2 pb-1' handleInputFieldsChange={updateInputFields} + allVariableNames={allVariableNames} />
diff --git a/web/i18n/en-US/dataset-pipeline.ts b/web/i18n/en-US/dataset-pipeline.ts index 84e4ae845a..c01af0a625 100644 --- a/web/i18n/en-US/dataset-pipeline.ts +++ b/web/i18n/en-US/dataset-pipeline.ts @@ -82,6 +82,9 @@ const translation = { stepOneTitle: 'Data Source', stepTwoTitle: 'Process Documents', }, + error: { + variableDuplicate: 'Variable name already exists. Please choose a different name.', + }, }, addDocuments: { title: 'Add Documents', diff --git a/web/i18n/zh-Hans/dataset-pipeline.ts b/web/i18n/zh-Hans/dataset-pipeline.ts index 9639958e0a..682ea34940 100644 --- a/web/i18n/zh-Hans/dataset-pipeline.ts +++ b/web/i18n/zh-Hans/dataset-pipeline.ts @@ -82,6 +82,9 @@ const translation = { stepOneTitle: '数据源', stepTwoTitle: '处理文档', }, + error: { + variableDuplicate: '变量名已存在。请选择其他名称。', + }, }, addDocuments: { title: '添加文档',