dify/web/app/components/base/chat/chat/check-input-forms-hooks.ts
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

55 lines
1.8 KiB
TypeScript

import type { InputForm } from './type'
import { toast } from '@langgenius/dify-ui/toast'
import { useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { InputVarType } from '@/app/components/workflow/types'
import { TransferMethod } from '@/types/app'
export const useCheckInputsForms = () => {
const { t } = useTranslation()
const checkInputsForm = useCallback(
(inputs: Record<string, any>, inputsForm: InputForm[]) => {
let hasEmptyInput = ''
let fileIsUploading = false
const requiredVars = inputsForm.filter(
({ required, type }) => required && type !== InputVarType.checkbox,
) // boolean can be not checked
if (requiredVars?.length) {
requiredVars.forEach(({ variable, label, type }) => {
if (hasEmptyInput) return
if (fileIsUploading) return
if (!inputs[variable]) hasEmptyInput = label as string
if (
(type === InputVarType.singleFile || type === InputVarType.multiFiles) &&
inputs[variable]
) {
const files = inputs[variable]
if (Array.isArray(files))
fileIsUploading = files.find(
(item) => item.transferMethod === TransferMethod.local_file && !item.uploadedId,
)
else
fileIsUploading =
files.transferMethod === TransferMethod.local_file && !files.uploadedId
}
})
}
if (hasEmptyInput) {
toast.error(
t(($) => $['errorMessage.valueOfVarRequired'], { ns: 'appDebug', key: hasEmptyInput }),
)
return false
}
if (fileIsUploading) {
toast.info(t(($) => $['errorMessage.waitForFileUpload'], { ns: 'appDebug' }))
return
}
return true
},
[t],
)
return {
checkInputsForm,
}
}