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

main
tsx 110 lines 4.14 KB
Raw
1 import type { ScopedThreadRef } from "@t3tools/contracts";
2 import {
3 HTML_RENDER_COLUMN_WIDTH,
4 htmlRenderFrameHeight,
5 type HtmlRenderReference,
6 } from "@t3tools/shared/htmlRender";
7 import { useGT } from "gt-react";
8 import { MinusIcon, PlusIcon, XIcon } from "lucide-react";
9 import { useRef, useState } from "react";
10
11 import { observeResize } from "~/lib/observeResize";
12
13 import { HtmlRenderDocument } from "../files/BrowserDocumentFrame";
14 import { Button } from "../ui/button";
15 import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
16
17 export function HtmlRenderDialog(props: {
18 readonly src: string;
19 readonly htmlRender: HtmlRenderReference;
20 readonly threadRef?: ScopedThreadRef | undefined;
21 readonly onClose: () => void;
22 }) {
23 const gt = useGT();
24 const { title } = props.htmlRender;
25 const [zoom, setZoom] = useState(1);
26 const closeButtonRef = useRef<HTMLButtonElement>(null);
27 const [width, setWidth] = useState(HTML_RENDER_COLUMN_WIDTH);
28 // A callback ref, since the popup can mount after this component's first commit.
29 const measureBox = (box: HTMLDivElement) => {
30 setWidth(box.clientWidth);
31 return observeResize(box, ([entry]) => {
32 // Rounded like clientWidth, so the two writes agree at fractional widths.
33 if (entry) setWidth(Math.round(entry.contentRect.width));
34 });
35 };
36 const [contentHeight, setContentHeight] = useState<number>();
37 // Zoom divides the page's layout width, so the page reflows to fill the card
38 // at every level, and the frame is as tall as the page so only the card scrolls.
39 const height = htmlRenderFrameHeight(props.htmlRender, width / zoom, contentHeight);
40 return (
41 <Dialog open onOpenChange={(open) => !open && props.onClose()}>
42 <DialogPopup
43 variant="media"
44 showCloseButton={false}
45 bottomStickOnMobile={false}
46 className="row-start-1"
47 initialFocus={closeButtonRef}
48 >
49 <DialogTitle className="sr-only">{gt("Expanded {title} preview", { title })}</DialogTitle>
50 {/* 1144px is the widest the server measures a page; pages are written for the reply column and stretch badly beyond it. */}
51 <div className="relative w-[min(92vw,1176px)]">
52 <Button
53 ref={closeButtonRef}
54 aria-label={gt("Close")}
55 size="icon-xs"
56 variant="media-close"
57 className="absolute right-0 -top-10 z-20"
58 onClick={props.onClose}
59 >
60 <XIcon />
61 </Button>
62 <div className="max-h-[min(86vh,calc(100vh-160px))] overflow-y-auto overscroll-contain rounded-lg bg-background p-4 shadow-2xl ring-1 ring-border/70">
63 <div ref={measureBox}>
64 <div style={{ zoom, height }}>
65 <HtmlRenderDocument
66 src={props.src}
67 title={title}
68 threadRef={props.threadRef}
69 className="block size-full"
70 onContentHeight={setContentHeight}
71 />
72 </div>
73 </div>
74 </div>
75 <div className="mt-2 flex min-w-0 items-center justify-center gap-3 text-xs text-white/80">
76 <span className="truncate">{title}</span>
77 <div className="flex shrink-0 items-center gap-1">
78 <Button
79 aria-label={gt("Zoom out")}
80 size="icon-sm"
81 variant="overlay"
82 disabled={zoom === 0.5}
83 onClick={() => setZoom((value) => value - 0.25)}
84 >
85 <MinusIcon />
86 </Button>
87 <Button
88 aria-label={gt("Reset zoom")}
89 size="sm"
90 variant="overlay"
91 onClick={() => setZoom(1)}
92 >
93 {Math.round(zoom * 100)}%
94 </Button>
95 <Button
96 aria-label={gt("Zoom in")}
97 size="icon-sm"
98 variant="overlay"
99 disabled={zoom === 2}
100 onClick={() => setZoom((value) => value + 0.25)}
101 >
102 <PlusIcon />
103 </Button>
104 </div>
105 </div>
106 </div>
107 </DialogPopup>
108 </Dialog>
109 );
110 }