import type { ScopedThreadRef } from "@t3tools/contracts"; import { HTML_RENDER_COLUMN_WIDTH, htmlRenderFitHeight, type HtmlRenderReference, } from "@t3tools/shared/htmlRender"; import { useGT } from "gt-react"; import { MinusIcon, PlusIcon, XIcon } from "lucide-react"; import { useRef, useState } from "react"; import { observeResize } from "~/lib/observeResize"; import { HtmlRenderDocument } from "../files/BrowserDocumentFrame"; import { Button } from "../ui/button"; import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog"; export function HtmlRenderDialog(props: { readonly src: string; readonly htmlRender: HtmlRenderReference; readonly threadRef?: ScopedThreadRef | undefined; readonly onClose: () => void; }) { const gt = useGT(); const { title } = props.htmlRender; const [zoom, setZoom] = useState(1); const closeButtonRef = useRef(null); const [width, setWidth] = useState(HTML_RENDER_COLUMN_WIDTH); // A callback ref, since the popup can mount after this component's first commit. const measureBox = (box: HTMLDivElement) => { setWidth(box.clientWidth); return observeResize(box, ([entry]) => { // Rounded like clientWidth, so the two writes agree at fractional widths. if (entry) setWidth(Math.round(entry.contentRect.width)); }); }; const [contentHeight, setContentHeight] = useState(); // Zoom divides the page's layout width, so the page reflows to fill the card // at every level, and the frame is as tall as the page so only the card scrolls. const height = htmlRenderFitHeight(props.htmlRender, width / zoom, contentHeight); return ( !open && props.onClose()}> {gt("Expanded {title} preview", { title })} {/* 1144px is the widest the server measures a page; pages are written for the reply column and stretch badly beyond it. */}
{title}
); }