"use client"; import { useRef, useEffect } from "react"; import { Bug, Loader2 } from "lucide-react"; import { useKaTeXInjection } from "../hooks"; import { useTranslation } from "react-i18next"; interface HTMLViewerProps { html: string; currentIndex: number; loadingMessage: string; onOpenDebugModal: () => void; } export default function HTMLViewer({ html, currentIndex, loadingMessage, onOpenDebugModal, }: HTMLViewerProps) { const { t } = useTranslation(); const htmlFrameRef = useRef(null); const { injectKaTeX } = useKaTeXInjection(); // Update HTML iframe useEffect(() => { if (!html) return; const timer = setTimeout(() => { if (htmlFrameRef.current) { const iframe = htmlFrameRef.current; console.log("Updating iframe with HTML, length:", html.length); // Inject KaTeX support if needed const htmlWithKaTeX = injectKaTeX(html); // Use srcdoc attribute (most reliable method) try { iframe.srcdoc = htmlWithKaTeX; console.log("Iframe srcdoc set successfully with KaTeX support"); } catch (e) { console.warn("srcdoc not supported, using contentDocument:", e); // Fallback to contentDocument if srcdoc not supported const handleLoad = () => { try { const doc = iframe.contentDocument || iframe.contentWindow?.document; if (doc) { doc.open(); doc.write(htmlWithKaTeX); doc.close(); console.log( "Iframe content written via contentDocument with KaTeX support", ); } } catch (err) { console.error("Failed to write to iframe:", err); } }; if ( iframe.contentDocument && iframe.contentDocument.readyState === "complete" ) { handleLoad(); } else { iframe.onload = handleLoad; } } } else { console.warn("htmlFrameRef.current is null"); } }, 100); return () => clearTimeout(timer); }, [html, currentIndex, injectKaTeX]); if (!html) { return (

{loadingMessage || t("Loading learning content...")}

); } return (
{/* Debug Button */} {/* HTML Content */}