import React, { useCallback, useEffect, useRef } from "react"; import { FileCode2, Globe } from "lucide-react"; import { basename } from "../lib/filePreview"; import { IFRAME_ALLOW, IFRAME_SANDBOX } from "../lib/iframePreview"; /** Mini-navigateur pour aperçu HTML live — chrome pro, iframe sandbox, mises à jour sans reload. */ export default function LiveHtmlPreview({ path = "", title, content = "", compact = false }) { const iframeRef = useRef(null); const lastWrittenRef = useRef(""); const displayTitle = title || (path ? basename(path) : "Aperçu HTML"); const writeContent = useCallback((html) => { const iframe = iframeRef.current; if (!iframe || html == null) return; if (html === lastWrittenRef.current) return; lastWrittenRef.current = html; try { const doc = iframe.contentDocument || iframe.contentWindow?.document; if (!doc) return; doc.open(); doc.write(html); doc.close(); } catch { /* sandbox / cross-origin — ignore */ } }, []); useEffect(() => { writeContent(content || ""); }, [content, writeContent]); const handleIframeLoad = () => { if (content && content !== lastWrittenRef.current) { writeContent(content); } }; return (
{displayTitle}