index.tsx 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useState } from 'react'
  4. import {
  5. useCSVReader,
  6. } from 'react-papaparse'
  7. import { useTranslation } from 'react-i18next'
  8. import s from './style.module.css'
  9. import cn from '@/utils/classnames'
  10. import { Csv as CSVIcon } from '@/app/components/base/icons/src/public/files'
  11. export type Props = {
  12. onParsed: (data: string[][]) => void
  13. }
  14. const CSVReader: FC<Props> = ({
  15. onParsed,
  16. }) => {
  17. const { t } = useTranslation()
  18. const { CSVReader } = useCSVReader()
  19. const [zoneHover, setZoneHover] = useState(false)
  20. return (
  21. <CSVReader
  22. onUploadAccepted={(results: any) => {
  23. onParsed(results.data)
  24. setZoneHover(false)
  25. }}
  26. onDragOver={(event: DragEvent) => {
  27. event.preventDefault()
  28. setZoneHover(true)
  29. }}
  30. onDragLeave={(event: DragEvent) => {
  31. event.preventDefault()
  32. setZoneHover(false)
  33. }}
  34. >
  35. {({
  36. getRootProps,
  37. acceptedFile,
  38. }: any) => (
  39. <>
  40. <div
  41. {...getRootProps()}
  42. className={cn(s.zone, zoneHover && s.zoneHover, acceptedFile ? 'px-6' : 'justify-center border-dashed text-gray-500')}
  43. >
  44. {
  45. acceptedFile
  46. ? (
  47. <div className='w-full flex items-center space-x-2'>
  48. <CSVIcon className="shrink-0" />
  49. <div className='flex w-0 grow'>
  50. <span className='max-w-[calc(100%_-_30px)] text-ellipsis whitespace-nowrap overflow-hidden text-gray-800'>{acceptedFile.name.replace(/.csv$/, '')}</span>
  51. <span className='shrink-0 text-gray-500'>.csv</span>
  52. </div>
  53. </div>
  54. )
  55. : (
  56. <div className='flex items-center justify-center space-x-2'>
  57. <CSVIcon className="shrink-0" />
  58. <div className='text-gray-500'>{t('share.generation.csvUploadTitle')}<span className='text-primary-400'>{t('share.generation.browse')}</span></div>
  59. </div>
  60. )}
  61. </div>
  62. </>
  63. )}
  64. </CSVReader>
  65. )
  66. }
  67. export default React.memo(CSVReader)