check-input-forms-hooks.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { useCallback } from 'react'
  2. import { useTranslation } from 'react-i18next'
  3. import type { InputForm } from './type'
  4. import { useToastContext } from '@/app/components/base/toast'
  5. import { InputVarType } from '@/app/components/workflow/types'
  6. import { TransferMethod } from '@/types/app'
  7. export const useCheckInputsForms = () => {
  8. const { t } = useTranslation()
  9. const { notify } = useToastContext()
  10. const checkInputsForm = useCallback((inputs: Record<string, any>, inputsForm: InputForm[]) => {
  11. let hasEmptyInput = ''
  12. let fileIsUploading = false
  13. const requiredVars = inputsForm.filter(({ required }) => required)
  14. if (requiredVars?.length) {
  15. requiredVars.forEach(({ variable, label, type }) => {
  16. if (hasEmptyInput)
  17. return
  18. if (fileIsUploading)
  19. return
  20. if (!inputs[variable])
  21. hasEmptyInput = label as string
  22. if ((type === InputVarType.singleFile || type === InputVarType.multiFiles) && inputs[variable]) {
  23. const files = inputs[variable]
  24. if (Array.isArray(files))
  25. fileIsUploading = files.find(item => item.transferMethod === TransferMethod.local_file && !item.uploadedId)
  26. else
  27. fileIsUploading = files.transferMethod === TransferMethod.local_file && !files.uploadedId
  28. }
  29. })
  30. }
  31. if (hasEmptyInput) {
  32. notify({ type: 'error', message: t('appDebug.errorMessage.valueOfVarRequired', { key: hasEmptyInput }) })
  33. return false
  34. }
  35. if (fileIsUploading) {
  36. notify({ type: 'info', message: t('appDebug.errorMessage.waitForFileUpload') })
  37. return
  38. }
  39. return true
  40. }, [notify, t])
  41. return {
  42. checkInputsForm,
  43. }
  44. }