file-list.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { useFile } from '../hooks'
  2. import { useStore } from '../store'
  3. import type { FileEntity } from '../types'
  4. import FileImageItem from './file-image-item'
  5. import FileItem from './file-item'
  6. import type { FileUpload } from '@/app/components/base/features/types'
  7. import { SupportUploadFileTypes } from '@/app/components/workflow/types'
  8. import cn from '@/utils/classnames'
  9. type FileListProps = {
  10. className?: string
  11. files: FileEntity[]
  12. onRemove?: (fileId: string) => void
  13. onReUpload?: (fileId: string) => void
  14. showDeleteAction?: boolean
  15. showDownloadAction?: boolean
  16. canPreview?: boolean
  17. }
  18. export const FileList = ({
  19. className,
  20. files,
  21. onReUpload,
  22. onRemove,
  23. showDeleteAction = true,
  24. showDownloadAction = false,
  25. canPreview,
  26. }: FileListProps) => {
  27. return (
  28. <div className={cn('flex flex-wrap gap-2', className)}>
  29. {
  30. files.map((file) => {
  31. if (file.supportFileType === SupportUploadFileTypes.image) {
  32. return (
  33. <FileImageItem
  34. key={file.id}
  35. file={file}
  36. showDeleteAction={showDeleteAction}
  37. showDownloadAction={showDownloadAction}
  38. onRemove={onRemove}
  39. onReUpload={onReUpload}
  40. canPreview={canPreview}
  41. />
  42. )
  43. }
  44. return (
  45. <FileItem
  46. key={file.id}
  47. file={file}
  48. showDeleteAction={showDeleteAction}
  49. showDownloadAction={showDownloadAction}
  50. onRemove={onRemove}
  51. onReUpload={onReUpload}
  52. />
  53. )
  54. })
  55. }
  56. </div>
  57. )
  58. }
  59. type FileListInChatInputProps = {
  60. fileConfig: FileUpload
  61. }
  62. export const FileListInChatInput = ({
  63. fileConfig,
  64. }: FileListInChatInputProps) => {
  65. const files = useStore(s => s.files)
  66. const {
  67. handleRemoveFile,
  68. handleReUploadFile,
  69. } = useFile(fileConfig)
  70. return (
  71. <FileList
  72. files={files}
  73. onReUpload={handleReUploadFile}
  74. onRemove={handleRemoveFile}
  75. />
  76. )
  77. }