'use client' import type { FC } from 'react' import React, { useEffect, useRef, useState } from 'react' import { RiDeleteBinLine, } from '@remixicon/react' import { useTranslation } from 'react-i18next' import { useContext } from 'use-context-selector' import cn from '@/utils/classnames' import { Csv as CSVIcon } from '@/app/components/base/icons/src/public/files' import { ToastContext } from '@/app/components/base/toast' import Button from '@/app/components/base/button' export type Props = { file: File | undefined updateFile: (file?: File) => void } const CSVUploader: FC = ({ file, updateFile, }) => { const { t } = useTranslation() const { notify } = useContext(ToastContext) const [dragging, setDragging] = useState(false) const dropRef = useRef(null) const dragRef = useRef(null) const fileUploader = useRef(null) const handleDragEnter = (e: DragEvent) => { e.preventDefault() e.stopPropagation() e.target !== dragRef.current && setDragging(true) } const handleDragOver = (e: DragEvent) => { e.preventDefault() e.stopPropagation() } const handleDragLeave = (e: DragEvent) => { e.preventDefault() e.stopPropagation() e.target === dragRef.current && setDragging(false) } const handleDrop = (e: DragEvent) => { e.preventDefault() e.stopPropagation() setDragging(false) if (!e.dataTransfer) return const files = [...e.dataTransfer.files] if (files.length > 1) { notify({ type: 'error', message: t('datasetCreation.stepOne.uploader.validation.count') }) return } updateFile(files[0]) } const selectHandle = () => { if (fileUploader.current) fileUploader.current.click() } const removeFile = () => { if (fileUploader.current) fileUploader.current.value = '' updateFile() } const fileChangeHandle = (e: React.ChangeEvent) => { const currentFile = e.target.files?.[0] updateFile(currentFile) } useEffect(() => { dropRef.current?.addEventListener('dragenter', handleDragEnter) dropRef.current?.addEventListener('dragover', handleDragOver) dropRef.current?.addEventListener('dragleave', handleDragLeave) dropRef.current?.addEventListener('drop', handleDrop) return () => { dropRef.current?.removeEventListener('dragenter', handleDragEnter) dropRef.current?.removeEventListener('dragover', handleDragOver) dropRef.current?.removeEventListener('dragleave', handleDragLeave) dropRef.current?.removeEventListener('drop', handleDrop) } }, []) return (
{!file && (
{t('datasetDocuments.list.batchModal.csvUploadTitle')} {t('datasetDocuments.list.batchModal.browse')}
{dragging &&
}
)} {file && (
{file.name.replace(/.csv$/, '')} .csv
)}
) } export default React.memo(CSVUploader)