import { useRef } from 'react' import { Check, Download, FileSpreadsheet, Loader2, Trash2, UploadCloud } from 'lucide-react' import { useTranslation } from 'react-i18next' import { Badge, Button, useToast } from '../../../components/ui' import { downloadAttendeeTemplateCsv, normalizeRowsToContacts, parseCsvContent, parseXlsxContent, } from '../../../lib/file-parser' import { cn } from '../../../lib/utils' import { useAddAttendee } from './AddAttendeeProvider' export function BulkImportPanel() { const { parsedContacts, setParsedContacts, removeParsedContact, fileName, setFileName, isParsingFile, setIsParsingFile, isDragging, setIsDragging, validRowsCount, invalidRowsCount, isSubmittingBulkImport, cancelAddAttendee, requestBulkImport, } = useAddAttendee() const toast = useToast() const { t } = useTranslation(['groupAdd', 'common']) const fileInputRef = useRef(null) const processFile = async (file: File) => { const fileExtension = file.name.split('.').pop()?.toLowerCase() if (!['csv', 'xlsx', 'xls', 'txt'].includes(fileExtension ?? '')) { toast.error(t('bulk.unsupported')) return } setIsParsingFile(true) setFileName(file.name) try { const rawRows = fileExtension === 'xlsx' || fileExtension === 'xls' ? await parseXlsxContent(await file.arrayBuffer()) : parseCsvContent(await file.text()) const contacts = normalizeRowsToContacts(rawRows) if (contacts.length === 0) { toast.error(t('bulk.noContacts')) } else { setParsedContacts(contacts) const validCount = contacts.filter((c) => c.isValid).length toast.info(t('bulk.detected', { count: contacts.length, valid: validCount })) } } catch (err: unknown) { const message = err instanceof Error ? err.message : t('bulk.processError') toast.error(message) } finally { setIsParsingFile(false) } } const handleFileDrop = (e: React.DragEvent) => { e.preventDefault() setIsDragging(false) const file = e.dataTransfer.files[0] if (file) { void processFile(file) } } const handleFileSelect = (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) { void processFile(file) } } return (
{ e.preventDefault() setIsDragging(true) }} onDragLeave={() => setIsDragging(false)} onDrop={handleFileDrop} onClick={() => fileInputRef.current?.click()} className={cn( 'relative flex cursor-pointer flex-col items-center justify-center rounded-xl border-2 border-dashed p-6 transition-all', isDragging ? 'border-accent bg-accent/5' : 'border-border bg-primary-soft/30 hover:border-accent/60', )} >

{t('bulk.dropzone')}

{t('bulk.formats')}

{fileName ? (
{fileName}
) : null}
{t('bulk.templateHint')}
{isParsingFile ? (
{t('bulk.parsing')}
) : null} {parsedContacts.length > 0 ? (
{t('bulk.preview')} {t('bulk.ready', { count: validRowsCount })} {invalidRowsCount > 0 ? {t('bulk.withErrors', { count: invalidRowsCount })} : null}
{parsedContacts.map((contact) => ( ))}
{t('bulk.name')} {t('bulk.phone')} {t('bulk.email')} {t('bulk.status')}
{contact.fullName || {t('bulk.noName')}} {contact.phone || {t('bulk.noPhone')}} {contact.email || '—'} {contact.isValid ? ( {t('bulk.valid')} ) : ( {contact.error} )}
) : null}
) }