import { useState, useRef, useCallback } from "react"; import { UploadCloudIcon, FileTextIcon, XIcon, AlertCircleIcon, Loader2Icon } from "lucide-react"; interface Step1Props { onAnalyze: (file: File | null, text: string) => void; isLoading: boolean; } export function Step1Upload({ onAnalyze, isLoading }: Step1Props) { const [dragOver, setDragOver] = useState(false); const [uploadedFile, setUploadedFile] = useState(null); const [manualText, setManualText] = useState(""); const [activeInput, setActiveInput] = useState<"file" | "text" | null>(null); const fileInputRef = useRef(null); const handleDrop = useCallback((e: React.DragEvent) => { e.preventDefault(); setDragOver(false); const file = e.dataTransfer.files[0]; if (file && (file.type === "application/pdf" || file.name.endsWith(".docx"))) { setUploadedFile(file); setActiveInput("file"); setManualText(""); } }, []); const handleFileChange = (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (file) { setUploadedFile(file); setActiveInput("file"); setManualText(""); } }; const handleTextChange = (e: React.ChangeEvent) => { setManualText(e.target.value); setActiveInput(e.target.value.length > 0 ? "text" : null); if (e.target.value.length > 0) setUploadedFile(null); }; const canAnalyze = (activeInput === "file" && uploadedFile !== null) || (activeInput === "text" && manualText.trim().length > 50); const formatFileSize = (bytes: number) => { if (bytes < 1024) return `${bytes} B`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; }; const handleSubmit = () => { if (!canAnalyze || isLoading) return; onAnalyze(uploadedFile, manualText); }; return (

Input Dokumen

Upload laporan uji laboratorium produk pangan atau tempel teks secara manual untuk dianalisis.

{/* Upload Panel */}
{ e.preventDefault(); setDragOver(true); }} onDragLeave={() => setDragOver(false)} onDrop={handleDrop} >

Upload Dokumen

Drag & drop atau klik untuk browse

PDF DOCX
{uploadedFile ? (

{uploadedFile.name}

{formatFileSize(uploadedFile.size)}

) : (
fileInputRef.current?.click()} >

Klik atau drag file ke sini

Maksimal 25 MB per file

)}
{!uploadedFile && !dragOver && (

Pastikan dokumen berisi data uji laboratorium lengkap termasuk parameter, nilai, dan satuan ukuran.

)}
{/* Manual Text Panel */}

Input Teks Manual

Salin dan tempel data lab report

{manualText.length > 0 && ( {manualText.length.toLocaleString()} karakter )}