feat: Enhance input field handling by adding allVariableNames prop and localizing error messages

This commit is contained in:
twwu 2025-06-30 15:28:55 +08:00
parent 4c82c9d029
commit ada632f9f5
5 changed files with 36 additions and 7 deletions

View File

@ -11,14 +11,24 @@ import { ChangeType } from '@/app/components/workflow/types'
import { useBoolean } from 'ahooks' import { useBoolean } from 'ahooks'
import Toast from '@/app/components/base/toast' import Toast from '@/app/components/base/toast'
import { usePipeline } from '../../../hooks/use-pipeline' import { usePipeline } from '../../../hooks/use-pipeline'
import { useTranslation } from 'react-i18next'
const VARIABLE_PREFIX = 'rag' const VARIABLE_PREFIX = 'rag'
export const useFieldList = ( type useFieldListProps = {
initialInputFields: InputVar[], initialInputFields: InputVar[],
onInputFieldsChange: (value: InputVar[]) => void, onInputFieldsChange: (value: InputVar[]) => void,
nodeId: string, nodeId: string,
) => { allVariableNames: string[],
}
export const useFieldList = ({
initialInputFields,
onInputFieldsChange,
nodeId,
allVariableNames,
}: useFieldListProps) => {
const { t } = useTranslation()
const [inputFields, setInputFields] = useState<InputVar[]>(initialInputFields) const [inputFields, setInputFields] = useState<InputVar[]>(initialInputFields)
const inputFieldsRef = useRef<InputVar[]>(inputFields) const inputFieldsRef = useRef<InputVar[]>(inputFields)
const [removedVar, setRemovedVar] = useState<ValueSelector>([]) const [removedVar, setRemovedVar] = useState<ValueSelector>([])
@ -81,12 +91,12 @@ export const useFieldList = (
}, [removedIndex, handleInputFieldsChange, removeUsedVarInNodes, removedVar, hideRemoveVarConfirm]) }, [removedIndex, handleInputFieldsChange, removeUsedVarInNodes, removedVar, hideRemoveVarConfirm])
const handleSubmitField = useCallback((data: InputVar, moreInfo?: MoreInfo) => { const handleSubmitField = useCallback((data: InputVar, moreInfo?: MoreInfo) => {
const isDuplicate = inputFieldsRef.current.some(field => const isDuplicate = allVariableNames.some(name =>
field.variable === data.variable && field.variable !== editingField?.variable) name === data.variable && name !== editingField?.variable)
if (isDuplicate) { if (isDuplicate) {
Toast.notify({ Toast.notify({
type: 'error', type: 'error',
message: 'Variable name already exists. Please choose a different name.', message: t('datasetPipeline.inputFieldPanel.error.variableDuplicate'),
}) })
return return
} }
@ -103,7 +113,7 @@ export const useFieldList = (
if (moreInfo?.type === ChangeType.changeVarName) if (moreInfo?.type === ChangeType.changeVarName)
handleInputVarRename(nodeId, [VARIABLE_PREFIX, nodeId, moreInfo.payload?.beforeKey || ''], [VARIABLE_PREFIX, nodeId, moreInfo.payload?.afterKey || '']) handleInputVarRename(nodeId, [VARIABLE_PREFIX, nodeId, moreInfo.payload?.beforeKey || ''], [VARIABLE_PREFIX, nodeId, moreInfo.payload?.afterKey || ''])
handleCloseInputFieldEditor() handleCloseInputFieldEditor()
}, [editingField?.variable, handleCloseInputFieldEditor, handleInputFieldsChange, handleInputVarRename, nodeId]) }, [allVariableNames, editingField?.variable, handleCloseInputFieldEditor, handleInputFieldsChange, handleInputVarRename, nodeId, t])
return { return {
inputFields, inputFields,

View File

@ -15,6 +15,7 @@ type FieldListProps = {
handleInputFieldsChange: (key: string, value: InputVar[]) => void handleInputFieldsChange: (key: string, value: InputVar[]) => void
readonly?: boolean readonly?: boolean
labelClassName?: string labelClassName?: string
allVariableNames: string[]
} }
const FieldList = ({ const FieldList = ({
@ -24,6 +25,7 @@ const FieldList = ({
handleInputFieldsChange, handleInputFieldsChange,
readonly, readonly,
labelClassName, labelClassName,
allVariableNames,
}: FieldListProps) => { }: FieldListProps) => {
const onInputFieldsChange = useCallback((value: InputVar[]) => { const onInputFieldsChange = useCallback((value: InputVar[]) => {
handleInputFieldsChange(nodeId, value) handleInputFieldsChange(nodeId, value)
@ -41,7 +43,12 @@ const FieldList = ({
isShowRemoveVarConfirm, isShowRemoveVarConfirm,
hideRemoveVarConfirm, hideRemoveVarConfirm,
onRemoveVarConfirm, onRemoveVarConfirm,
} = useFieldList(initialInputFields, onInputFieldsChange, nodeId) } = useFieldList({
initialInputFields,
onInputFieldsChange,
nodeId,
allVariableNames,
})
return ( return (
<div className='flex flex-col'> <div className='flex flex-col'>

View File

@ -100,6 +100,10 @@ const InputFieldDialog = ({
setPreviewPanelOpen(prev => !prev) setPreviewPanelOpen(prev => !prev)
}, []) }, [])
const allVariableNames = useMemo(() => {
return ragPipelineVariables?.map(variable => variable.variable) || []
}, [ragPipelineVariables])
return ( return (
<> <>
<DialogWrapper <DialogWrapper
@ -160,6 +164,7 @@ const InputFieldDialog = ({
readonly={readonly} readonly={readonly}
labelClassName='pt-1 pb-1' labelClassName='pt-1 pb-1'
handleInputFieldsChange={updateInputFields} handleInputFieldsChange={updateInputFields}
allVariableNames={allVariableNames}
/> />
) )
}) })
@ -173,6 +178,7 @@ const InputFieldDialog = ({
readonly={readonly} readonly={readonly}
labelClassName='pt-2 pb-1' labelClassName='pt-2 pb-1'
handleInputFieldsChange={updateInputFields} handleInputFieldsChange={updateInputFields}
allVariableNames={allVariableNames}
/> />
</div> </div>
<FooterTip /> <FooterTip />

View File

@ -82,6 +82,9 @@ const translation = {
stepOneTitle: 'Data Source', stepOneTitle: 'Data Source',
stepTwoTitle: 'Process Documents', stepTwoTitle: 'Process Documents',
}, },
error: {
variableDuplicate: 'Variable name already exists. Please choose a different name.',
},
}, },
addDocuments: { addDocuments: {
title: 'Add Documents', title: 'Add Documents',

View File

@ -82,6 +82,9 @@ const translation = {
stepOneTitle: '数据源', stepOneTitle: '数据源',
stepTwoTitle: '处理文档', stepTwoTitle: '处理文档',
}, },
error: {
variableDuplicate: '变量名已存在。请选择其他名称。',
},
}, },
addDocuments: { addDocuments: {
title: '添加文档', title: '添加文档',