chore: code output var empty check

This commit is contained in:
Joel 2024-03-29 11:48:45 +08:00
parent a32465eeb8
commit 1ea8504cf1
2 changed files with 23 additions and 1 deletions

View File

@ -2,10 +2,13 @@
import type { FC } from 'react' import type { FC } from 'react'
import React, { useCallback } from 'react' import React, { useCallback } from 'react'
import produce from 'immer' import produce from 'immer'
import { useTranslation } from 'react-i18next'
import type { OutputVar } from '../../../code/types' import type { OutputVar } from '../../../code/types'
import RemoveButton from '../remove-button' import RemoveButton from '../remove-button'
import VarTypePicker from './var-type-picker' import VarTypePicker from './var-type-picker'
import type { VarType } from '@/app/components/workflow/types' import type { VarType } from '@/app/components/workflow/types'
import { checkKeys } from '@/utils/var'
import Toast from '@/app/components/base/toast'
type Props = { type Props = {
readonly: boolean readonly: boolean
@ -22,6 +25,8 @@ const OutputVarList: FC<Props> = ({
onChange, onChange,
onRemove, onRemove,
}) => { }) => {
const { t } = useTranslation()
const list = outputKeyOrders.map((key) => { const list = outputKeyOrders.map((key) => {
return { return {
variable: key, variable: key,
@ -33,6 +38,23 @@ const OutputVarList: FC<Props> = ({
const oldKey = list[index].variable const oldKey = list[index].variable
const newKey = e.target.value const newKey = e.target.value
const { isValid, errorKey, errorMessageKey } = checkKeys([newKey], true)
if (!isValid) {
Toast.notify({
type: 'error',
message: t(`appDebug.varKeyError.${errorMessageKey}`, { key: errorKey }),
})
return
}
if (list.map(item => item.variable?.trim()).includes(newKey.trim())) {
Toast.notify({
type: 'error',
message: t('appDebug.varKeyError.keyAlreadyExists', { key: newKey }),
})
return
}
const newOutputs = produce(outputs, (draft) => { const newOutputs = produce(outputs, (draft) => {
draft[newKey] = draft[oldKey] draft[newKey] = draft[oldKey]
delete draft[oldKey] delete draft[oldKey]

View File

@ -44,7 +44,7 @@ function useOutputVarList<T>({
}, [inputs, setInputs, handleOutVarRenameChange, id, outputKeyOrders, varKey, onOutputKeyOrdersChange]) }, [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}`
const newInputs = produce(inputs, (draft: any) => { const newInputs = produce(inputs, (draft: any) => {
draft[varKey] = { draft[varKey] = {
...draft[varKey], ...draft[varKey],