The best way to control Claude Code, Codex and more from any computer or phone

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