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

HtmlRenderFrame.tsx

126 lines5.0 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 />
81 <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">
82 <Tooltip>
83 <TooltipTrigger
84 render={
85 <Button
86 aria-label="Open full screen"
87 size="icon-xs"
88 variant="glass"
89 onClick={async () => {
90 const url =
91 loaded !== null && loaded.expiresAt - Date.now() > MIN_URL_LIFE_MS
92 ? src
93 : await refresh().catch(() => null);
94 if (url !== null) setExpandedSrc(url);
95 else
96 toastManager.add({
97 type: "error",
98 title: "Page unavailable",
99 description: "Reconnect to the environment and try again.",
100 });
101 }}
102 />
103 }
104 >
105 <Maximize2Icon className="size-3.5" />
106 </TooltipTrigger>
107 <TooltipPopup side="left">Open full screen</TooltipPopup>
108 </Tooltip>
109 </div>
110 {expandedSrc !== null ? (
111 <HtmlRenderDialog
112 src={expandedSrc}
113 htmlRender={props.htmlRender}
114 threadRef={props.threadRef}
115 onClose={() => setExpandedSrc(null)}
116 />
117 ) : null}
118 </>
119 ) : failed ? (
120 <p className="flex size-full items-center justify-center text-muted-foreground text-xs">
121 Unable to load {title}
122 </p>
123 ) : null}
124 </div>
125 );