import React, { useRef, useState } from 'react'; export default function UploadForm({ onSubmit, isLoading }) { const [btnLabel, setBtnLabel] = useState('Analyze'); const [preview, setPreview] = useState(null); const [previewType, setPreviewType] = useState(null); // 'image' | 'video' const [scanning, setScanning] = useState(false); const fileRef = useRef(); function handleFileChange(e) { const file = e.target.files[0]; if (!file) return; const isVideo = file.type.startsWith('video/'); const reader = new FileReader(); reader.onload = (ev) => { setPreview(ev.target.result); setPreviewType(isVideo ? 'video' : 'image'); setBtnLabel(isVideo ? 'Analyze Video' : 'Analyze Image'); }; reader.readAsDataURL(file); } function handleSubmit(e) { e.preventDefault(); const file = fileRef.current.files[0]; // guard first if (!file) return; setScanning(true); // only runs if file exists onSubmit(file); } return ( <>
{/* FIX: removed onClick — input is inside label, so the browser natively opens the file picker on click. The manual fileRef.current.click() was causing a double trigger. */}
{preview && (
{previewType === 'image' ? ( preview ) : (
)} ); }