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