import { useState, useRef, useEffect } from "react"; import type { StudentReport } from "../../App"; interface ReportViewerProps { reports: StudentReport[]; isGenerating: boolean; onBack: () => void; onExportAllHtml: () => void; } export function ReportViewer({ reports, isGenerating, onBack, onExportAllHtml, }: ReportViewerProps) { const [activeIndex, setActiveIndex] = useState(0); const iframeRef = useRef(null); // Auto-switch to the report currently being generated useEffect(() => { const generatingIdx = reports.findIndex((r) => r.status === "generating"); if (generatingIdx >= 0) { setActiveIndex(generatingIdx); } else { // When generation finishes, show the last completed one let lastDone = -1; for (let j = reports.length - 1; j >= 0; j--) { if (reports[j].status === "done") { lastDone = j; break; } } if (lastDone >= 0) setActiveIndex(lastDone); } }, [reports]); const activeReport = reports[activeIndex]; const doneCount = reports.filter((r) => r.status === "done").length; const errorCount = reports.filter((r) => r.status === "error").length; const handleDownloadSingle = () => { if (!activeReport?.html) return; const blob = new Blob([activeReport.html], { type: "text/html" }); const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = `classlens-${activeReport.studentName}.html`; a.click(); URL.revokeObjectURL(url); }; return (
{/* Progress bar */} {isGenerating && (
生成進度 {doneCount + errorCount} / {reports.length}
)} {/* Action bar */}
{activeReport?.status === "done" && ( )} {doneCount > 1 && ( )}
{/* Student tabs */}
{reports.map((r, i) => ( ))}
{/* Report content */}
{activeReport?.status === "done" && (