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