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

DeviceAndroidFoldControls.tsx

167 lines5.8 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 AndroidFoldError,
7 readAndroidFold,
8 setAndroidFold,
9 type AndroidFoldPosture,
10 type AndroidFoldState,
11} from "./deviceFold";
12import { DeviceDuoGlyph } from "./DeviceDuoGlyph";
13​
14export function DeviceAndroidFoldControls(props: {
15 readonly access: DeviceHubAccess;
16 readonly deviceId: string;
17 readonly visible: boolean;
18 readonly enabled: boolean;
19 readonly screenWidth: number | undefined;
20 readonly screenHeight: number | undefined;
21 readonly onFoldAngle: (angle: number | null) => void;
22 readonly renewAccess: () => Promise<DeviceHubAccess | null>;
23}) {
24 const [fold, setFold] = useState<AndroidFoldState | null>(null);
25 const [pending, setPending] = useState(false);
26 const [error, setError] = useState<string | null>(null);
27 const busy = useRef(false);
28 const mounted = useRef(true);
29 const { access, deviceId, visible, enabled, screenWidth, screenHeight, onFoldAngle } = props;
30​
31 useEffect(() => {
32 mounted.current = true;
33 return () => {
34 mounted.current = false;
35 };
36 }, []);
37​
38 useEffect(() => {
39 if (!visible || !enabled || pending || !screenWidth || !screenHeight) return;
40 const controller = new AbortController();
41 let retry: number | undefined;
42 const read = () => {
43 void readAndroidFold(access, deviceId, controller.signal)
44 .then((next) => {
45 if (!controller.signal.aborted) {
46 setFold(next);
47 onFoldAngle(
48 next.supported ? (next.hingeAngle ?? (next.posture === "closed" ? 0 : 180)) : null,
49 );
50 }
51 })
52 .catch((cause: unknown) => {
53 // Retrying an expired ticket cannot succeed. New access re-runs this effect.
54 const expired = cause instanceof AndroidFoldError && cause.status === 401;
55 if (!controller.signal.aborted && !expired) retry = window.setTimeout(read, 3_000);
56 });
57 };
58 read();
59 return () => {
60 controller.abort();
61 window.clearTimeout(retry);
62 };
63 }, [access, deviceId, visible, enabled, screenWidth, screenHeight, pending, onFoldAngle]);
64​
65 if (!fold?.supported || !props.visible) return null;
66​
67 const change = (posture: AndroidFoldPosture) => {
68 if (busy.current || !props.enabled) return;
69 busy.current = true;
70 setPending(true);
71 setError(null);
72 props.onFoldAngle(posture === "closed" ? 0 : 180);
73 const controller = new AbortController();
74 const timeout = window.setTimeout(() => controller.abort(), 12_000);
75 const send = (access: DeviceHubAccess) =>
76 setAndroidFold(access, props.deviceId, posture, controller.signal);
77 void send(props.access)
78 .catch(async (cause: unknown) => {
79 // Hub tickets expire after a few minutes, while the panel stays open much longer.
80 if (!(cause instanceof AndroidFoldError && cause.status === 401)) throw cause;
81 const renewed = await props.renewAccess();
82 if (!renewed || controller.signal.aborted) throw cause;
83 return send(renewed);
84 })
85 .then((next) => {
86 if (mounted.current) {
87 setFold(next);
88 props.onFoldAngle(next.hingeAngle ?? (next.posture === "closed" ? 0 : 180));
89 }
90 })
91 .catch((cause: unknown) => {
92 if (mounted.current) {
93 props.onFoldAngle(fold.hingeAngle ?? (fold.posture === "closed" ? 0 : 180));
94 setError(
95 controller.signal.aborted
96 ? "Fold command timed out."
97 : cause instanceof Error
98 ? cause.message
99 : "Could not change fold posture.",
100 );
101 }
102 })
103 .finally(() => {
104 window.clearTimeout(timeout);
105 busy.current = false;
106 if (mounted.current) setPending(false);
107 });
108 };
109​
110 return (
111 <div aria-label="Android fold controls" className="flex flex-col items-center gap-2">
112 <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">
113 <Tooltip>
114 <TooltipTrigger
115 render={
116 <Button
117 size="icon"
118 variant={fold.posture === "closed" ? "secondary" : "ghost"}
119 aria-label="Fold device"
120 aria-pressed={fold.posture === "closed"}
121 disabled={pending || !props.enabled}
122 onClick={() => change("closed")}
123 />
124 }
125 >
126 <DeviceDuoGlyph pose="closed" />
127 </TooltipTrigger>
128 <TooltipPopup side="left">Fold device</TooltipPopup>
129 </Tooltip>
130 <Tooltip>
131 <TooltipTrigger
132 render={
133 <Button
134 size="icon"
135 variant={fold.posture === "opened" ? "secondary" : "ghost"}
136 aria-label="Unfold device"
137 aria-pressed={fold.posture === "opened"}
138 disabled={pending || !props.enabled}
139 onClick={() => change("opened")}
140 />
141 }
142 >
143 <DeviceDuoGlyph pose="open" />
144 </TooltipTrigger>
145 <TooltipPopup side="left">Unfold device</TooltipPopup>
146 </Tooltip>
147 </div>
148 {error ? (
149 <Tooltip>
150 <TooltipTrigger
151 render={
152 <span
153 tabIndex={0}
154 role="alert"
155 aria-label={error}
156 className="pointer-events-auto text-xs text-destructive"
157 >
158 !
159 </span>
160 }
161 />
162 <TooltipPopup side="left">{error}</TooltipPopup>
163 </Tooltip>
164 ) : null}
165 </div>
166 );