'use client'; import { useRef, useState, type ChangeEvent } from 'react'; import { documentToasts } from '@/lib/toast'; import type { Document } from '@/types/document'; interface DocumentUploadButtonProps { onUploaded: (doc: Document) => void; } export const DocumentUploadButton = ({ onUploaded }: DocumentUploadButtonProps) => { const [uploading, setUploading] = useState(false); const fileInputRef = useRef(null); const handleUpload = async (e: ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; setUploading(true); try { const formData = new FormData(); formData.append('file', file); const response = await fetch('/api/documents', { method: 'POST', body: formData, }); const data = await response.json(); if (data.success) { onUploaded(data.document); documentToasts.uploadSuccess(file.name); } else { documentToasts.uploadError(data.error); } } catch { documentToasts.uploadError(); } finally { setUploading(false); if (fileInputRef.current) { fileInputRef.current.value = ''; } } }; return (
); };