file-input.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { useFile } from './hooks'
  2. import { useStore } from './store'
  3. import type { FileUpload } from '@/app/components/base/features/types'
  4. import { FILE_EXTS } from '@/app/components/base/prompt-editor/constants'
  5. import { SupportUploadFileTypes } from '@/app/components/workflow/types'
  6. type FileInputProps = {
  7. fileConfig: FileUpload
  8. }
  9. const FileInput = ({
  10. fileConfig,
  11. }: FileInputProps) => {
  12. const files = useStore(s => s.files)
  13. const { handleLocalFileUpload } = useFile(fileConfig)
  14. const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
  15. const file = e.target.files?.[0]
  16. if (file)
  17. handleLocalFileUpload(file)
  18. }
  19. const allowedFileTypes = fileConfig.allowed_file_types
  20. const isCustom = allowedFileTypes?.includes(SupportUploadFileTypes.custom)
  21. const exts = isCustom ? (fileConfig.allowed_file_extensions?.map(item => `.${item}`) || []) : (allowedFileTypes?.map(type => FILE_EXTS[type]) || []).flat().map(item => `.${item}`)
  22. const accept = exts.join(',')
  23. return (
  24. <input
  25. className='absolute block inset-0 opacity-0 text-[0] w-full disabled:cursor-not-allowed cursor-pointer'
  26. onClick={e => ((e.target as HTMLInputElement).value = '')}
  27. type='file'
  28. onChange={handleChange}
  29. accept={accept}
  30. disabled={!!(fileConfig.number_limits && files.length >= fileConfig?.number_limits)}
  31. />
  32. )
  33. }
  34. export default FileInput