'use client' import { useCallback, useRef, useState } from 'react' import type { InvoiceInboxItem } from '@/types' import { Upload, Loader2, FileUp } from 'lucide-react' import { cn } from '@/lib/utils' interface InboxUploadZoneProps { onUploadComplete: (item: InvoiceInboxItem) => void isUploading: boolean setIsUploading: (v: boolean) => void } const ACCEPTED_TYPES = ['application/pdf', 'image/jpeg', 'image/png', 'image/webp'] const MAX_SIZE = 10 * 1024 * 1024 // 10 MB export default function InboxUploadZone({ onUploadComplete, isUploading, setIsUploading, }: InboxUploadZoneProps) { const [isDragOver, setIsDragOver] = useState(false) const [error, setError] = useState(null) const inputRef = useRef(null) const uploadFile = useCallback( async (file: File) => { setError(null) if (!ACCEPTED_TYPES.includes(file.type)) { setError('Filtypen stöds inte. Välj PDF, JPEG, PNG eller WebP.') return } if (file.size > MAX_SIZE) { setError('Filen är för stor. Max 10 MB.') return } setIsUploading(true) try { const formData = new FormData() formData.append('file', file) const res = await fetch('/api/extensions/invoice-inbox/inbox', { method: 'POST', body: formData, }) if (!res.ok) { const body = await res.json().catch(() => ({ error: 'Uppladdning misslyckades' })) setError(body.error ?? 'Uppladdning misslyckades') return } const { data } = await res.json() onUploadComplete(data) } catch { setError('Nätverksfel vid uppladdning') } finally { setIsUploading(false) } }, [onUploadComplete, setIsUploading] ) const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault() setIsDragOver(false) const file = e.dataTransfer.files[0] if (file) uploadFile(file) }, [uploadFile] ) const handleDragOver = useCallback((e: React.DragEvent) => { e.preventDefault() setIsDragOver(true) }, []) const handleDragLeave = useCallback((e: React.DragEvent) => { e.preventDefault() setIsDragOver(false) }, []) const handleFileSelect = useCallback( (e: React.ChangeEvent) => { const file = e.target.files?.[0] if (file) uploadFile(file) // Reset so same file can be re-selected e.target.value = '' }, [uploadFile] ) return (
!isUploading && inputRef.current?.click()} className={cn( 'relative flex flex-col items-center justify-center rounded-lg border-2 border-dashed p-6 transition-colors cursor-pointer', isDragOver ? 'border-primary bg-primary/5' : 'border-border hover:border-primary/50 hover:bg-accent/30', isUploading && 'pointer-events-none opacity-60' )} > {isUploading ? ( <>

Laddar upp och analyserar...

) : isDragOver ? ( <>

Släpp filen här

) : ( <>

Dra och släpp en faktura, eller{' '} välj fil

PDF, JPEG, PNG eller WebP (max 10 MB)

)}
{error && (

{error}

)}
) }