Open History to see the last commit
1import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
2import {
3 HTML_RENDER_COLUMN_WIDTH,
4 htmlRenderFileName,
5 htmlRenderFrameHeight,
6 type HtmlRenderReference,
7} from "@t3tools/shared/htmlRender";
8import { Maximize2Icon } from "lucide-react";
9import { useEffect, useState } from "react";
10
11import { useAssetUrlRefresh } from "~/assets/assetUrls";
12import { useLongLivedAssetUrl } from "~/assets/longLivedAssetUrls";
13
14import { HtmlRenderDocument } from "../files/BrowserDocumentFrame";
15import { Button } from "../ui/button";
16import { toastManager } from "../ui/toast";
17import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
18import { HtmlRenderDialog } from "./HtmlRenderDialog";
19import { observeResize } from "~/lib/observeResize";
20
21const MIN_URL_LIFE_MS = 5 * 60_000;
22
23export function HtmlRenderFrame(props: {
24 readonly environmentId: EnvironmentId;
25 readonly htmlRender: HtmlRenderReference;
26 readonly threadRef: ScopedThreadRef | undefined;
27 readonly onFullscreenChange?: (fullscreen: boolean) => void;
28}) {
29 const { attachmentId, title } = props.htmlRender;
30 const [width, setWidth] = useState(HTML_RENDER_COLUMN_WIDTH);
31 const observeBox = (box: HTMLDivElement) => {
32 setWidth(box.clientWidth);
33 return observeResize(box, ([entry]) => {
34 if (entry) setWidth(entry.contentRect.width);
35 });
36 };
37 // Client fonts can exceed the server-measured height and cause nested scrolling.
38 const [contentHeight, setContentHeight] = useState<number>();
39 const height = htmlRenderFrameHeight(props.htmlRender, width, contentHeight);
40 const fileName = htmlRenderFileName(title);
41 const resource = {
42 _tag: "attachment" as const,
43 attachmentId,
44 fileName,
45 mimeType: "text/html",
46 disposition: "inline" as const,
47 };
48 // The frame keeps the first URL it loads, so later mints do not reload the page.
49 const [loaded, setLoaded] = useState<{ src: string; expiresAt: number } | null>(null);
50 const assetUrl = useLongLivedAssetUrl(loaded === null ? props.environmentId : null, resource);
51 if (loaded === null && assetUrl._tag === "Success") {
52 setLoaded({ src: assetUrl.url, expiresAt: assetUrl.expiresAt });
53 }
54 const src = loaded?.src ?? null;
55 const failed = loaded === null && assetUrl._tag === "Failure";
56 const refresh = useAssetUrlRefresh(props.environmentId, resource);
57 const [expandedSrc, setExpandedSrc] = useState<string | null>(null);
58 const { onFullscreenChange } = props;
59 // Tied to the viewer's lifetime: unmounting while expanded must still release
60 // the timeline's follow pin, which a close handler alone never reaches.
61 const expanded = expandedSrc !== null;
62 useEffect(() => {
63 if (!expanded) return;
64 onFullscreenChange?.(true);
65 return () => onFullscreenChange?.(false);
66 }, [expanded, onFullscreenChange]);
67
68 return (
69 <div ref={observeBox} className="group/html-render relative" style={{ height }}>
70 {src !== null ? (
71 <>
72 <HtmlRenderDocument
73 src={src}
74 title={title}
75 threadRef={props.threadRef}
76 className="block size-full"
77 onContentHeight={setContentHeight}
78 onFullscreenChange={onFullscreenChange}
79 />
80 <div className="absolute end-2 top-2 flex gap-1 opacity-0 transition-opacity duration-150 focus-within:opacity-100 group-hover/html-render:opacity-100 pointer-coarse:opacity-100">
81 <Tooltip>
82 <TooltipTrigger
83 render={
84 <Button
85 aria-label="Open full screen"
86 size="icon-xs"
87 variant="glass"
88 onClick={async () => {
89 const url =
90 loaded !== null && loaded.expiresAt - Date.now() > MIN_URL_LIFE_MS
91 ? src
92 : await refresh().catch(() => null);
93 if (url !== null) setExpandedSrc(url);
94 else
95 toastManager.add({
96 type: "error",
97 title: "Page unavailable",
98 description: "Reconnect to the environment and try again.",
99 });
100 }}
101 />
102 }
103 >
104 <Maximize2Icon className="size-3.5" />
105 </TooltipTrigger>
106 <TooltipPopup side="left">Open full screen</TooltipPopup>
107 </Tooltip>
108 </div>
109 {expandedSrc !== null ? (
110 <HtmlRenderDialog
111 src={expandedSrc}
112 title={title}
113 threadRef={props.threadRef}
114 onClose={() => setExpandedSrc(null)}
115 />
116 ) : null}
117 </>
118 ) : failed ? (
119 <p className="flex size-full items-center justify-center text-muted-foreground text-xs">
120 Unable to load {title}
121 </p>
122 ) : null}
123 </div>
124 );
125}