csv-uploader.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 'use client'
  2. import type { FC } from 'react'
  3. import React, { useEffect, useRef, useState } from 'react'
  4. import cn from 'classnames'
  5. import { useTranslation } from 'react-i18next'
  6. import { useContext } from 'use-context-selector'
  7. import { Csv as CSVIcon } from '@/app/components/base/icons/src/public/files'
  8. import { ToastContext } from '@/app/components/base/toast'
  9. import { Trash03 } from '@/app/components/base/icons/src/vender/line/general'
  10. import Button from '@/app/components/base/button'
  11. export type Props = {
  12. file: File | undefined
  13. updateFile: (file?: File) => void
  14. }
  15. const CSVUploader: FC<Props> = ({
  16. file,
  17. updateFile,
  18. }) => {
  19. const { t } = useTranslation()
  20. const { notify } = useContext(ToastContext)
  21. const [dragging, setDragging] = useState(false)
  22. const dropRef = useRef<HTMLDivElement>(null)
  23. const dragRef = useRef<HTMLDivElement>(null)
  24. const fileUploader = useRef<HTMLInputElement>(null)
  25. const handleDragEnter = (e: DragEvent) => {
  26. e.preventDefault()
  27. e.stopPropagation()
  28. e.target !== dragRef.current && setDragging(true)
  29. }
  30. const handleDragOver = (e: DragEvent) => {
  31. e.preventDefault()
  32. e.stopPropagation()
  33. }
  34. const handleDragLeave = (e: DragEvent) => {
  35. e.preventDefault()
  36. e.stopPropagation()
  37. e.target === dragRef.current && setDragging(false)
  38. }
  39. const handleDrop = (e: DragEvent) => {
  40. e.preventDefault()
  41. e.stopPropagation()
  42. setDragging(false)
  43. if (!e.dataTransfer)
  44. return
  45. const files = [...e.dataTransfer.files]
  46. if (files.length > 1) {
  47. notify({ type: 'error', message: t('datasetCreation.stepOne.uploader.validation.count') })
  48. return
  49. }
  50. updateFile(files[0])
  51. }
  52. const selectHandle = () => {
  53. if (fileUploader.current)
  54. fileUploader.current.click()
  55. }
  56. const removeFile = () => {
  57. if (fileUploader.current)
  58. fileUploader.current.value = ''
  59. updateFile()
  60. }
  61. const fileChangeHandle = (e: React.ChangeEvent<HTMLInputElement>) => {
  62. const currentFile = e.target.files?.[0]
  63. updateFile(currentFile)
  64. }
  65. useEffect(() => {
  66. dropRef.current?.addEventListener('dragenter', handleDragEnter)
  67. dropRef.current?.addEventListener('dragover', handleDragOver)
  68. dropRef.current?.addEventListener('dragleave', handleDragLeave)
  69. dropRef.current?.addEventListener('drop', handleDrop)
  70. return () => {
  71. dropRef.current?.removeEventListener('dragenter', handleDragEnter)
  72. dropRef.current?.removeEventListener('dragover', handleDragOver)
  73. dropRef.current?.removeEventListener('dragleave', handleDragLeave)
  74. dropRef.current?.removeEventListener('drop', handleDrop)
  75. }
  76. }, [])
  77. return (
  78. <div className='mt-6'>
  79. <input
  80. ref={fileUploader}
  81. style={{ display: 'none' }}
  82. type="file"
  83. id="fileUploader"
  84. accept='.csv'
  85. onChange={fileChangeHandle}
  86. />
  87. <div ref={dropRef}>
  88. {!file && (
  89. <div className={cn('flex items-center h-20 rounded-xl bg-gray-50 border border-dashed border-gray-200 text-sm font-normal', dragging && 'bg-[#F5F8FF] border border-[#B2CCFF]')}>
  90. <div className='w-full flex items-center justify-center space-x-2'>
  91. <CSVIcon className="shrink-0" />
  92. <div className='text-gray-500'>
  93. {t('datasetDocuments.list.batchModal.csvUploadTitle')}
  94. <span className='text-primary-400 cursor-pointer' onClick={selectHandle}>{t('datasetDocuments.list.batchModal.browse')}</span>
  95. </div>
  96. </div>
  97. {dragging && <div ref={dragRef} className='absolute w-full h-full top-0 left-0'/>}
  98. </div>
  99. )}
  100. {file && (
  101. <div className={cn('flex items-center h-20 px-6 rounded-xl bg-gray-50 border border-gray-200 text-sm font-normal group', 'hover:bg-[#F5F8FF] hover:border-[#B2CCFF]')}>
  102. <CSVIcon className="shrink-0" />
  103. <div className='flex ml-2 w-0 grow'>
  104. <span className='max-w-[calc(100%_-_30px)] text-ellipsis whitespace-nowrap overflow-hidden text-gray-800'>{file.name.replace(/.csv$/, '')}</span>
  105. <span className='shrink-0 text-gray-500'>.csv</span>
  106. </div>
  107. <div className='hidden group-hover:flex items-center'>
  108. <Button className='!h-8 !px-3 !py-[6px] bg-white !text-[13px] !leading-[18px] text-gray-700' onClick={selectHandle}>{t('datasetCreation.stepOne.uploader.change')}</Button>
  109. <div className='mx-2 w-px h-4 bg-gray-200' />
  110. <div className='p-2 cursor-pointer' onClick={removeFile}>
  111. <Trash03 className='w-4 h-4 text-gray-500' />
  112. </div>
  113. </div>
  114. </div>
  115. )}
  116. </div>
  117. </div>
  118. )
  119. }
  120. export default React.memo(CSVUploader)