import React, { useState, useRef, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { CheckCircleIcon, XCircleIcon, UploadIcon, ScanIcon, ArrowLeftIcon } from './Icons'; import { SingleAnalysisReport } from '../types'; import { uploadSingle, classifySingle, getSamples, useSample } from '../services/apiService'; interface SingleAnalysisProps { onBack: () => void; } const SingleAnalysis: React.FC = () => { const navigate = useNavigate(); const [image, setImage] = useState(null); const [preview, setPreview] = useState(null); const [loading, setLoading] = useState(false); const [report, setReport] = useState(null); const [samples, setSamples] = useState<{ id: number, url: string, filename: string }[]>([]); const fileInputRef = useRef(null); useEffect(() => { const fetchSamples = async () => { try { const data = await getSamples(); if (Array.isArray(data)) { setSamples(data); } } catch (err) { console.error("Failed to fetch samples", err); } }; fetchSamples(); }, []); const handleFileChange = (e: React.ChangeEvent) => { if (e.target.files && e.target.files[0]) { const file = e.target.files[0]; setImage(file); setPreview(URL.createObjectURL(file)); setReport(null); } }; const runClassification = async (filename: string) => { setLoading(true); try { const result = await classifySingle(filename); setReport(result); } catch (err) { console.error(err); alert("Analysis failed. Please try again."); } finally { setLoading(false); } }; const handleUploadAndAnalyze = async () => { if (!image) return; setLoading(true); try { const filename = await uploadSingle(image); await runClassification(filename); } catch (err) { console.error(err); alert("Upload failed."); setLoading(false); } }; const handleSampleSelect = async (filename: string) => { setLoading(true); try { // Call backend to copy sample to uploads folder await useSample(filename, 'single'); // Set preview setPreview(`/static/samples/${filename}`); setImage(null); // Clear file input // Run classification on the sample (now in uploads folder) await runClassification(filename); } catch (err) { console.error("Failed to use sample", err); alert("Failed to load sample."); setLoading(false); } }; const reset = () => { setImage(null); setPreview(null); setReport(null); }; return (

Single Image Analysis

{/* Left: Upload / Preview */}
{preview ? (
Preview
) : (
fileInputRef.current?.click()} className="cursor-pointer flex flex-col items-center text-center p-8 border-2 border-dashed border-slate-700 hover:border-cyan-500 rounded-2xl transition-colors w-full h-full justify-center" >

Upload Image

Drag & drop or click to browse

Supports PNG, JPG, JPEG

)}
{/* Sample Gallery */}

Or try a sample

{samples.map((sample) => { const isSelected = preview === sample.url; return (
handleSampleSelect(sample.filename)} > {`Sample
{isSelected && (
)}
); })}
{/* Right: Report Area */}
{!report && (

Upload an image or select a sample to generate a compliance report.

{image && !loading && ( )}
)} {report && (

Compliance Report

AI Verified
{/* DEBUG: Check what we are receiving */} {/*
{JSON.stringify(report, null, 2)}
*/} {/* Render Tailwind Table */}
{report.detailed_results && report.detailed_results.map(([label, result], index) => { const isFail = String(result).startsWith('1') || result === 1; return ( ); })}
Label Result
{label} {isFail ? 'Fail' : 'Pass'}
)}
); }; export default SingleAnalysis;