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 type RefObject,
15 useState,
16} from "react";
17
18import { Button } from "~/components/ui/button";
19import { InputGroup, InputGroupAddon, InputGroupInput } from "~/components/ui/input-group";
20import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
21import { cn } from "~/lib/utils";
22
23interface Props {
24 url: string;
25 loading: boolean;
26 canGoBack: boolean;
27 canGoForward: boolean;
28 refreshDisabled: boolean;
29 inputDisabled?: boolean | undefined;
30 /** Owned by the caller so its shortcut handler can focus the address bar directly. */
31 urlInputRef: RefObject<HTMLInputElement | null>;
32 onBack: () => void;
33 onForward: () => void;
34 onRefresh: () => void;
35 onStopLoading: () => void;
36 onSubmit: (url: string) => void;
37 onOpenInBrowser?: (() => void) | undefined;
38 onCapture?: ((record: boolean) => void) | undefined;
39 captureDisabled?: boolean | undefined;
40 recording?: boolean | undefined;
41 onPictureInPicture?: (() => void) | undefined;
42 pictureInPicture?: boolean | undefined;
43 pictureInPictureDisabled?: boolean | undefined;
44 onPickElement?: (() => void) | undefined;
45 pickActive?: boolean | undefined;
46 pickDisabled?: boolean | undefined;
47 pickDisabledReason?: string | undefined;
48 trailingActions?: ReactNode;
49 leadingActions?: ReactNode;
50}
51
52const NOOP = () => {};
53
54export function PreviewChromeRow({
55 url,
56 loading,
57 canGoBack,
58 canGoForward,
59 refreshDisabled,
60 inputDisabled,
61 urlInputRef: inputRef,
62 onBack,
63 onForward,
64 onRefresh,
65 onStopLoading,
66 onSubmit,
67 onOpenInBrowser,
68 onCapture,
69 captureDisabled,
70 recording,
71 onPictureInPicture,
72 pictureInPicture,
73 pictureInPictureDisabled,
74 onPickElement,
75 pickActive,
76 pickDisabled,
77 pickDisabledReason,
78 trailingActions,
79 leadingActions,
80}: Props) {
81 const [draft, setDraft] = useState(url);
82 const [inputFocused, setInputFocused] = useState(false);
83
84 const submit = (event?: FormEvent | KeyboardEvent) => {
85 event?.preventDefault();
86 const next = draft.trim();
87 if (next.length === 0) return;
88 onSubmit(next);
89 inputRef.current?.blur();
90 };
91
92 return (
93 <div className="relative">
94 <form
95 onSubmit={submit}
96 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"
97 data-surface-subheader
98 >
99 <div className="flex items-center gap-0.5" role="group" aria-label="Navigation">
100 <Tooltip>
101 <TooltipTrigger
102 render={
103 <Button
104 variant="ghost"
105 size="icon-xs"
106 onClick={canGoBack ? onBack : NOOP}
107 disabled={!canGoBack}
108 aria-label="Back"
109 type="button"
110 />
111 }
112 >
113 <ArrowLeft />
114 </TooltipTrigger>
115 <TooltipPopup>Back</TooltipPopup>
116 </Tooltip>
117 <Tooltip>
118 <TooltipTrigger
119 render={
120 <Button
121 variant="ghost"
122 size="icon-xs"
123 onClick={canGoForward ? onForward : NOOP}
124 disabled={!canGoForward}
125 aria-label="Forward"
126 type="button"
127 />
128 }
129 >
130 <ArrowRight />
131 </TooltipTrigger>
132 <TooltipPopup>Forward</TooltipPopup>
133 </Tooltip>
134 <Tooltip>
135 <TooltipTrigger
136 render={
137 <Button
138 variant="ghost"
139 size="icon-xs"
140 onClick={refreshDisabled ? NOOP : loading ? onStopLoading : onRefresh}
141 disabled={refreshDisabled}
142 aria-label={loading ? "Stop" : "Refresh"}
143 type="button"
144 />
145 }
146 >
147 <RefreshIcon refreshing={loading} />
148 </TooltipTrigger>
149 <TooltipPopup>{loading ? "Stop" : "Refresh"}</TooltipPopup>
150 </Tooltip>
151 </div>
152
153 {leadingActions}
154
155 <InputGroup variant="ghost" className="group/address h-7 flex-1">
156 <Tooltip>
157 <TooltipTrigger
158 render={
159 <InputGroupInput
160 ref={inputRef}
161 value={inputFocused ? draft : url}
162 onChange={(event) => setDraft(event.target.value)}
163 onFocus={() => {
164 setDraft(url);
165 setInputFocused(true);
166 queueMicrotask(() => inputRef.current?.select());
167 }}
168 onBlur={() => {
169 setInputFocused(false);
170 }}
171 onKeyDown={(event) => {
172 if (event.nativeEvent.isComposing || event.keyCode === 229) {
173 // A trusted Enter would still submit the form implicitly.
174 if (event.key === "Enter") event.preventDefault();
175 return;
176 }
177 if (event.key === "Enter") submit(event);
178 if (event.key === "Escape") {
179 event.preventDefault();
180 setDraft(url);
181 inputRef.current?.blur();
182 }
183 }}
184 placeholder="Search or enter URL"
185 aria-label="Search or enter URL"
186 data-workspace-focus-default
187 spellCheck={false}
188 disabled={inputDisabled}
189 data-preview-url-input
190 size="sm"
191 />
192 }
193 />
194 </Tooltip>
195 {onOpenInBrowser && !inputFocused ? (
196 <InputGroupAddon align="inline-end">
197 {}
198 <span className="pointer-events-none flex opacity-0 transition-opacity focus-within:pointer-events-auto focus-within:opacity-100 group-hover/address:pointer-events-auto group-hover/address:opacity-100">
199 <Tooltip>
200 <TooltipTrigger
201 render={
202 <Button
203 variant="ghost"
204 size="icon-xs"
205 onClick={onOpenInBrowser}
206 aria-label="Open in system browser"
207 type="button"
208 />
209 }
210 >
211 <ExternalLink />
212 </TooltipTrigger>
213 <TooltipPopup>Open in system browser</TooltipPopup>
214 </Tooltip>
215 </span>
216 </InputGroupAddon>
217 ) : null}
218 </InputGroup>
219
220 {onPickElement ? (
221 <Tooltip>
222 <TooltipTrigger
223 render={
224 <Button
225 variant={pickActive ? "secondary" : "ghost"}
226 size="icon-xs"
227 onClick={onPickElement}
228 disabled={pickDisabled}
229 aria-label={pickActive ? "Cancel annotation" : "Annotate preview"}
230 aria-pressed={pickActive ? "true" : "false"}
231 type="button"
232 />
233 }
234 >
235 <MousePointerClick className={cn(pickActive && "text-primary")} />
236 </TooltipTrigger>
237 <TooltipPopup>
238 {pickDisabled && pickDisabledReason
239 ? pickDisabledReason
240 : pickActive
241 ? "Cancel annotation (Esc)"
242 : "Annotate elements, regions, and drawings"}
243 </TooltipPopup>
244 </Tooltip>
245 ) : null}
246 {onCapture ? (
247 <Tooltip>
248 <TooltipTrigger
249 render={
250 <Button
251 variant={recording ? "secondary" : "ghost"}
252 size="icon-xs"
253 onClick={(event) => onCapture(event.shiftKey)}
254 aria-label={recording ? "Stop recording" : "Capture screenshot"}
255 type="button"
256 className="relative"
257 disabled={captureDisabled}
258 />
259 }
260 >
261 <Camera className={cn(recording && "text-destructive")} />
262 {recording ? (
263 <span className="absolute right-0.5 top-0.5 size-1.5 motion-safe:animate-status-pulse rounded-full bg-destructive" />
264 ) : null}
265 </TooltipTrigger>
266 <TooltipPopup>
267 {recording ? "Stop recording" : "Screenshot · Shift-click to record"}
268 </TooltipPopup>
269 </Tooltip>
270 ) : null}
271 {onPictureInPicture ? (
272 <Tooltip>
273 <TooltipTrigger
274 render={
275 <Button
276 variant={pictureInPicture ? "secondary" : "ghost"}
277 size="icon-xs"
278 onClick={onPictureInPicture}
279 aria-label={
280 pictureInPicture ? "Close floating preview" : "Float preview over chat"
281 }
282 aria-pressed={pictureInPicture ? "true" : "false"}
283 type="button"
284 disabled={pictureInPictureDisabled}
285 />
286 }
287 >
288 <PictureInPicture2 className={cn(pictureInPicture && "text-primary")} />
289 </TooltipTrigger>
290 <TooltipPopup>
291 {pictureInPicture ? "Close floating preview" : "Float preview over chat"}
292 </TooltipPopup>
293 </Tooltip>
294 ) : null}
295 {trailingActions}
296 </form>
297 <div
298 aria-hidden
299 data-loading={loading}
300 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"
301 style={{ boxShadow: "0 0 6px 1px var(--color-ring)" }}
302 />
303 </div>
304 );
305}