csv-uploader.tsx 4.6 KB

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