import { useState, useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { Card } from '../components/common/Card'; import { Button } from '../components/common/Button'; import { Download, FileText, RefreshCw, Sparkles, Loader2, AlertTriangle, CheckCircle, Copy } from 'lucide-react'; import { apiClient } from '../api/client'; import { useStudy } from '../context/StudyContext'; import ReactMarkdown from 'react-markdown'; export default function Results() { const { selectedStudy } = useStudy(); const navigate = useNavigate(); const [reportContent, setReportContent] = useState(null); const [loading, setLoading] = useState(false); const [generating, setGenerating] = useState(false); const [error, setError] = useState(null); const [copied, setCopied] = useState(false); const [lastGenerated, setLastGenerated] = useState(null); // Redirect if no study selected useEffect(() => { if (!selectedStudy) { navigate('/'); } }, [selectedStudy, navigate]); // Load report when study changes useEffect(() => { if (selectedStudy) { loadReport(); } }, [selectedStudy]); const loadReport = async () => { if (!selectedStudy) return; setLoading(true); setError(null); try { const data = await apiClient.getStudyReport(selectedStudy.id); setReportContent(data.content); if (data.generated_at) { setLastGenerated(data.generated_at); } } catch (err: any) { // No report yet - show placeholder setReportContent(null); } finally { setLoading(false); } }; const handleGenerate = async () => { if (!selectedStudy) return; setGenerating(true); setError(null); try { const data = await apiClient.generateReport(selectedStudy.id); setReportContent(data.content); if (data.generated_at) { setLastGenerated(data.generated_at); } } catch (err: any) { setError(err.message || 'Failed to generate report'); } finally { setGenerating(false); } }; const handleCopy = async () => { if (reportContent) { await navigator.clipboard.writeText(reportContent); setCopied(true); setTimeout(() => setCopied(false), 2000); } }; const handleDownload = () => { if (!reportContent || !selectedStudy) return; const blob = new Blob([reportContent], { type: 'text/markdown' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `${selectedStudy.id}_report.md`; document.body.appendChild(a); a.click(); document.body.removeChild(a); URL.revokeObjectURL(url); }; if (!selectedStudy) { return null; } return (
{/* Header */}

Optimization Report

{selectedStudy.name}

{reportContent && ( <> )}
{/* Error Message */} {error && (
{error}
)} {/* Main Content */}

Report Content

{lastGenerated && ( Last generated: {new Date(lastGenerated).toLocaleString()} )}
{loading ? (
Loading report...
) : reportContent ? (
{reportContent}
) : (

No Report Generated

Click "Generate Report" to create an AI-generated analysis of your optimization results.

)}
{/* Study Stats */}
Total Trials
{selectedStudy.progress.current}
Best Value
{selectedStudy.best_value?.toFixed(4) || 'N/A'}
Target
{selectedStudy.target?.toFixed(4) || 'N/A'}
Status
{selectedStudy.status}
); }