Open History to see the last commit
1import { GUEST_RENDER_ROUTE, GUEST_SHARE_PARAM } from "@t3tools/contracts";
2import {
3 type HtmlRenderReference,
4 HTML_RENDER_COLUMN_WIDTH,
5 htmlRenderFrameHeight,
6 htmlRenderResult,
7 htmlRenderThemeFragment,
8 htmlRenderThemeMessage,
9 readHtmlRenderContentHeight,
10 readHtmlRenderLinkRequest,
11} from "@t3tools/shared/htmlRender";
12import { useEffect, useLayoutEffect, useRef, useState } from "react";
13
14import { useHtmlRenderTheme } from "~/hooks/useHtmlRenderTheme";
15import { observeResize } from "~/lib/observeResize";
16
17/**
18 * A visual the agent published, as the guest sees it. The server sends it with a sandbox CSP and
19 * the frame is sandboxed too, so the page runs scripts in an opaque origin. It can size itself
20 * and ask to open a web link; it cannot reach the guest's session.
21 */
22export function GuestHtmlRender({
23 itemId,
24 reference,
25 shareId,
26}: {
27 readonly itemId: string;
28 readonly reference: HtmlRenderReference;
29 readonly shareId: string;
30}) {
31 const theme = useHtmlRenderTheme();
32 const boxRef = useRef<HTMLDivElement>(null);
33 const frameRef = useRef<HTMLIFrameElement>(null);
34 const [width, setWidth] = useState(HTML_RENDER_COLUMN_WIDTH);
35 const [contentHeight, setContentHeight] = useState<number>();
36 // The theme rides in the fragment for the first paint; later changes are posted.
37 const [src] = useState(
38 () =>
39 `${GUEST_RENDER_ROUTE}?${GUEST_SHARE_PARAM}=${encodeURIComponent(shareId)}&item=${encodeURIComponent(itemId)}${htmlRenderThemeFragment(theme)}`,
40 );
41 useLayoutEffect(() => {
42 const box = boxRef.current;
43 if (!box) return;
44 setWidth(box.clientWidth);
45 return observeResize(box, ([entry]) => {
46 if (entry) setWidth(entry.contentRect.width);
47 });
48 }, []);
49 useEffect(() => {
50 frameRef.current?.contentWindow?.postMessage(htmlRenderThemeMessage(theme), "*");
51 }, [theme]);
52 useEffect(() => {
53 const onMessage = (event: MessageEvent) => {
54 const frame = frameRef.current;
55 if (frame === null || event.source !== frame.contentWindow) return;
56 const height = readHtmlRenderContentHeight(event.data);
57 if (height !== undefined) {
58 setContentHeight(height);
59 return;
60 }
61 // Only while the reader is using this frame, so a page cannot open links on its own.
62 const link = readHtmlRenderLinkRequest(event.data);
63 if (
64 link === undefined ||
65 document.activeElement !== frame ||
66 navigator.userActivation?.isActive === false
67 ) {
68 return;
69 }
70 window.open(link.url, "_blank", "noopener,noreferrer");
71 frame.contentWindow?.postMessage(htmlRenderResult(link.id), "*");
72 };
73 window.addEventListener("message", onMessage);
74 return () => window.removeEventListener("message", onMessage);
75 }, []);
76
77 return (
78 <div
79 ref={boxRef}
80 style={{
81 height: htmlRenderFrameHeight(reference, width, contentHeight),
82 }}
83 >
84 <iframe
85 ref={frameRef}
86 src={src}
87 title={reference.title}
88 sandbox="allow-scripts allow-forms allow-popups allow-downloads"
89 className="block size-full rounded-lg border border-border"
90 />
91 </div>
92 );
93}