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

PreviewChromeRow.tsx

323 lines10.5 KB

1import {
2 ArrowLeft,
3 ArrowRight,
4 Camera,
5 ExternalLink,
6 MousePointerClick,
7 PictureInPicture2,
8 RotateCw,
9} from "lucide-react";
10import {
11 type FormEvent,
12 type KeyboardEvent,
13 type ReactNode,
14 useEffect,
15 useRef,
16 useState,
17} from "react";
18​
19import { Button } from "~/components/ui/button";
20import { InputGroup, InputGroupAddon, InputGroupInput } from "~/components/ui/input-group";
21import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
22import { cn } from "~/lib/utils";
23​
24interface Props {
25 url: string;
26 loading: boolean;
27 loadProgress: number;
28 canGoBack: boolean;
29 canGoForward: boolean;
30 refreshDisabled: boolean;
31 inputDisabled?: boolean | undefined;
32 /** Bumping this value re-focuses and selects the URL input. */
33 focusUrlNonce?: number | undefined;
34 onBack: () => void;
35 onForward: () => void;
36 onRefresh: () => void;
37 onSubmit: (url: string) => void;
38 /** When provided, renders an "Open in browser" affordance to the right. */
39 onOpenInBrowser?: (() => void) | undefined;
40 onCapture?: ((record: boolean) => void) | undefined;
41 captureDisabled?: boolean | undefined;
42 recording?: boolean | undefined;
43 onPictureInPicture?: (() => void) | undefined;
44 pictureInPicture?: boolean | undefined;
45 pictureInPictureDisabled?: boolean | undefined;
46 /**
47 * When provided, renders an annotation-mode toggle button to the right of
48 * the URL input. Pressed while annotation mode is active (button shows in `pressed`
49 * state). Disabled in `pickDisabled` mode.
50 */
51 onPickElement?: (() => void) | undefined;
52 pickActive?: boolean | undefined;
53 pickDisabled?: boolean | undefined;
54 /** Optional reason string surfaced in the disabled tooltip. */
55 pickDisabledReason?: string | undefined;
56 /**
57 * Trailing slot rendered after the URL input. Used by the preview view
58 * to mount the three-dot menu (hard reload, devtools, zoom, clear data).
59 */
60 trailingActions?: ReactNode;
62​
63const NOOP = () => {};
64​
65export function PreviewChromeRow({
66 url,
67 loading,
68 loadProgress,
69 canGoBack,
70 canGoForward,
71 refreshDisabled,
72 inputDisabled,
73 focusUrlNonce,
74 onBack,
75 onForward,
76 onRefresh,
77 onSubmit,
78 onOpenInBrowser,
79 onCapture,
80 captureDisabled,
81 recording,
82 onPictureInPicture,
83 pictureInPicture,
84 pictureInPictureDisabled,
85 onPickElement,
86 pickActive,
87 pickDisabled,
88 pickDisabledReason,
89 trailingActions,
90}: Props) {
91 const inputRef = useRef<HTMLInputElement | null>(null);
92 const [draft, setDraft] = useState(url);
93 const [inputFocused, setInputFocused] = useState(false);
94​
95 useEffect(() => {
96 if (focusUrlNonce == null) return;
97 const node = inputRef.current;
98 if (!node) return;
99 node.focus();
100 }, [focusUrlNonce]);
101​
102 const submit = (event?: FormEvent | KeyboardEvent) => {
103 event?.preventDefault();
104 const next = draft.trim();
105 if (next.length === 0) return;
106 onSubmit(next);
107 inputRef.current?.blur();
108 };
109​
110 return (
111 <div className="relative">
112 <form
113 onSubmit={submit}
114 className="flex h-10 min-h-10 shrink-0 items-center gap-1 border-b border-border/60 bg-background px-2 in-data-[preview-panel-mode=inline]:mb-3 in-data-[preview-panel-mode=inline]:h-7 in-data-[preview-panel-mode=inline]:min-h-7 in-data-[preview-panel-mode=inline]:border-b-transparent"
115 data-surface-subheader
116 >
117 <div className="flex items-center gap-0.5" role="group" aria-label="Navigation">
118 <Tooltip>
119 <TooltipTrigger
120 render={
121 <Button
122 variant="ghost"
123 size="icon-xs"
124 onClick={canGoBack ? onBack : NOOP}
125 disabled={!canGoBack}
126 aria-label="Back"
127 type="button"
128 />
129 }
130 >
131 <ArrowLeft />
132 </TooltipTrigger>
133 <TooltipPopup>Back</TooltipPopup>
134 </Tooltip>
135 <Tooltip>
136 <TooltipTrigger
137 render={
138 <Button
139 variant="ghost"
140 size="icon-xs"
141 onClick={canGoForward ? onForward : NOOP}
142 disabled={!canGoForward}
143 aria-label="Forward"
144 type="button"
145 />
146 }
147 >
148 <ArrowRight />
149 </TooltipTrigger>
150 <TooltipPopup>Forward</TooltipPopup>
151 </Tooltip>
152 <Tooltip>
153 <TooltipTrigger
154 render={
155 <Button
156 variant="ghost"
157 size="icon-xs"
158 onClick={refreshDisabled ? NOOP : onRefresh}
159 disabled={refreshDisabled}
160 aria-label={loading ? "Stop" : "Refresh"}
161 type="button"
162 />
163 }
164 >
165 <RotateCw className={cn(loading && "animate-spin")} />
166 </TooltipTrigger>
167 <TooltipPopup>{loading ? "Loading…" : "Refresh"}</TooltipPopup>
168 </Tooltip>
169 </div>
170​
171 <InputGroup variant="ghost" className="group/address h-7 flex-1">
172 <Tooltip>
173 <TooltipTrigger
174 render={
175 <InputGroupInput
176 ref={inputRef}
177 value={inputFocused ? draft : url}
178 className={cn(
179 onOpenInBrowser &&
180 !inputFocused &&
181 "group-hover/address:pe-7 transition-[padding]",
182 )}
183 onChange={(event) => setDraft(event.target.value)}
184 onFocus={() => {
185 setDraft(url);
186 setInputFocused(true);
187 queueMicrotask(() => inputRef.current?.select());
188 }}
189 onBlur={() => {
190 setInputFocused(false);
191 }}
192 onKeyDown={(event) => {
193 if (event.key === "Enter") submit(event);
194 if (event.key === "Escape") {
195 event.preventDefault();
196 setDraft(url);
197 inputRef.current?.blur();
198 }
199 }}
200 placeholder="Search or enter URL"
201 spellCheck={false}
202 disabled={inputDisabled}
203 data-preview-url-input
204 size="sm"
205 />
206 }
207 />
208 </Tooltip>
209 {onOpenInBrowser && !inputFocused ? (
210 <InputGroupAddon
211 align="inline-end"
212 className="pointer-events-none absolute inset-y-0 right-0 opacity-0 transition-opacity group-hover/address:pointer-events-auto group-hover/address:opacity-100"
213 >
214 <Tooltip>
215 <TooltipTrigger
216 render={
217 <Button
218 variant="ghost"
219 size="icon-xs"
220 onClick={onOpenInBrowser}
221 aria-label="Open in system browser"
222 type="button"
223 />
224 }
225 >
226 <ExternalLink />
227 </TooltipTrigger>
228 <TooltipPopup>Open in system browser</TooltipPopup>
229 </Tooltip>
230 </InputGroupAddon>
231 ) : null}
232 </InputGroup>
233​
234 {onPickElement ? (
235 <Tooltip>
236 <TooltipTrigger
237 render={
238 <Button
239 variant={pickActive ? "secondary" : "ghost"}
240 size="icon-xs"
241 onClick={onPickElement}
242 disabled={pickDisabled}
243 aria-label={pickActive ? "Cancel annotation" : "Annotate preview"}
244 aria-pressed={pickActive ? "true" : "false"}
245 type="button"
246 />
247 }
248 >
249 <MousePointerClick className={cn(pickActive && "text-primary")} />
250 </TooltipTrigger>
251 <TooltipPopup>
252 {pickDisabled && pickDisabledReason
253 ? pickDisabledReason
254 : pickActive
255 ? "Cancel annotation (Esc)"
256 : "Annotate elements, regions, and drawings"}
257 </TooltipPopup>
258 </Tooltip>
259 ) : null}
260 {onCapture ? (
261 <Tooltip>
262 <TooltipTrigger
263 render={
264 <Button
265 variant={recording ? "secondary" : "ghost"}
266 size="icon-xs"
267 onClick={(event) => onCapture(event.shiftKey)}
268 aria-label={recording ? "Stop recording" : "Capture screenshot"}
269 type="button"
270 className="relative"
271 disabled={captureDisabled}
272 />
273 }
274 >
275 <Camera className={cn(recording && "text-destructive")} />
276 {recording ? (
277 <span className="absolute right-0.5 top-0.5 size-1.5 animate-status-pulse rounded-full bg-destructive" />
278 ) : null}
279 </TooltipTrigger>
280 <TooltipPopup>
281 {recording ? "Stop recording" : "Screenshot · Shift-click to record"}
282 </TooltipPopup>
283 </Tooltip>
284 ) : null}
285 {onPictureInPicture ? (
286 <Tooltip>
287 <TooltipTrigger
288 render={
289 <Button
290 variant={pictureInPicture ? "secondary" : "ghost"}
291 size="icon-xs"
292 onClick={onPictureInPicture}
293 aria-label={
294 pictureInPicture ? "Close floating preview" : "Float preview over chat"
295 }
296 aria-pressed={pictureInPicture ? "true" : "false"}
297 type="button"
298 disabled={pictureInPictureDisabled}
299 />
300 }
301 >
302 <PictureInPicture2 className={cn(pictureInPicture && "text-primary")} />
303 </TooltipTrigger>
304 <TooltipPopup>
305 {pictureInPicture ? "Close floating preview" : "Float preview over chat"}
306 </TooltipPopup>
307 </Tooltip>
308 ) : null}
309 {trailingActions}
310 </form>
311 {loadProgress > 0 ? (
312 <div
313 aria-hidden
314 className="pointer-events-none absolute bottom-0 left-0 z-10 h-0.5 rounded-r-full bg-primary transition-all duration-150 ease-out"
315 style={{
316 width: `${loadProgress}%`,
317 boxShadow: "0 0 6px 1px var(--color-ring)",
318 }}
319 />
320 ) : null}
321 </div>
322 );