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