Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DeviceAndroidFoldControls.tsx

154 lines5.1 KB

1import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess";
2import { useEffect, useRef, useState } from "react";
3import { Button } from "~/components/ui/button";
4import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
5import {
6 readAndroidFold,
7 setAndroidFold,
8 type AndroidFoldPosture,
9 type AndroidFoldState,
10} from "./deviceFold";
11import { DeviceDuoGlyph } from "./DeviceDuoGlyph";
12​
13export function DeviceAndroidFoldControls(props: {
14 readonly access: DeviceHubAccess;
15 readonly deviceId: string;
16 readonly visible: boolean;
17 readonly enabled: boolean;
18 readonly screenWidth: number | undefined;
19 readonly screenHeight: number | undefined;
20 readonly onFoldAngle: (angle: number | null) => void;
21}) {
22 const [fold, setFold] = useState<AndroidFoldState | null>(null);
23 const [pending, setPending] = useState(false);
24 const [error, setError] = useState<string | null>(null);
25 const busy = useRef(false);
26 const mounted = useRef(true);
27 const { access, deviceId, visible, enabled, screenWidth, screenHeight, onFoldAngle } = props;
28​
29 useEffect(() => {
30 mounted.current = true;
31 return () => {
32 mounted.current = false;
33 };
34 }, []);
35​
36 useEffect(() => {
37 if (!visible || !enabled || pending || !screenWidth || !screenHeight) return;
38 const controller = new AbortController();
39 let retry: number | undefined;
40 const read = () => {
41 void readAndroidFold(access, deviceId, controller.signal)
42 .then((next) => {
43 if (!controller.signal.aborted) {
44 setFold(next);
45 onFoldAngle(
46 next.supported ? (next.hingeAngle ?? (next.posture === "closed" ? 0 : 180)) : null,
47 );
48 }
49 })
50 .catch(() => {
51 if (!controller.signal.aborted) retry = window.setTimeout(read, 3_000);
52 });
53 };
54 read();
55 return () => {
56 controller.abort();
57 window.clearTimeout(retry);
58 };
59 }, [access, deviceId, visible, enabled, screenWidth, screenHeight, pending, onFoldAngle]);
60​
61 if (!fold?.supported || !props.visible) return null;
62​
63 const change = (posture: AndroidFoldPosture) => {
64 if (busy.current || !props.enabled) return;
65 busy.current = true;
66 setPending(true);
67 setError(null);
68 props.onFoldAngle(posture === "closed" ? 0 : 180);
69 const controller = new AbortController();
70 const timeout = window.setTimeout(() => controller.abort(), 12_000);
71 void setAndroidFold(props.access, props.deviceId, posture, controller.signal)
72 .then((next) => {
73 if (mounted.current) {
74 setFold(next);
75 props.onFoldAngle(next.hingeAngle ?? (next.posture === "closed" ? 0 : 180));
76 }
77 })
78 .catch((cause: unknown) => {
79 if (mounted.current) {
80 props.onFoldAngle(fold.hingeAngle ?? (fold.posture === "closed" ? 0 : 180));
81 setError(
82 controller.signal.aborted
83 ? "Fold command timed out."
84 : cause instanceof Error
85 ? cause.message
86 : "Could not change fold posture.",
87 );
88 }
89 })
90 .finally(() => {
91 window.clearTimeout(timeout);
92 busy.current = false;
93 if (mounted.current) setPending(false);
94 });
95 };
96​
97 return (
98 <div aria-label="Android fold controls" className="flex flex-col items-center gap-2">
99 <div className="pointer-events-auto flex shrink-0 flex-col items-center gap-1 rounded-full border border-border/50 bg-background/80 p-1 shadow-sm">
100 <Tooltip>
101 <TooltipTrigger
102 render={
103 <Button
104 size="icon"
105 variant={fold.posture === "closed" ? "secondary" : "ghost"}
106 aria-label="Fold device"
107 aria-pressed={fold.posture === "closed"}
108 disabled={pending || !props.enabled}
109 onClick={() => change("closed")}
110 />
111 }
112 >
113 <DeviceDuoGlyph pose="closed" />
114 </TooltipTrigger>
115 <TooltipPopup side="left">Fold device</TooltipPopup>
116 </Tooltip>
117 <Tooltip>
118 <TooltipTrigger
119 render={
120 <Button
121 size="icon"
122 variant={fold.posture === "opened" ? "secondary" : "ghost"}
123 aria-label="Unfold device"
124 aria-pressed={fold.posture === "opened"}
125 disabled={pending || !props.enabled}
126 onClick={() => change("opened")}
127 />
128 }
129 >
130 <DeviceDuoGlyph pose="open" />
131 </TooltipTrigger>
132 <TooltipPopup side="left">Unfold device</TooltipPopup>
133 </Tooltip>
134 </div>
135 {error ? (
136 <Tooltip>
137 <TooltipTrigger
138 render={
139 <span
140 tabIndex={0}
141 role="alert"
142 aria-label={error}
143 className="pointer-events-auto text-xs text-destructive"
144 >
145 !
146 </span>
147 }
148 />
149 <TooltipPopup side="left">{error}</TooltipPopup>
150 </Tooltip>
151 ) : null}
152 </div>
153 );