You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

file-preview.tsx 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. 'use client'
  2. import React from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import Loading from './loading'
  5. import type { CustomFile as File } from '@/models/datasets'
  6. import { RiCloseLine } from '@remixicon/react'
  7. import { useFilePreview } from '@/service/use-common'
  8. import DocumentFileIcon from '../../../common/document-file-icon'
  9. import { formatNumberAbbreviated } from '@/utils/format'
  10. type FilePreviewProps = {
  11. file: File
  12. hidePreview: () => void
  13. }
  14. const FilePreview = ({
  15. file,
  16. hidePreview,
  17. }: FilePreviewProps) => {
  18. const { t } = useTranslation()
  19. const { data: fileData, isFetching } = useFilePreview(file.id || '')
  20. const getFileName = (currentFile?: File) => {
  21. if (!currentFile)
  22. return ''
  23. const arr = currentFile.name.split('.')
  24. return arr.slice(0, -1).join()
  25. }
  26. const getFileSize = (size: number) => {
  27. if (size / 1024 < 10)
  28. return `${(size / 1024).toFixed(1)} KB`
  29. return `${(size / 1024 / 1024).toFixed(1)} MB`
  30. }
  31. return (
  32. <div className='h-full rounded-t-xl border-l border-t border-components-panel-border bg-background-default-lighter shadow-md shadow-shadow-shadow-5'>
  33. <div className='flex gap-x-2 pb-3 pl-6 pr-4 pt-4'>
  34. <div className='flex grow flex-col gap-y-1'>
  35. <div className='system-2xs-semibold-uppercase'>{t('datasetPipeline.addDocuments.stepOne.preview')}</div>
  36. <div className='title-md-semi-bold text-tex-primary'>{`${getFileName(file)}.${file.extension}`}</div>
  37. <div className='system-xs-medium flex gap-x-1 text-text-tertiary'>
  38. <DocumentFileIcon
  39. className='size-6 shrink-0'
  40. name={file.name}
  41. extension={file.extension}
  42. />
  43. <span className='uppercase'>{file.extension}</span>
  44. <span>·</span>
  45. <span>{getFileSize(file.size)}</span>
  46. {fileData && (
  47. <>
  48. <span>·</span>
  49. <span>{`${formatNumberAbbreviated(fileData.content.length)} ${t('datasetPipeline.addDocuments.characters')}`}</span>
  50. </>
  51. )}
  52. </div>
  53. </div>
  54. <button
  55. type='button'
  56. className='flex h-8 w-8 shrink-0 items-center justify-center'
  57. onClick={hidePreview}
  58. >
  59. <RiCloseLine className='size-[18px]' />
  60. </button>
  61. </div>
  62. <div className='px-6 py-5'>
  63. {isFetching && <Loading />}
  64. {!isFetching && fileData && (
  65. <div className='body-md-regular overflow-hidden text-text-secondary'>{fileData.content}</div>
  66. )}
  67. </div>
  68. </div>
  69. )
  70. }
  71. export default FilePreview