The best way to control Claude Code, Codex and more from any computer or phone

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