The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | HTML_RENDER_COLUMN_WIDTH, |
| 4 | htmlRenderFileName, |
| 5 | htmlRenderFrameHeight, |
| 6 | type HtmlRenderReference, |
| 7 | } from "@t3tools/shared/htmlRender"; |
| 8 | import { T, useGT, Var } from "gt-react"; |
| 9 | import { Maximize2Icon } from "lucide-react"; |
| 10 | import { useEffect, useState } from "react"; |
| 11 | |
| 12 | import { useAssetUrlRefresh } from "~/assets/assetUrls"; |
| 13 | import { useLongLivedAssetUrl } from "~/assets/longLivedAssetUrls"; |
| 14 | |
| 15 | import { HtmlRenderDocument } from "../files/BrowserDocumentFrame"; |
| 16 | import { Button } from "../ui/button"; |
| 17 | import { toastManager } from "../ui/toast"; |
| 18 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 19 | import { HtmlRenderDialog } from "./HtmlRenderDialog"; |
| 20 | import { observeResize } from "~/lib/observeResize"; |
| 21 | |
| 22 | const MIN_URL_LIFE_MS = 5 * 60_000; |
| 23 | const MAX_MEASURED_CONTENT_HEIGHTS = 200; |
| 24 | |
| 25 | // The height each render's page last reported, so a remounted frame opens at it before its page loads. |
| 26 | const measuredContentHeights = new Map<string, number>(); |
| 27 | |
| 28 | function rememberContentHeight(attachmentId: string, height: number) { |
| 29 | measuredContentHeights.delete(attachmentId); |
| 30 | measuredContentHeights.set(attachmentId, height); |
| 31 | if (measuredContentHeights.size > MAX_MEASURED_CONTENT_HEIGHTS) { |
| 32 | const oldest = measuredContentHeights.keys().next().value; |
| 33 | if (oldest !== undefined) measuredContentHeights.delete(oldest); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | export function HtmlRenderFrame(props: { |
| 38 | readonly environmentId: EnvironmentId; |
| 39 | readonly htmlRender: HtmlRenderReference; |
| 40 | readonly threadRef: ScopedThreadRef | undefined; |
| 41 | readonly onFullscreenChange?: (fullscreen: boolean) => void; |
| 42 | }) { |
| 43 | const gt = useGT(); |
| 44 | const { attachmentId, title } = props.htmlRender; |
| 45 | const [width, setWidth] = useState(HTML_RENDER_COLUMN_WIDTH); |
| 46 | const observeBox = (box: HTMLDivElement) => { |
| 47 | setWidth(box.clientWidth); |
| 48 | return observeResize(box, ([entry]) => { |
| 49 | if (entry) setWidth(entry.contentRect.width); |
| 50 | }); |
| 51 | }; |
| 52 | // Client fonts can exceed the server-measured height and cause nested scrolling. |
| 53 | const [contentHeight, setContentHeight] = useState(() => |
| 54 | measuredContentHeights.get(attachmentId), |
| 55 | ); |
| 56 | const height = htmlRenderFrameHeight(props.htmlRender, width, contentHeight); |
| 57 | const fileName = htmlRenderFileName(title); |
| 58 | const resource = { |
| 59 | _tag: "attachment" as const, |
| 60 | attachmentId, |
| 61 | fileName, |
| 62 | mimeType: "text/html", |
| 63 | disposition: "inline" as const, |
| 64 | }; |
| 65 | // The frame keeps the first URL it loads, so later mints do not reload the page. |
| 66 | const [loaded, setLoaded] = useState<{ src: string; expiresAt: number } | null>(null); |
| 67 | const assetUrl = useLongLivedAssetUrl(loaded === null ? props.environmentId : null, resource); |
| 68 | if (loaded === null && assetUrl._tag === "Success") { |
| 69 | setLoaded({ src: assetUrl.url, expiresAt: assetUrl.expiresAt }); |
| 70 | } |
| 71 | const src = loaded?.src ?? null; |
| 72 | const failed = loaded === null && assetUrl._tag === "Failure"; |
| 73 | const refresh = useAssetUrlRefresh(props.environmentId, resource); |
| 74 | const [expandedSrc, setExpandedSrc] = useState<string | null>(null); |
| 75 | const { onFullscreenChange } = props; |
| 76 | // Tied to the viewer's lifetime: unmounting while expanded must still release |
| 77 | // the timeline's follow pin, which a close handler alone never reaches. |
| 78 | const expanded = expandedSrc !== null; |
| 79 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- holds the parent's fullscreen pin while expanded and releases it on collapse or unmount |
| 80 | useEffect(() => { |
| 81 | if (!expanded) return; |
| 82 | onFullscreenChange?.(true); |
| 83 | return () => onFullscreenChange?.(false); |
| 84 | }, [expanded, onFullscreenChange]); |
| 85 | |
| 86 | return ( |
| 87 | <div ref={observeBox} className="group/html-render relative" style={{ height }}> |
| 88 | {src !== null ? ( |
| 89 | <> |
| 90 | <HtmlRenderDocument |
| 91 | src={src} |
| 92 | title={title} |
| 93 | threadRef={props.threadRef} |
| 94 | className="block size-full" |
| 95 | containScroll |
| 96 | onContentHeight={(height) => { |
| 97 | rememberContentHeight(attachmentId, height); |
| 98 | setContentHeight(height); |
| 99 | }} |
| 100 | onFullscreenChange={onFullscreenChange} |
| 101 | /> |
| 102 | <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"> |
| 103 | <Tooltip> |
| 104 | <TooltipTrigger |
| 105 | render={ |
| 106 | <Button |
| 107 | aria-label={gt("Open full screen")} |
| 108 | size="icon-xs" |
| 109 | variant="glass" |
| 110 | onClick={async () => { |
| 111 | const url = |
| 112 | loaded !== null && loaded.expiresAt - Date.now() > MIN_URL_LIFE_MS |
| 113 | ? src |
| 114 | : await refresh().catch(() => null); |
| 115 | if (url !== null) setExpandedSrc(url); |
| 116 | else |
| 117 | toastManager.add({ |
| 118 | type: "error", |
| 119 | title: gt("Page unavailable"), |
| 120 | description: gt("Reconnect to the environment and try again."), |
| 121 | }); |
| 122 | }} |
| 123 | /> |
| 124 | } |
| 125 | > |
| 126 | <Maximize2Icon className="size-3.5" /> |
| 127 | </TooltipTrigger> |
| 128 | <TooltipPopup side="left"> |
| 129 | <T>Open full screen</T> |
| 130 | </TooltipPopup> |
| 131 | </Tooltip> |
| 132 | </div> |
| 133 | {expandedSrc !== null ? ( |
| 134 | <HtmlRenderDialog |
| 135 | src={expandedSrc} |
| 136 | htmlRender={props.htmlRender} |
| 137 | threadRef={props.threadRef} |
| 138 | onClose={() => setExpandedSrc(null)} |
| 139 | /> |
| 140 | ) : null} |
| 141 | </> |
| 142 | ) : failed ? ( |
| 143 | <p className="flex size-full items-center justify-center text-muted-foreground text-xs"> |
| 144 | <T> |
| 145 | Unable to load <Var>{title}</Var> |
| 146 | </T> |
| 147 | </p> |
| 148 | ) : null} |
| 149 | </div> |
| 150 | ); |
| 151 | } |