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