index.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use client'
  2. import React, { useEffect, useState } from 'react'
  3. import { useTranslation } from 'react-i18next'
  4. import { XMarkIcon } from '@heroicons/react/20/solid'
  5. import s from './index.module.css'
  6. import cn from '@/utils/classnames'
  7. import type { CustomFile as File } from '@/models/datasets'
  8. import { fetchFilePreview } from '@/service/common'
  9. type IProps = {
  10. file?: File
  11. hidePreview: () => void
  12. }
  13. const FilePreview = ({
  14. file,
  15. hidePreview,
  16. }: IProps) => {
  17. const { t } = useTranslation()
  18. const [previewContent, setPreviewContent] = useState('')
  19. const [loading, setLoading] = useState(true)
  20. const getPreviewContent = async (fileID: string) => {
  21. try {
  22. const res = await fetchFilePreview({ fileID })
  23. setPreviewContent(res.content)
  24. setLoading(false)
  25. }
  26. catch { }
  27. }
  28. const getFileName = (currentFile?: File) => {
  29. if (!currentFile)
  30. return ''
  31. const arr = currentFile.name.split('.')
  32. return arr.slice(0, -1).join()
  33. }
  34. useEffect(() => {
  35. if (file?.id) {
  36. setLoading(true)
  37. getPreviewContent(file.id)
  38. }
  39. }, [file])
  40. return (
  41. <div className={cn(s.filePreview)}>
  42. <div className={cn(s.previewHeader)}>
  43. <div className={cn(s.title)}>
  44. <span>{t('datasetCreation.stepOne.filePreview')}</span>
  45. <div className='flex items-center justify-center w-6 h-6 cursor-pointer' onClick={hidePreview}>
  46. <XMarkIcon className='h-4 w-4'></XMarkIcon>
  47. </div>
  48. </div>
  49. <div className={cn(s.fileName)}>
  50. <span>{getFileName(file)}</span><span className={cn(s.filetype)}>.{file?.extension}</span>
  51. </div>
  52. </div>
  53. <div className={cn(s.previewContent)}>
  54. {loading && <div className={cn(s.loading)} />}
  55. {!loading && (
  56. <div className={cn(s.fileContent)}>{previewContent}</div>
  57. )}
  58. </div>
  59. </div>
  60. )
  61. }
  62. export default FilePreview