230 lines
8.5 KiB
TypeScript
230 lines
8.5 KiB
TypeScript
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<HTMLInputElement>(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<HTMLInputElement>) => {
|
|
const file = e.target.files?.[0]
|
|
if (file) {
|
|
void processFile(file)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<div
|
|
onDragOver={(e) => {
|
|
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',
|
|
)}
|
|
>
|
|
<input
|
|
ref={fileInputRef}
|
|
type="file"
|
|
accept=".csv,.xlsx,.xls,.txt"
|
|
className="hidden"
|
|
onChange={handleFileSelect}
|
|
/>
|
|
<UploadCloud className="mb-2 size-10 text-accent" />
|
|
<p className="text-sm font-semibold text-primary">
|
|
{t('bulk.dropzone')}
|
|
</p>
|
|
<p className="mt-1 text-xs text-foreground/60">{t('bulk.formats')}</p>
|
|
|
|
{fileName ? (
|
|
<div className="mt-3 flex items-center gap-2 rounded-lg border border-border bg-surface px-3 py-1.5 text-xs font-medium text-primary">
|
|
<FileSpreadsheet className="size-4 text-accent" />
|
|
<span>{fileName}</span>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
|
|
<div className="flex flex-col items-start gap-1 px-1 text-xs sm:flex-row sm:items-center sm:justify-between">
|
|
<span className="text-foreground/60">{t('bulk.templateHint')}</span>
|
|
<button
|
|
type="button"
|
|
onClick={downloadAttendeeTemplateCsv}
|
|
className="inline-flex shrink-0 items-center gap-1 whitespace-nowrap font-medium text-accent transition-colors hover:text-accent-strong"
|
|
>
|
|
<Download className="size-3.5" />
|
|
<span>{t('bulk.downloadTemplate')}</span>
|
|
</button>
|
|
</div>
|
|
|
|
{isParsingFile ? (
|
|
<div className="flex items-center justify-center gap-2 py-6 text-sm text-foreground/60">
|
|
<Loader2 className="size-4 animate-spin text-accent" />
|
|
<span>{t('bulk.parsing')}</span>
|
|
</div>
|
|
) : null}
|
|
|
|
{parsedContacts.length > 0 ? (
|
|
<div className="space-y-3 pt-2">
|
|
<div className="flex items-center justify-between">
|
|
<div className="flex items-center gap-2 text-xs">
|
|
<span className="font-semibold text-primary">{t('bulk.preview')}</span>
|
|
<Badge variant="success">{t('bulk.ready', { count: validRowsCount })}</Badge>
|
|
{invalidRowsCount > 0 ? <Badge variant="danger">{t('bulk.withErrors', { count: invalidRowsCount })}</Badge> : null}
|
|
</div>
|
|
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
onClick={() => {
|
|
setParsedContacts([])
|
|
setFileName(null)
|
|
}}
|
|
className="h-7 px-2 text-xs text-foreground/50 hover:text-danger"
|
|
>
|
|
{t('bulk.clear')}
|
|
</Button>
|
|
</div>
|
|
|
|
<div className="max-h-60 overflow-y-auto rounded-xl border border-border">
|
|
<table className="w-full text-left text-xs">
|
|
<thead className="sticky top-0 border-b border-border bg-surface font-semibold text-foreground/70">
|
|
<tr>
|
|
<th className="px-3 py-2">{t('bulk.name')}</th>
|
|
<th className="px-3 py-2">{t('bulk.phone')}</th>
|
|
<th className="px-3 py-2">{t('bulk.email')}</th>
|
|
<th className="px-3 py-2">{t('bulk.status')}</th>
|
|
<th className="px-2 py-2 text-right" />
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-border">
|
|
{parsedContacts.map((contact) => (
|
|
<tr
|
|
key={contact.id}
|
|
className={!contact.isValid ? 'bg-danger-soft/30' : 'hover:bg-primary-soft/30'}
|
|
>
|
|
<td className="px-3 py-2 font-medium text-primary">
|
|
{contact.fullName || <span className="italic text-danger">{t('bulk.noName')}</span>}
|
|
</td>
|
|
<td className="px-3 py-2 text-foreground/80">
|
|
{contact.phone || <span className="italic text-danger">{t('bulk.noPhone')}</span>}
|
|
</td>
|
|
<td className="px-3 py-2 text-foreground/60">{contact.email || '—'}</td>
|
|
<td className="px-3 py-2">
|
|
{contact.isValid ? (
|
|
<Badge variant="success" className="py-0 text-[10px]">
|
|
{t('bulk.valid')}
|
|
</Badge>
|
|
) : (
|
|
<Badge variant="danger" className="py-0 text-[10px]" title={contact.error}>
|
|
{contact.error}
|
|
</Badge>
|
|
)}
|
|
</td>
|
|
<td className="px-2 py-2 text-right">
|
|
<button
|
|
type="button"
|
|
onClick={() => removeParsedContact(contact.id)}
|
|
className="rounded p-1 text-foreground/40 transition-colors hover:text-danger"
|
|
title={t('bulk.removeRow')}
|
|
>
|
|
<Trash2 className="size-3.5" />
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div className="flex flex-col-reverse gap-2.5 border-t border-border pt-3 sm:flex-row sm:items-center sm:justify-end">
|
|
<Button variant="ghost" onClick={cancelAddAttendee} className="w-full sm:w-auto">
|
|
{t('common:actions.cancel')}
|
|
</Button>
|
|
<Button
|
|
variant="primary"
|
|
onClick={requestBulkImport}
|
|
disabled={isSubmittingBulkImport || validRowsCount === 0}
|
|
className="w-full sm:w-auto"
|
|
>
|
|
{isSubmittingBulkImport ? <Loader2 className="size-4 animate-spin" /> : <Check className="size-4" />}
|
|
<span>{t('bulk.confirm', { count: validRowsCount })}</span>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
) : null}
|
|
</div>
|
|
)
|
|
} |