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

main
tsx 333 lines 10.5 KB
Raw
1 import type { DeviceHostSummary, DevicePlatform } from "@t3tools/contracts";
2 import {
3 Camera,
4 ChevronLeft,
5 Home,
6 Keyboard,
7 Box,
8 Maximize,
9 MoreHorizontal,
10 PictureInPicture2,
11 Power,
12 RotateCcw,
13 SlidersHorizontal,
14 Smartphone,
15 Square,
16 Type,
17 X,
18 MoonIcon,
19 SunIcon,
20 } from "lucide-react";
21 import { T, useGT, useMessages, msg } from "gt-react";
22 import type { ReactNode } from "react";
23 import { Button } from "~/components/ui/button";
24 import {
25 Menu,
26 MenuItem,
27 MenuPopup,
28 MenuSeparator,
29 MenuTrigger,
30 MenuRadioGroup,
31 MenuRadioItem,
32 MenuRadioItemIndicator,
33 } from "~/components/ui/menu";
34 import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
35 import type { DeviceStreamHandle, DeviceViewControls } from "./DeviceStreamView";
36 import type { DeviceControls } from "./useDeviceControls";
37
38 export function DeviceControlsRail(props: {
39 platform: DevicePlatform;
40 hostKind: DeviceHostSummary["kind"] | undefined;
41 handle: DeviceStreamHandle | null;
42 view: DeviceViewControls;
43 controls: DeviceControls;
44 screenshotPending: boolean;
45 onScreenshot: () => void;
46 toolsOpen: boolean;
47 onTools: () => void;
48 onFloat: () => void;
49 onClose: () => void;
50 onPowerOff: () => void;
51 }) {
52 const { view, handle, controls } = props;
53 const popupSide = "left";
54 const settings = controls.detail?.settings;
55 const inputDisabled = !handle?.inputConnected;
56 const runsHostCommands = props.hostKind === "local" || props.hostKind === "ssh";
57 const nextAppearance = settings?.appearance === "dark" ? "light" : "dark";
58 const gt = useGT();
59 const m = useMessages();
60 return (
61 <aside
62 aria-label={gt("Device controls")}
63 data-layout="rail"
64 className="pointer-events-none absolute inset-y-0 right-0 z-10 flex w-14 flex-col items-center gap-2 overflow-y-auto [justify-content:safe_center] py-3 pr-2 [scrollbar-width:none]"
65 >
66 <div className="pointer-events-auto flex shrink-0 flex-col items-center gap-1 overflow-y-auto rounded-full border border-border/50 bg-background/80 p-2 shadow-sm [scrollbar-width:none]">
67 <RailButton
68 tooltipSide={popupSide}
69 label={gt("Home", { $context: "device home button" })}
70 disabled={inputDisabled}
71 onClick={() => handle?.pressButton("home")}
72 >
73 <Home />
74 </RailButton>
75 {props.platform === "android" ? (
76 <>
77 <RailButton
78 tooltipSide={popupSide}
79 label={gt("Back", { $context: "device back button" })}
80 disabled={inputDisabled}
81 onClick={() => handle?.pressButton("back")}
82 >
83 <ChevronLeft />
84 </RailButton>
85 <RailButton
86 tooltipSide={popupSide}
87 label={gt("Recents", { $context: "Android recent apps button" })}
88 disabled={inputDisabled}
89 onClick={() => handle?.pressButton("recents")}
90 >
91 <Square />
92 </RailButton>
93 {runsHostCommands ? (
94 <Menu>
95 <MenuTrigger
96 render={
97 <Button
98 size="icon-sm"
99 variant="ghost"
100 aria-label={gt("Rotate device")}
101 disabled={controls.disabled}
102 />
103 }
104 >
105 <RotateCcw />
106 </MenuTrigger>
107 <MenuPopup side={popupSide}>
108 <MenuItem
109 onClick={() => void controls.act({ type: "setOrientation", value: "portrait" })}
110 >
111 <T>Portrait</T>
112 </MenuItem>
113 <MenuItem
114 onClick={() =>
115 void controls.act({ type: "setOrientation", value: "landscape_left" })
116 }
117 >
118 <T>Landscape</T>
119 </MenuItem>
120 </MenuPopup>
121 </Menu>
122 ) : null}
123 </>
124 ) : props.hostKind === "simlock" ? null : (
125 <RailButton
126 tooltipSide={popupSide}
127 label={gt("Rotate device")}
128 disabled={inputDisabled || !!view.keyboard?.attached}
129 onClick={() => handle?.rotate()}
130 >
131 <RotateCcw />
132 </RailButton>
133 )}
134 {runsHostCommands ? (
135 <>
136 <RailDivider />
137 <RailButton
138 tooltipSide={popupSide}
139 label={
140 nextAppearance === "light"
141 ? gt("Switch device to light mode")
142 : gt("Switch device to dark mode")
143 }
144 disabled={controls.disabled || !settings?.appearance}
145 onClick={() => void controls.act({ type: "setAppearance", value: nextAppearance })}
146 >
147 {settings?.appearance === "dark" ? <SunIcon /> : <MoonIcon />}
148 </RailButton>
149 <Menu>
150 <MenuTrigger
151 render={
152 <Button
153 size="icon-sm"
154 variant="ghost"
155 aria-label={gt("Device text size")}
156 title={gt("Device text size")}
157 disabled={controls.disabled || !settings?.textSize}
158 />
159 }
160 >
161 <Type />
162 </MenuTrigger>
163 <MenuPopup side={popupSide} className="min-w-40">
164 <MenuRadioGroup
165 value={settings?.textSize ?? ""}
166 onValueChange={(value) => {
167 if (
168 value === "small" ||
169 value === "default" ||
170 value === "large" ||
171 value === "extra-large"
172 )
173 void controls.act({ type: "setTextSize", value });
174 }}
175 >
176 {(
177 [
178 ["small", msg("Small")],
179 ["default", msg("Default")],
180 ["large", msg("Large")],
181 ["extra-large", msg("Extra large")],
182 ] as const
183 ).map(([value, label]) => (
184 <MenuRadioItem key={value} value={value}>
185 <span className="flex items-center gap-2">
186 <span className="flex-1">{m(label)}</span>
187 <MenuRadioItemIndicator />
188 </span>
189 </MenuRadioItem>
190 ))}
191 </MenuRadioGroup>
192 </MenuPopup>
193 </Menu>
194 <RailButton
195 tooltipSide={popupSide}
196 label={gt("Device tools")}
197 pressed={props.toolsOpen}
198 onClick={props.onTools}
199 >
200 <SlidersHorizontal />
201 </RailButton>
202 </>
203 ) : null}
204 {props.hostKind === "simlock" ? null : (
205 <RailButton
206 tooltipSide={popupSide}
207 label={props.screenshotPending ? gt("Capturing screenshot") : gt("Save screenshot")}
208 disabled={!view.streaming || props.screenshotPending}
209 onClick={props.onScreenshot}
210 >
211 <Camera />
212 </RailButton>
213 )}
214 <Menu>
215 <MenuTrigger
216 render={
217 <Button
218 size="icon-sm"
219 variant="ghost"
220 aria-label={gt("More device actions")}
221 title={gt("More device actions")}
222 />
223 }
224 >
225 <MoreHorizontal />
226 </MenuTrigger>
227 <MenuPopup side={popupSide} align="end">
228 <MenuItem onClick={props.onFloat}>
229 <PictureInPicture2 />
230 <T>Float device over chat</T>
231 </MenuItem>
232 <MenuItem onClick={props.onClose}>
233 <X />
234 <T>Close device panel</T>
235 </MenuItem>
236 <MenuSeparator />
237 <MenuItem variant="destructive" onClick={props.onPowerOff}>
238 <Power />
239 {props.hostKind === "simlock"
240 ? gt("Return device to the pool")
241 : gt("Power off device")}
242 </MenuItem>
243 </MenuPopup>
244 </Menu>
245 <RailDivider />
246 <RailButton
247 tooltipSide={popupSide}
248 label={gt("3D view")}
249 pressed={view.phone}
250 disabled={!view.streaming || !!view.phoneUnavailableReason}
251 description={view.phoneUnavailableReason ?? undefined}
252 onClick={view.showPhone}
253 >
254 <Box />
255 </RailButton>
256 <RailButton
257 tooltipSide={popupSide}
258 label={gt("Flat view")}
259 pressed={!view.phone}
260 disabled={!view.streaming}
261 onClick={view.showFlat}
262 >
263 <Smartphone />
264 </RailButton>
265 {view.keyboard ? (
266 <RailButton
267 tooltipSide={popupSide}
268 label={
269 view.keyboard.attached ? gt("Detach Magic Keyboard") : gt("Attach Magic Keyboard")
270 }
271 pressed={view.keyboard.attached}
272 onClick={view.keyboard.toggle}
273 >
274 <Keyboard />
275 </RailButton>
276 ) : null}
277 {view.phone ? (
278 <RailButton
279 tooltipSide={popupSide}
280 label={gt("Restore 3D view")}
281 onClick={view.resetView}
282 >
283 <Maximize />
284 </RailButton>
285 ) : null}
286 </div>
287 {view.foldingControls}
288 </aside>
289 );
290 }
291
292 function RailDivider() {
293 return <div aria-hidden className="my-1 h-px w-5 shrink-0 bg-border/70" />;
294 }
295
296 function RailButton(props: {
297 tooltipSide: "left" | "bottom";
298 label: string;
299 description?: string | undefined;
300 disabled?: boolean;
301 pressed?: boolean;
302 onClick: () => void;
303 children: ReactNode;
304 }) {
305 const button = (
306 <Button
307 size="icon-sm"
308 variant={props.pressed ? "secondary" : "ghost"}
309 aria-label={props.label}
310 aria-pressed={props.pressed}
311 disabled={props.disabled}
312 onClick={props.onClick}
313 >
314 {props.children}
315 </Button>
316 );
317 return (
318 <Tooltip>
319 {props.disabled && props.description ? (
320 <TooltipTrigger
321 render={
322 <span className="inline-flex" tabIndex={0} role="group" aria-label={props.label} />
323 }
324 >
325 {button}
326 </TooltipTrigger>
327 ) : (
328 <TooltipTrigger render={button} />
329 )}
330 <TooltipPopup side={props.tooltipSide}>{props.description ?? props.label}</TooltipPopup>
331 </Tooltip>
332 );
333 }