@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 679 lines 22.9 KB
Raw
1 import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess";
2 import type { DevicePermission, DeviceSummary, DeviceTextSize } from "@t3tools/contracts";
3 import { ChevronDown, X } from "lucide-react";
4 import { useAtomValue } from "@effect/atom-react";
5 import { msg, T, useGT, useMessages } from "gt-react";
6 import { useState } from "react";
7
8 import { Button } from "~/components/ui/button";
9 import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "~/components/ui/collapsible";
10 import { Input } from "~/components/ui/input";
11 import {
12 Select,
13 SelectItem,
14 SelectPopup,
15 SelectTrigger,
16 SelectValue,
17 } from "~/components/ui/select";
18 import { Spinner } from "~/components/ui/spinner";
19 import { Switch } from "~/components/ui/switch";
20 import { Toggle, ToggleGroup } from "~/components/ui/toggle-group";
21 import { cn } from "~/lib/utils";
22 import type { DeviceControls } from "./useDeviceControls";
23 import { deviceEventLogAtom } from "./deviceHubAtoms";
24
25 const TEXT_SIZES: ReadonlyArray<{ value: DeviceTextSize; label: string }> = [
26 { value: "small", label: msg("Small") },
27 { value: "default", label: msg("Default") },
28 { value: "large", label: msg("Large") },
29 { value: "extra-large", label: msg("Extra large") },
30 ];
31
32 const COLOR_FILTERS = [
33 { value: "none", label: msg("None") },
34 { value: "grayscale", label: msg("Grayscale") },
35 { value: "red-green", label: msg("Red / green (protanopia)") },
36 { value: "green-red", label: msg("Green / red (deuteranopia)") },
37 { value: "blue-yellow", label: msg("Blue / yellow (tritanopia)") },
38 ] as const;
39
40 const ORIENTATIONS = [
41 { value: "portrait", label: msg("Portrait") },
42 { value: "landscape_left", label: msg("Landscape left") },
43 { value: "portrait_upside_down", label: msg("Upside down") },
44 { value: "landscape_right", label: msg("Landscape right") },
45 ] as const;
46
47 const IOS_PERMISSIONS: ReadonlyArray<{ value: DevicePermission; label: string }> = [
48 { value: "camera", label: msg("Camera") },
49 { value: "microphone", label: msg("Microphone") },
50 { value: "photos", label: msg("Photos") },
51 { value: "contacts", label: msg("Contacts") },
52 { value: "calendar", label: msg("Calendar") },
53 { value: "reminders", label: msg("Reminders") },
54 { value: "location", label: msg("Location") },
55 { value: "notifications", label: msg("Notifications") },
56 { value: "motion", label: msg("Motion") },
57 { value: "media-library", label: msg("Media library") },
58 { value: "faceid", label: msg("Face ID") },
59 ];
60
61 const ANDROID_PERMISSIONS: ReadonlyArray<{ value: DevicePermission; label: string }> = [
62 { value: "camera", label: msg("Camera") },
63 { value: "microphone", label: msg("Microphone") },
64 { value: "photos", label: msg("Photos") },
65 { value: "contacts", label: msg("Contacts") },
66 { value: "calendar", label: msg("Calendar") },
67 { value: "location", label: msg("Location") },
68 { value: "notifications", label: msg("Notifications") },
69 { value: "motion", label: msg("Physical activity") },
70 ];
71
72 const LOCATION_PRESETS = [
73 {
74 label: msg("San Francisco", { $context: "city name" }),
75 latitude: 37.7749,
76 longitude: -122.4194,
77 },
78 { label: msg("New York", { $context: "city name" }), latitude: 40.7128, longitude: -74.006 },
79 { label: msg("London", { $context: "city name" }), latitude: 51.5074, longitude: -0.1278 },
80 { label: msg("Stockholm", { $context: "city name" }), latitude: 59.3293, longitude: 18.0686 },
81 { label: msg("Tokyo", { $context: "city name" }), latitude: 35.6762, longitude: 139.6503 },
82 ] as const;
83
84 export function DeviceToolsPanel(props: {
85 readonly controls: DeviceControls;
86 readonly hostDiagnostics: string | undefined;
87 readonly device: DeviceSummary;
88 readonly access: DeviceHubAccess | null;
89 readonly axOverlay: boolean;
90 readonly onAxOverlayChange: (enabled: boolean) => void;
91 readonly onClose: () => void;
92 readonly className?: string;
93 }) {
94 const { device, controls } = props;
95 const { detail, pending, error, foregroundApp, disabled, act } = controls;
96 const settings = detail?.settings;
97 const isIos = device.platform === "ios";
98 const gt = useGT();
99
100 return (
101 <div
102 className={cn("flex min-h-0 flex-col border-border bg-background text-sm", props.className)}
103 >
104 <div className="flex h-9 shrink-0 items-center gap-2 border-b px-3">
105 <span className="font-medium">
106 <T>Tools</T>
107 </span>
108 {pending ? <Spinner size="sm" /> : null}
109 <Button
110 size="icon-xs"
111 variant="ghost-muted"
112 aria-label={gt("Close tools")}
113 className="ml-auto"
114 onClick={props.onClose}
115 >
116 <X />
117 </Button>
118 </div>
119 <div className="min-h-0 flex-1 overflow-y-auto">
120 {error ? (
121 <p className="border-b bg-destructive/10 px-3 py-2 text-xs text-destructive">{error}</p>
122 ) : null}
123 {detail === null && !error ? (
124 <div className="flex items-center gap-2 px-3 py-3 text-xs text-muted-foreground">
125 <Spinner size="sm" /> <T>Reading device settings…</T>
126 </div>
127 ) : null}
128
129 {props.hostDiagnostics ? (
130 <Section title={gt("Host diagnostics")}>
131 <p className="whitespace-pre-line text-xs text-muted-foreground">
132 {props.hostDiagnostics}
133 </p>
134 </Section>
135 ) : null}
136
137 <Section title={gt("App", { $context: "section heading for the foreground app" })}>
138 <Row label={gt("Foreground", { $context: "the app currently in the foreground" })}>
139 <span className="truncate font-mono text-xs">{foregroundApp?.id ?? "-"}</span>
140 </Row>
141 {foregroundApp ? (
142 <div className="flex gap-1.5">
143 <Button
144 size="xs"
145 variant="outline"
146 disabled={disabled}
147 onClick={() => void act({ type: "terminateApp", appId: foregroundApp.id })}
148 >
149 <T>Terminate</T>
150 </Button>
151 <Button
152 size="xs"
153 variant="outline"
154 disabled={disabled}
155 onClick={() => void act({ type: "launchApp", appId: foregroundApp.id })}
156 >
157 <T>Relaunch</T>
158 </Button>
159 </div>
160 ) : null}
161 <SubmitRow
162 placeholder={gt("https://… or myapp://")}
163 action={gt("Open", { $context: "verb, opens a URL" })}
164 disabled={disabled}
165 onSubmit={(url) => act({ type: "openUrl", url })}
166 />
167 <SubmitRow
168 placeholder={isIos ? gt("Bundle ID to launch") : gt("Package name to launch")}
169 action={gt("Launch", { $context: "verb, launches an app" })}
170 disabled={disabled}
171 onSubmit={(appId) => act({ type: "launchApp", appId })}
172 />
173 </Section>
174
175 <Section title={isIos ? gt("Simulator") : gt("Emulator")}>
176 <Row label={gt("Appearance")}>
177 <ToggleGroup
178 aria-label={gt("Appearance")}
179 value={settings?.appearance ? [settings.appearance] : []}
180 disabled={disabled}
181 onValueChange={(value) => {
182 const next = value[0];
183 if (next === "light" || next === "dark")
184 void act({ type: "setAppearance", value: next });
185 }}
186 >
187 <Toggle value="light">
188 <T context="appearance mode">Light</T>
189 </Toggle>
190 <Toggle value="dark">
191 <T context="appearance mode">Dark</T>
192 </Toggle>
193 </ToggleGroup>
194 </Row>
195 <Row label={gt("Text size")}>
196 <ChoiceSelect
197 ariaLabel={gt("Text size")}
198 value={settings?.textSize ?? null}
199 options={TEXT_SIZES}
200 disabled={disabled}
201 onChange={(value) => act({ type: "setTextSize", value })}
202 />
203 </Row>
204 {isIos ? (
205 <>
206 <Row label="Liquid Glass">
207 <ToggleGroup
208 aria-label="Liquid Glass"
209 value={settings?.liquidGlass ? [settings.liquidGlass] : []}
210 disabled={disabled || settings?.liquidGlass === undefined}
211 onValueChange={(value) => {
212 const next = value[0];
213 if (next === "clear" || next === "tinted") {
214 void act({ type: "setLiquidGlass", value: next });
215 }
216 }}
217 >
218 <Toggle value="clear">
219 <T context="Liquid Glass style">Clear</T>
220 </Toggle>
221 <Toggle value="tinted">
222 <T context="Liquid Glass style">Tinted</T>
223 </Toggle>
224 </ToggleGroup>
225 </Row>
226 <Row label={gt("Color filter")}>
227 <ChoiceSelect
228 ariaLabel={gt("Color filter")}
229 value={settings?.colorFilter ?? null}
230 options={COLOR_FILTERS}
231 disabled={disabled}
232 onChange={(value) => act({ type: "setColorFilter", value })}
233 />
234 </Row>
235 </>
236 ) : (
237 <Row label={gt("Orientation")}>
238 <ChoiceSelect
239 ariaLabel={gt("Orientation")}
240 value={null}
241 placeholder={gt("Rotate to…")}
242 options={ORIENTATIONS}
243 disabled={disabled}
244 onChange={(value) => act({ type: "setOrientation", value })}
245 />
246 </Row>
247 )}
248 <SwitchRow
249 label={gt("Reduce Motion")}
250 checked={settings?.reduceMotion}
251 disabled={disabled}
252 onChange={(value) => act({ type: "setToggle", setting: "reduceMotion", value })}
253 />
254 {isIos ? (
255 <>
256 <SwitchRow
257 label={gt("Increase Contrast")}
258 checked={settings?.increaseContrast}
259 disabled={disabled}
260 onChange={(value) => act({ type: "setToggle", setting: "increaseContrast", value })}
261 />
262 <SwitchRow
263 label={gt("Reduce Transparency")}
264 checked={settings?.reduceTransparency}
265 disabled={disabled}
266 onChange={(value) =>
267 act({ type: "setToggle", setting: "reduceTransparency", value })
268 }
269 />
270 <SwitchRow
271 label={gt("Show Borders")}
272 checked={settings?.showBorders}
273 disabled={disabled}
274 onChange={(value) => act({ type: "setToggle", setting: "showBorders", value })}
275 />
276 <SwitchRow
277 label="VoiceOver"
278 checked={settings?.voiceOver}
279 disabled={disabled}
280 onChange={(value) => act({ type: "setToggle", setting: "voiceOver", value })}
281 />
282 </>
283 ) : (
284 <SwitchRow
285 label={gt("Network")}
286 checked={settings?.networkEnabled}
287 disabled={disabled}
288 onChange={(value) => act({ type: "setToggle", setting: "networkEnabled", value })}
289 />
290 )}
291 </Section>
292
293 <Section title={gt("Accessibility")}>
294 <SwitchRow
295 label={gt("Overlay element frames")}
296 checked={props.axOverlay}
297 disabled={props.access === null}
298 onChange={(value) => {
299 props.onAxOverlayChange(value);
300 return Promise.resolve();
301 }}
302 />
303 </Section>
304
305 <LocationSection
306 disabled={disabled}
307 canClear={isIos}
308 onSet={(latitude, longitude) => act({ type: "setLocation", latitude, longitude })}
309 onClear={() => act({ type: "clearLocation" })}
310 />
311
312 <PermissionsSection
313 permissions={isIos ? IOS_PERMISSIONS : ANDROID_PERMISSIONS}
314 canReset={isIos}
315 defaultAppId={foregroundApp?.id ?? ""}
316 disabled={disabled}
317 onDecide={(appId, permission, decision) =>
318 act({ type: "setPermission", appId, permission, decision })
319 }
320 />
321
322 {isIos ? (
323 <Section title={gt("Push notification")}>
324 <SubmitRow
325 placeholder={gt("Alert text")}
326 action={gt("Send", { $context: "verb, sends a push notification" })}
327 disabled={disabled || !foregroundApp}
328 onSubmit={(payload) =>
329 foregroundApp
330 ? act({ type: "sendPush", appId: foregroundApp.id, payload })
331 : Promise.resolve(false)
332 }
333 />
334 {!foregroundApp ? (
335 <p className="text-xs text-muted-foreground">
336 <T>Open an app first.</T>
337 </p>
338 ) : null}
339 </Section>
340 ) : null}
341
342 {isIos && props.access ? <EventLogSection access={props.access} device={device} /> : null}
343 </div>
344 </div>
345 );
346 }
347
348 function Section(props: { readonly title: string; readonly children: React.ReactNode }) {
349 return (
350 <section className="flex flex-col gap-2 border-b px-3 py-2.5 last:border-b-0">
351 <h3 className="text-xs font-medium text-muted-foreground">{props.title}</h3>
352 {props.children}
353 </section>
354 );
355 }
356
357 function Row(props: { readonly label: string; readonly children: React.ReactNode }) {
358 return (
359 <div className="flex min-h-7 items-center justify-between gap-3">
360 <span className="shrink-0 text-xs text-muted-foreground">{props.label}</span>
361 <div className="flex min-w-0 items-center justify-end">{props.children}</div>
362 </div>
363 );
364 }
365
366 function SwitchRow(props: {
367 readonly label: string;
368 readonly checked: boolean | undefined;
369 readonly disabled: boolean;
370 readonly onChange: (value: boolean) => Promise<unknown>;
371 }) {
372 return (
373 <Row label={props.label}>
374 <Switch
375 size="sm"
376 aria-label={props.label}
377 checked={props.checked ?? false}
378 disabled={props.disabled || props.checked === undefined}
379 onCheckedChange={(checked) => void props.onChange(checked)}
380 />
381 </Row>
382 );
383 }
384
385 function ChoiceSelect<V extends string>(props: {
386 readonly ariaLabel: string;
387 readonly value: V | null;
388 readonly options: ReadonlyArray<{ readonly value: V; readonly label: string }>;
389 readonly disabled: boolean;
390 readonly placeholder?: string;
391 readonly onChange: (value: V) => Promise<unknown>;
392 }) {
393 const gt = useGT();
394 const m = useMessages();
395 const current = props.options.find((option) => option.value === props.value);
396 return (
397 <Select
398 value={props.value}
399 disabled={props.disabled}
400 onValueChange={(value) => {
401 if (value !== null && value !== props.value) void props.onChange(value as V);
402 }}
403 >
404 <SelectTrigger size="xs" className="w-40" aria-label={props.ariaLabel}>
405 <SelectValue>
406 {current ? (
407 m(current.label)
408 ) : (
409 <span className="text-muted-foreground">{props.placeholder ?? gt("Unknown")}</span>
410 )}
411 </SelectValue>
412 </SelectTrigger>
413 <SelectPopup align="end" alignItemWithTrigger={false}>
414 {props.options.map((option) => (
415 <SelectItem key={option.value} value={option.value}>
416 {m(option.label)}
417 </SelectItem>
418 ))}
419 </SelectPopup>
420 </Select>
421 );
422 }
423
424 function SubmitRow(props: {
425 readonly placeholder: string;
426 readonly action: string;
427 readonly disabled: boolean;
428 readonly onSubmit: (value: string) => Promise<boolean>;
429 }) {
430 const [value, setValue] = useState("");
431 const submit = () => {
432 const trimmed = value.trim();
433 if (!trimmed) return;
434 void props.onSubmit(trimmed).then((sent) => {
435 if (sent) setValue("");
436 });
437 };
438 return (
439 <form
440 className="flex gap-1.5"
441 onSubmit={(event) => {
442 event.preventDefault();
443 submit();
444 }}
445 >
446 <Input
447 size="compact"
448 font="mono"
449 className="min-w-0 flex-1"
450 placeholder={props.placeholder}
451 value={value}
452 disabled={props.disabled}
453 onChange={(event) => setValue(event.target.value)}
454 />
455 <Button
456 type="submit"
457 size="xs"
458 variant="outline"
459 disabled={props.disabled || value.trim().length === 0}
460 >
461 {props.action}
462 </Button>
463 </form>
464 );
465 }
466
467 function LocationSection(props: {
468 readonly disabled: boolean;
469 readonly canClear: boolean;
470 readonly onSet: (latitude: number, longitude: number) => Promise<unknown>;
471 readonly onClear: () => Promise<unknown>;
472 }) {
473 const gt = useGT();
474 const m = useMessages();
475 const [latitude, setLatitude] = useState("");
476 const [longitude, setLongitude] = useState("");
477 const parsed = { latitude: Number(latitude), longitude: Number(longitude) };
478 const valid =
479 latitude.trim() !== "" &&
480 longitude.trim() !== "" &&
481 Math.abs(parsed.latitude) <= 90 &&
482 Math.abs(parsed.longitude) <= 180;
483 return (
484 <Section title={gt("Location")}>
485 <div className="flex gap-1.5">
486 <Input
487 size="compact"
488 font="mono"
489 className="min-w-0 flex-1"
490 placeholder={gt("Latitude")}
491 inputMode="decimal"
492 value={latitude}
493 disabled={props.disabled}
494 onChange={(event) => setLatitude(event.target.value)}
495 />
496 <Input
497 size="compact"
498 font="mono"
499 className="min-w-0 flex-1"
500 placeholder={gt("Longitude")}
501 inputMode="decimal"
502 value={longitude}
503 disabled={props.disabled}
504 onChange={(event) => setLongitude(event.target.value)}
505 />
506 </div>
507 <div className="flex flex-wrap items-center gap-1.5">
508 <Select<string | null>
509 value={null}
510 disabled={props.disabled}
511 onValueChange={(value) => {
512 const preset = LOCATION_PRESETS.find((candidate) => candidate.label === value);
513 if (!preset) return;
514 setLatitude(String(preset.latitude));
515 setLongitude(String(preset.longitude));
516 void props.onSet(preset.latitude, preset.longitude);
517 }}
518 >
519 <SelectTrigger size="xs" className="w-32" aria-label={gt("Location preset")}>
520 <SelectValue>
521 <span className="text-muted-foreground">
522 <T>Preset…</T>
523 </span>
524 </SelectValue>
525 </SelectTrigger>
526 <SelectPopup align="start" alignItemWithTrigger={false}>
527 {LOCATION_PRESETS.map((preset) => (
528 <SelectItem key={preset.label} value={preset.label}>
529 {m(preset.label)}
530 </SelectItem>
531 ))}
532 </SelectPopup>
533 </Select>
534 <Button
535 size="xs"
536 variant="outline"
537 disabled={props.disabled || !valid}
538 onClick={() => void props.onSet(parsed.latitude, parsed.longitude)}
539 >
540 <T context="verb, applies the location">Set</T>
541 </Button>
542 {props.canClear ? (
543 <Button
544 size="xs"
545 variant="ghost"
546 disabled={props.disabled}
547 onClick={() => {
548 setLatitude("");
549 setLongitude("");
550 void props.onClear();
551 }}
552 >
553 <T context="verb, clears the location">Clear</T>
554 </Button>
555 ) : null}
556 </div>
557 </Section>
558 );
559 }
560
561 function PermissionsSection(props: {
562 readonly permissions: ReadonlyArray<{ value: DevicePermission; label: string }>;
563 readonly canReset: boolean;
564 readonly defaultAppId: string;
565 readonly disabled: boolean;
566 readonly onDecide: (
567 appId: string,
568 permission: DevicePermission,
569 decision: "grant" | "revoke" | "reset",
570 ) => Promise<unknown>;
571 }) {
572 const gt = useGT();
573 const [appId, setAppId] = useState("");
574 const [permission, setPermission] = useState<DevicePermission>("camera");
575 const resolvedAppId = appId.trim() || props.defaultAppId;
576 const decide = (decision: "grant" | "revoke" | "reset") =>
577 void props.onDecide(resolvedAppId, permission, decision);
578 return (
579 <Section title={gt("Permissions")}>
580 <Input
581 size="compact"
582 font="mono"
583 placeholder={props.defaultAppId || gt("App ID")}
584 value={appId}
585 disabled={props.disabled}
586 onChange={(event) => setAppId(event.target.value)}
587 />
588 <div className="flex flex-wrap items-center gap-1.5">
589 <ChoiceSelect
590 ariaLabel={gt("Permission")}
591 value={permission}
592 options={props.permissions}
593 disabled={props.disabled}
594 onChange={(value) => {
595 setPermission(value);
596 return Promise.resolve();
597 }}
598 />
599 <Button
600 size="xs"
601 variant="outline"
602 disabled={props.disabled || !resolvedAppId}
603 onClick={() => decide("grant")}
604 >
605 <T>Grant</T>
606 </Button>
607 <Button
608 size="xs"
609 variant="outline"
610 disabled={props.disabled || !resolvedAppId}
611 onClick={() => decide("revoke")}
612 >
613 <T>Revoke</T>
614 </Button>
615 {props.canReset ? (
616 <Button
617 size="xs"
618 variant="ghost"
619 disabled={props.disabled || !resolvedAppId}
620 onClick={() => decide("reset")}
621 >
622 <T context="verb, resets the permission">Reset</T>
623 </Button>
624 ) : null}
625 </div>
626 </Section>
627 );
628 }
629
630 function EventLogSection(props: {
631 readonly access: DeviceHubAccess;
632 readonly device: DeviceSummary;
633 }) {
634 const [open, setOpen] = useState(false);
635
636 return (
637 <Collapsible open={open} onOpenChange={setOpen}>
638 <CollapsibleTrigger className="flex w-full items-center gap-1.5 border-b px-3 py-2.5 text-left text-xs font-medium text-muted-foreground">
639 <T>Event log</T>
640 <ChevronDown
641 className={cn("ml-auto size-3.5 transition-transform", open && "rotate-180")}
642 />
643 </CollapsibleTrigger>
644 <CollapsiblePanel>
645 <EventLogEntries access={props.access} device={props.device} />
646 </CollapsiblePanel>
647 </Collapsible>
648 );
649 }
650
651 // The panel unmounts while closed, so the log streams only while it is open.
652 function EventLogEntries(props: {
653 readonly access: DeviceHubAccess;
654 readonly device: DeviceSummary;
655 }) {
656 const entries = useAtomValue(
657 deviceEventLogAtom({
658 access: props.access,
659 platform: props.device.platform,
660 deviceId: props.device.id,
661 }),
662 );
663 return (
664 <ol className="max-h-64 overflow-y-auto px-3 py-2 font-mono text-2xs leading-relaxed">
665 {entries.length === 0 ? (
666 <li className="text-muted-foreground">
667 <T>No events yet.</T>
668 </li>
669 ) : (
670 entries.map((entry) => (
671 <li key={entry.id} className="flex gap-2">
672 <span className="shrink-0 text-muted-foreground">{entry.timestamp.slice(11, 19)}</span>
673 <span className="truncate">{entry.summary}</span>
674 </li>
675 ))
676 )}
677 </ol>
678 );
679 }