Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

HtmlRenderFrame.tsx

130 lines5.1 KB

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 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- holds the parent's fullscreen pin while expanded and releases it on collapse or unmount
63 useEffect(() => {
64 if (!expanded) return;
65 onFullscreenChange?.(true);
66 return () => onFullscreenChange?.(false);
67 }, [expanded, onFullscreenChange]);
68​
69 return (
70 <div ref={observeBox} className="group/html-render relative" style={{ height }}>
71 {src !== null ? (
72 <>
73 <HtmlRenderDocument
74 src={src}
75 title={title}
76 threadRef={props.threadRef}
77 className="block size-full"
78 onContentHeight={setContentHeight}
79 onFullscreenChange={onFullscreenChange}
80 selectionBridge
81 />
82 <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">
83 <Tooltip>
84 <TooltipTrigger
85 render={
86 <Button
87 aria-label="Open full screen"
88 size="icon-xs"
89 variant="glass"
90 onClick={async () => {
91 const url =
92 loaded !== null && loaded.expiresAt - Date.now() > MIN_URL_LIFE_MS
93 ? src
94 : await refresh().catch(() => null);
95 if (url !== null) setExpandedSrc(url);
96 else
97 toastManager.add({
98 type: "error",
99 title: "Page unavailable",
100 description: "Reconnect to the environment and try again.",
101 });
102 }}
103 />
104 }
105 >
106 <Maximize2Icon className="size-3.5" />
107 </TooltipTrigger>
108 <TooltipPopup side="left">Open full screen</TooltipPopup>
109 </Tooltip>
110 </div>
111 {expandedSrc !== null ? (
112 <HtmlRenderDialog
113 src={expandedSrc}
114 title={title}
115 threadRef={props.threadRef}
116 onClose={() => setExpandedSrc(null)}
117 />
118 ) : null}
119 </>
120 ) : failed ? (
121 <p
122 data-html-render-error
123 className="flex size-full items-center justify-center text-muted-foreground text-xs"
124 >
125 Unable to load {title}
126 </p>
127 ) : null}
128 </div>
129 );