The best way to control Claude Code, Codex and more from any computer or phone
refactor(web): device panel reads hub state from atoms (#25265)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Julius Marminge committed
Oct 25, 2026 at 20:38 UTC
0a3b561bc0901d8fa47e430fc9d8593918cde978
19 files changed
+985
-653
apps/web/src/components/device/DeviceAndroidFoldControls.tsx
+27
-64
@@ -1,75 +1,45 @@
1
import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess";
2
-import { useEffect, useRef, useState } from "react";
2
+import { useAtomMount, useAtomSet, useAtomValue } from "@effect/atom-react";
3
+import { useRef, useState } from "react";
4
import { Button } from "~/components/ui/button";
5
import { Tooltip, TooltipPopup, TooltipTrigger } from "~/components/ui/tooltip";
5
-import {
6
- AndroidFoldError,
7
- readAndroidFold,
8
- setAndroidFold,
9
- type AndroidFoldPosture,
10
- type AndroidFoldState,
11
-} from "./deviceFold";
6
+import { AndroidFoldError, setAndroidFold, type AndroidFoldPosture } from "./deviceFold";
7
+import { androidFoldReadAtom, androidFoldViewAtom, foldAngle, NOOP_ATOM } from "./deviceHubAtoms";
8
import { DeviceDuoGlyph } from "./DeviceDuoGlyph";
9
10
export function DeviceAndroidFoldControls(props: {
11
readonly access: DeviceHubAccess;
12
+ readonly deviceKey: string;
13
readonly deviceId: string;
14
readonly visible: boolean;
15
readonly enabled: boolean;
16
readonly screenWidth: number | undefined;
17
readonly screenHeight: number | undefined;
21
- readonly onFoldAngle: (angle: number | null) => void;
18
readonly renewAccess: () => Promise<DeviceHubAccess | null>;
19
}) {
24
- const [fold, setFold] = useState<AndroidFoldState | null>(null);
20
const [pending, setPending] = useState(false);
21
const [error, setError] = useState<string | null>(null);
22
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]);
23
+ const { access, deviceKey, deviceId, visible, enabled, screenWidth, screenHeight } = props;
24
+ const viewAtom = androidFoldViewAtom(deviceKey);
25
+ const { fold } = useAtomValue(viewAtom);
26
+ const setView = useAtomSet(viewAtom);
27
+ // A change in flight owns the posture; reading meanwhile could show the old one.
28
+ useAtomMount(
29
+ visible && enabled && !pending && screenWidth && screenHeight
30
+ ? androidFoldReadAtom({ deviceKey, access, deviceId, screenWidth, screenHeight })
31
+ : NOOP_ATOM,
32
+ );
33
34
if (!fold?.supported || !props.visible) return null;
35
36
+ const setAngle = (angle: number) => setView((current) => ({ ...current, angle }));
37
const change = (posture: AndroidFoldPosture) => {
38
if (busy.current || !props.enabled) return;
39
busy.current = true;
40
setPending(true);
41
setError(null);
72
- props.onFoldAngle(posture === "closed" ? 0 : 180);
42
+ setAngle(posture === "closed" ? 0 : 180);
43
const controller = new AbortController();
44
const timeout = window.setTimeout(() => controller.abort(), 12_000);
45
const send = (access: DeviceHubAccess) =>
@@ -82,28 +52,21 @@ export function DeviceAndroidFoldControls(props: {
52
if (!renewed || controller.signal.aborted) throw cause;
53
return send(renewed);
54
})
85
- .then((next) => {
86
- if (mounted.current) {
87
- setFold(next);
88
- props.onFoldAngle(next.hingeAngle ?? (next.posture === "closed" ? 0 : 180));
89
- }
90
- })
55
+ .then((next) => setView({ fold: next, angle: foldAngle(next) }))
56
.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
- }
57
+ setAngle(foldAngle(fold));
58
+ setError(
59
+ controller.signal.aborted
60
+ ? "Fold command timed out."
61
+ : cause instanceof Error
62
+ ? cause.message
63
+ : "Could not change fold posture.",
64
+ );
65
})
66
.finally(() => {
67
window.clearTimeout(timeout);
68
busy.current = false;
106
- if (mounted.current) setPending(false);
69
+ setPending(false);
70
});
71
};
72
apps/web/src/components/device/DeviceDuoViewport.tsx
+83
-112
@@ -1,37 +1,43 @@
1
-import { useEffect, useRef, type RefObject } from "react";
1
+import { useEffect, useRef } from "react";
2
import type { DuoViewer } from "@t3tools/client-runtime/device/duo-viewer";
3
import type { DeviceModelSource } from "@t3tools/client-runtime/device/model";
4
-import { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
4
+import type { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
5
import { createCanvasFrameSink } from "@t3tools/client-runtime/device/frame";
6
import type { DeviceScreenSize, DeviceStreamClient } from "@t3tools/client-runtime/device/stream";
7
import { createDuoPinch } from "@t3tools/client-runtime/device/duo-control";
8
-import { bindPhoneTrackpad } from "./phoneTrackpad";
9
-import { observeResize } from "../../lib/observeResize";
8
+import { mountDeviceViewer } from "./mountDeviceViewer";
9
10
const loadDuoViewer = () => import("@t3tools/client-runtime/device/duo-viewer");
11
12
export function DeviceDuoViewport(props: {
13
readonly model: DeviceModelSource;
15
- readonly source: RefObject<HTMLCanvasElement | null>;
14
+ readonly source: HTMLCanvasElement;
15
readonly onFrameListener: (listener: (() => void) | null) => void;
16
readonly onResetReady: (reset: (() => void) | null) => void;
17
readonly onInputCancel: (cancel: (() => void) | null) => void;
19
- readonly client: RefObject<DeviceStreamClient | null>;
18
+ readonly client: DeviceStreamClient;
19
readonly screen: DeviceScreenSize | null;
20
readonly hingePreview: number | null;
22
- readonly controlError: string | null;
21
+ readonly onControlRejectedListener: (listener: (() => void) | null) => void;
22
readonly onUnavailable: () => void;
23
}) {
25
- const hostRef = useRef<HTMLDivElement | null>(null);
26
- const canvasRef = useRef<HTMLCanvasElement | null>(null);
27
- const viewerRef = useRef<DuoViewer | null>(null);
24
+ const viewerRef = useRef<
25
+ (DuoViewer & { readonly sources: Record<1 | 3, HTMLCanvasElement> }) | null
26
+ >(null);
27
const interactionRef = useRef<ReturnType<typeof createPhoneInteraction> | null>(null);
28
const pinchRef = useRef<ReturnType<typeof createDuoPinch> | null>(null);
30
- const trackpadRef = useRef<ReturnType<typeof bindPhoneTrackpad> | null>(null);
29
const screenRef = useRef(props.screen);
30
const modelRef = useRef(props.model);
31
const previewRef = useRef(props.hingePreview);
34
- const { source, client, onInputCancel, onResetReady, onUnavailable, onFrameListener } = props;
32
+ const {
33
+ source,
34
+ client,
35
+ onInputCancel,
36
+ onControlRejectedListener,
37
+ onResetReady,
38
+ onUnavailable,
39
+ onFrameListener,
40
+ } = props;
41
42
useEffect(() => {
43
screenRef.current = props.screen;
@@ -40,84 +46,60 @@ export function DeviceDuoViewport(props: {
46
viewerRef.current?.setScreen(props.screen);
47
}, [props.screen, props.model]);
48
43
- useEffect(() => {
44
- if (props.controlError || !props.screen) {
45
- trackpadRef.current?.cancel();
46
- viewerRef.current?.rejectOrientation();
47
- }
48
- }, [props.controlError, props.screen]);
49
-
49
useEffect(() => {
50
previewRef.current = props.hingePreview;
51
if (!pinchRef.current?.active) viewerRef.current?.setHingePreview(props.hingePreview);
52
}, [props.hingePreview]);
53
55
- useEffect(() => {
56
- const host = hostRef.current;
57
- const canvas = canvasRef.current;
58
- const decoded = source.current;
59
- if (!host || !canvas || !decoded) return;
60
- let disposed = false;
61
- let trackpad: ReturnType<typeof bindPhoneTrackpad> | null = null;
62
- let stopTrackpadEnd: (() => void) | undefined;
63
- const resize = () => {
64
- const { width, height } = host.getBoundingClientRect();
65
- viewerRef.current?.resize(width, height, window.devicePixelRatio);
66
- };
67
- const stopObserving = observeResize(host, resize);
68
- const blur = () => {
69
- interactionRef.current?.end();
70
- trackpad?.cancel();
71
- viewerRef.current?.cancelInput();
72
- };
73
- onInputCancel(blur);
74
- window.addEventListener("blur", blur);
75
- void loadDuoViewer()
76
- .then(({ createDuoViewer }) => {
77
- if (disposed) return;
78
- const sources = {
79
- 1: document.createElement("canvas"),
80
- 3: document.createElement("canvas"),
81
- };
82
- const viewer = createDuoViewer({
83
- canvas,
84
- sources,
85
- onUnavailable,
86
- onPanelRequested: (panel) =>
87
- client.current?.controlDuo({
88
- control: "physical",
89
- value: panel === 1 ? "facedown" : "faceup",
90
- }),
91
- onOrientationRequested: (value) =>
92
- client.current?.controlDuo({ control: "orientation", value }),
93
- onModelError: (cause) => console.warn("Device 3D asset could not load", cause),
94
- model: modelRef.current,
54
+ // The 3D viewer lives as long as its canvas; later prop changes are pushed into it above.
55
+ const mountViewer = (canvas: HTMLCanvasElement | null) => {
56
+ const host = canvas?.parentElement;
57
+ if (!canvas || !host) return;
58
+ return mountDeviceViewer(canvas, host, {
59
+ load: () =>
60
+ loadDuoViewer().then(({ createDuoViewer }) => () => {
61
+ const sources = {
62
+ 1: document.createElement("canvas"),
63
+ 3: document.createElement("canvas"),
64
+ };
65
+ const viewer = createDuoViewer({
66
+ canvas,
67
+ sources,
68
+ onUnavailable,
69
+ onPanelRequested: (panel) =>
70
+ client.controlDuo({
71
+ control: "physical",
72
+ value: panel === 1 ? "facedown" : "faceup",
73
+ }),
74
+ onOrientationRequested: (value) => client.controlDuo({ control: "orientation", value }),
75
+ onModelError: (cause) => console.warn("Device 3D asset could not load", cause),
76
+ model: modelRef.current,
77
+ });
78
+ return { ...viewer, sources };
79
+ }),
80
+ touch: (phase, point) => client.sendRawTouch(phase, point.x, point.y),
81
+ viewerRef,
82
+ interactionRef,
83
+ onInputCancel,
84
+ onResetReady,
85
+ onFrameListener,
86
+ onUnavailable,
87
+ cancelInput: (viewer) => viewer.cancelInput(),
88
+ ready: ({ viewer, cancelTrackpad }) => {
89
+ onControlRejectedListener(() => {
90
+ cancelTrackpad();
91
+ viewer.rejectOrientation();
92
});
96
- viewerRef.current = viewer;
97
- onResetReady(viewer.resetPose);
93
viewer.setScreen(screenRef.current);
99
- const primaryFrame = () => {
100
- const panel = screenRef.current?.screenId;
101
- if (panel === 1 || panel === 3) viewer.frameUpdated(panel, decoded);
102
- };
103
- onFrameListener(primaryFrame);
104
- primaryFrame();
94
viewer.setHingePreview(previewRef.current);
106
- client.current?.setDuoPanels({
95
+ client.setDuoPanels({
96
onScreen(next) {
97
screenRef.current = next;
98
interactionRef.current?.end();
99
viewer.setScreen(next);
100
},
112
- cover: createCanvasFrameSink(sources[1], () => viewer.frameUpdated(1)),
113
- inner: createCanvasFrameSink(sources[3], () => viewer.frameUpdated(3)),
114
- });
115
- interactionRef.current = createPhoneInteraction({
116
- screenPoint: (point, captured) => viewer.screenPoint(point.x, point.y, captured),
117
- touch: (phase, point) => client.current?.sendRawTouch(phase, point.x, point.y),
118
- orbit: viewer.orbit,
119
- zoomBy: () => {},
120
- onInteractionActive: viewer.setInteractionActive,
101
+ cover: createCanvasFrameSink(viewer.sources[1], () => viewer.frameUpdated(1)),
102
+ inner: createCanvasFrameSink(viewer.sources[3], () => viewer.frameUpdated(3)),
103
});
104
const pinch = createDuoPinch({
105
angle: () =>
@@ -127,43 +109,32 @@ export function DeviceDuoViewport(props: {
109
contains: (x, y) => !!screenRef.current && viewer.beginHinge(x, y),
110
change: (angle) => {
111
viewer.setHingePreview(angle ?? previewRef.current, true);
130
- if (angle !== null) client.current?.controlDuo({ control: "angle", value: angle });
112
+ if (angle !== null) client.controlDuo({ control: "angle", value: angle });
113
},
114
});
115
pinchRef.current = pinch;
134
- trackpad = bindPhoneTrackpad(canvas, interactionRef.current, {
135
- begin(x, y) {
136
- interactionRef.current?.end();
137
- return pinch.begin(x, y);
116
+ return {
117
+ frame: () => {
118
+ const panel = screenRef.current?.screenId;
119
+ if (panel === 1 || panel === 3) viewer.frameUpdated(panel, source);
120
},
139
- move: pinch.move,
140
- end: pinch.end,
141
- });
142
- trackpadRef.current = trackpad;
143
- stopTrackpadEnd = window.desktopBridge?.onTrackpadScrollEnd?.(() => trackpad?.endOrbit());
144
- resize();
145
- })
146
- .catch(() => {
147
- if (!disposed) onUnavailable();
148
- });
149
- return () => {
150
- disposed = true;
151
- stopTrackpadEnd?.();
152
- trackpad?.dispose();
153
- trackpadRef.current = null;
154
- pinchRef.current = null;
155
- interactionRef.current?.end();
156
- interactionRef.current = null;
157
- onInputCancel(null);
158
- onResetReady(null);
159
- onFrameListener(null);
160
- client.current?.setDuoPanels(null);
161
- stopObserving();
162
- window.removeEventListener("blur", blur);
163
- viewerRef.current?.dispose();
164
- viewerRef.current = null;
165
- };
166
- }, [onInputCancel, onResetReady, client, onUnavailable, source, onFrameListener]);
121
+ pinch: {
122
+ begin(x, y) {
123
+ interactionRef.current?.end();
124
+ return pinch.begin(x, y);
125
+ },
126
+ move: pinch.move,
127
+ end: pinch.end,
128
+ },
129
+ dispose: () => {
130
+ pinchRef.current = null;
131
+ onControlRejectedListener(null);
132
+ client.setDuoPanels(null);
133
+ },
134
+ };
135
+ },
136
+ });
137
+ };
138
139
const point = (event: React.PointerEvent<HTMLCanvasElement>) => {
140
const rect = event.currentTarget.getBoundingClientRect();
@@ -174,13 +145,13 @@ export function DeviceDuoViewport(props: {
145
};
146
return (
147
<div className="absolute inset-0">
177
- <div ref={hostRef} className="absolute inset-0">
148
+ <div className="absolute inset-0">
149
<div
150
aria-hidden
151
className="pointer-events-none absolute bottom-[6%] left-1/2 h-5 w-2/5 -translate-x-1/2 rounded-full bg-foreground/10 blur-xl"
152
/>
153
<canvas
183
- ref={canvasRef}
154
+ ref={mountViewer}
155
aria-label="Interactive 3D iPhone Duo. Drag the screen to interact. Drag outside it or swipe with two fingers to turn. Pinch over the device to open or close its hinge."
156
className="size-full touch-none"
157
onPointerDown={(event) => {
apps/web/src/components/device/DevicePanel.browser.test.tsx
+33
-5
@@ -20,6 +20,7 @@ type CommandResult =
20
const mocks = vi.hoisted(() => ({
21
open: vi.fn<(request: unknown) => Promise<CommandResult>>(),
22
close: vi.fn<(request: unknown) => Promise<CommandResult>>(),
23
+ list: vi.fn<(request: unknown) => Promise<CommandResult>>(),
24
openDevice: vi.fn(),
25
closeSurface: vi.fn(),
26
}));
@@ -61,17 +62,28 @@ const deviceState: DeviceServiceState = {
62
revision: 1,
63
};
64
let currentState = deviceState;
64
-vi.mock("~/state/device", () => ({
65
- deviceEnvironment: { list: "list", open: "open", close: "close" },
66
- useDeviceState: () => ({ state: currentState, loaded: true }),
67
-}));
65
+// One probe per environment, however many views mount it; each probe sends one list.
66
+vi.mock("~/state/device", async () => {
67
+ const Effect = await import("effect/Effect");
68
+ const { AsyncResult, Atom } = await import("effect/reactivity");
69
+ return {
70
+ deviceEnvironment: { list: "list", open: "open", close: "close" },
71
+ deviceHostProbeAtom: Atom.family((environmentId: string) =>
72
+ Atom.make(() => Effect.promise(() => mocks.list({ environmentId, input: {} }))),
73
+ ),
74
+ NO_DEVICE_PROBE_ATOM: Atom.make(AsyncResult.initial<void, never>()),
75
+ useDeviceState: () => ({ state: currentState, loaded: true }),
76
+ };
77
+});
78
vi.mock("~/state/use-atom-command", () => ({
79
useAtomCommand: (command: string) =>
80
command === "open"
81
? mocks.open
82
: command === "close"
83
? mocks.close
74
- : () => Promise.resolve({ _tag: "Success", value: {} }),
84
+ : command === "list"
85
+ ? mocks.list
86
+ : () => Promise.resolve({ _tag: "Success", value: {} }),
87
}));
88
vi.mock("~/state/query", () => ({ formatEnvironmentQueryError: () => "Device failed" }));
89
vi.mock("~/rightPanelStore", () => ({
@@ -141,6 +153,7 @@ const errors = () => container.querySelectorAll('[role="alert"]');
153
154
function setup() {
155
vi.resetAllMocks();
156
+ mocks.list.mockResolvedValue({ _tag: "Success", value: { hostId: "local", deviceId: "" } });
157
currentState = deviceState;
158
container = document.createElement("div");
159
container.style.width = "480px";
@@ -185,3 +198,18 @@ describe("DevicePanel thread switches", () => {
198
]);
199
});
200
});
201
+
202
+describe("DevicePanel host probe", () => {
203
+ it("probes the hosts once while setup shows inside the panel", async () => {
204
+ setup();
205
+ currentState = { ...deviceState, onboardingCompleted: false };
206
+ // An environment no other test probed, so its probe starts fresh.
207
+ const threadRef = { ...threadA, environmentId: EnvironmentId.make("environment-setup") };
208
+ await using _screen = await render(devicePanel(threadRef), { container });
209
+
210
+ await vi.waitFor(() => expect(mocks.list).toHaveBeenCalled());
211
+ expect(mocks.list.mock.calls).toEqual([
212
+ [{ environmentId: threadRef.environmentId, input: {} }],
213
+ ]);
214
+ });
215
+});
apps/web/src/components/device/DevicePanel.tsx
+13
-7
@@ -6,7 +6,8 @@ import type {
6
ScopedThreadRef,
7
} from "@t3tools/contracts";
8
import { ArrowUpRight, CircleAlert, Play, Smartphone, X } from "lucide-react";
9
-import { type ReactNode, useEffect, useState } from "react";
9
+import { useAtomMount } from "@effect/atom-react";
10
+import { type ReactNode, useState } from "react";
11
12
import { usePreviewMiniPlayerStore } from "~/previewMiniPlayerStore";
13
import {
@@ -21,7 +22,12 @@ import { WizardPopup } from "~/components/ui/wizard";
22
import { Spinner } from "~/components/ui/spinner";
23
import { cn } from "~/lib/utils";
24
import { focusPanelTerminalAfterClose } from "~/lib/terminalFocus";
24
-import { deviceEnvironment, useDeviceState } from "~/state/device";
25
+import {
26
+ deviceEnvironment,
27
+ deviceHostProbeAtom,
28
+ NO_DEVICE_PROBE_ATOM,
29
+ useDeviceState,
30
+} from "~/state/device";
31
import { formatEnvironmentQueryError } from "~/state/query";
32
import { useAtomCommand } from "~/state/use-atom-command";
33
import { DeviceLoadingView } from "./DeviceLoadingView";
@@ -61,11 +67,11 @@ export function DevicePanel(props: {
67
68
const hostDisabled = state.hostStatus === "disabled";
69
64
- // Opening setup never grants permission to install or start helpers.
65
- useEffect(() => {
66
- if (!props.visible || !loaded || hostDisabled) return;
67
- void list({ environmentId, input: {} });
68
- }, [environmentId, list, loaded, props.visible, hostDisabled]);
70
+ useAtomMount(
71
+ props.visible && loaded && !hostDisabled
72
+ ? deviceHostProbeAtom(environmentId)
73
+ : NO_DEVICE_PROBE_ATOM,
74
+ );
75
76
const sessions = state.sessions.filter((session) => session.threadId === threadId);
77
const activeSession = props.surface.target
apps/web/src/components/device/DevicePhoneViewport.tsx
+39
-75
@@ -1,14 +1,13 @@
1
-import { useEffect, useRef, type RefObject } from "react";
1
+import { useEffect, useRef } from "react";
2
import type { PhoneViewer } from "@t3tools/client-runtime/device/phone-viewer";
3
import type {
4
DeviceAccessorySource,
5
DeviceModelSource,
6
} from "@t3tools/client-runtime/device/model";
7
import type { DeviceShapeProfile } from "@t3tools/client-runtime/device/shape-profile";
8
-import { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
8
+import type { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
9
import type { DeviceScreenSize, DeviceStreamClient } from "@t3tools/client-runtime/device/stream";
10
-import { bindPhoneTrackpad } from "./phoneTrackpad";
11
-import { observeResize } from "../../lib/observeResize";
10
+import { mountDeviceViewer } from "./mountDeviceViewer";
11
12
const loadPhoneViewer = () => import("@t3tools/client-runtime/device/phone-viewer");
13
@@ -16,17 +15,15 @@ export function DevicePhoneViewport(props: {
15
readonly profile: DeviceShapeProfile;
16
readonly model: DeviceModelSource | null;
17
readonly accessory: DeviceAccessorySource | null;
19
- readonly source: RefObject<HTMLCanvasElement | null>;
18
+ readonly source: HTMLCanvasElement;
19
readonly onFrameListener: (listener: (() => void) | null) => void;
20
readonly onResetReady: (reset: (() => void) | null) => void;
21
readonly onInputCancel: (cancel: (() => void) | null) => void;
23
- readonly client: RefObject<DeviceStreamClient | null>;
22
+ readonly client: DeviceStreamClient;
23
readonly screen: DeviceScreenSize | null;
24
readonly foldAngle: number | null;
25
readonly onUnavailable: () => void;
26
}) {
28
- const hostRef = useRef<HTMLDivElement | null>(null);
29
- const canvasRef = useRef<HTMLCanvasElement | null>(null);
27
const viewerRef = useRef<PhoneViewer | null>(null);
28
const interactionRef = useRef<ReturnType<typeof createPhoneInteraction> | null>(null);
29
const screenRef = useRef(props.screen);
@@ -52,72 +49,39 @@ export function DevicePhoneViewport(props: {
49
viewerRef.current?.setScreen(props.screen, props.profile);
50
}, [props.screen, props.profile, props.model, props.accessory]);
51
55
- useEffect(() => {
56
- const host = hostRef.current;
57
- const canvas = canvasRef.current;
58
- const decoded = source.current;
59
- if (!host || !canvas || !decoded) return;
60
- let disposed = false;
61
- let trackpad: ReturnType<typeof bindPhoneTrackpad> | null = null;
62
- let stopTrackpadEnd: (() => void) | undefined;
63
- const resize = () => {
64
- const { width, height } = host.getBoundingClientRect();
65
- viewerRef.current?.resize(width, height, window.devicePixelRatio);
66
- };
67
- const stopObserving = observeResize(host, resize);
68
- const blur = () => {
69
- interactionRef.current?.end();
70
- trackpad?.cancel();
71
- };
72
- onInputCancel(blur);
73
- window.addEventListener("blur", blur);
74
- void loadPhoneViewer()
75
- .then(({ createPhoneViewer }) => {
76
- if (disposed) return;
77
- const viewer = createPhoneViewer({
78
- canvas,
79
- source: decoded,
80
- onUnavailable,
81
- onModelError: (cause) => console.warn("Device 3D asset could not load", cause),
82
- profile: profileRef.current,
83
- model: modelRef.current,
84
- accessory: accessoryRef.current,
85
- foldAngle: foldAngleRef.current,
86
- });
87
- viewerRef.current = viewer;
88
- onResetReady(viewer.resetPose);
52
+ // The 3D viewer lives as long as its canvas; later prop changes are pushed into it above.
53
+ const mountViewer = (canvas: HTMLCanvasElement | null) => {
54
+ const host = canvas?.parentElement;
55
+ if (!canvas || !host) return;
56
+ return mountDeviceViewer(canvas, host, {
57
+ load: () =>
58
+ loadPhoneViewer().then(
59
+ ({ createPhoneViewer }) =>
60
+ () =>
61
+ createPhoneViewer({
62
+ canvas,
63
+ source,
64
+ onUnavailable,
65
+ onModelError: (cause) => console.warn("Device 3D asset could not load", cause),
66
+ profile: profileRef.current,
67
+ model: modelRef.current,
68
+ accessory: accessoryRef.current,
69
+ foldAngle: foldAngleRef.current,
70
+ }),
71
+ ),
72
+ touch: (phase, point) => client.sendTouch(phase, point.x, point.y),
73
+ viewerRef,
74
+ interactionRef,
75
+ onInputCancel,
76
+ onResetReady,
77
+ onFrameListener,
78
+ onUnavailable,
79
+ ready: ({ viewer }) => {
80
viewer.setScreen(screenRef.current);
90
- onFrameListener(viewer.frameUpdated);
91
- interactionRef.current = createPhoneInteraction({
92
- screenPoint: (point, captured) => viewer.screenPoint(point.x, point.y, captured),
93
- touch: (phase, point) => client.current?.sendTouch(phase, point.x, point.y),
94
- orbit: viewer.orbit,
95
- zoomBy: () => {},
96
- onInteractionActive: viewer.setInteractionActive,
97
- });
98
- trackpad = bindPhoneTrackpad(canvas, interactionRef.current);
99
- stopTrackpadEnd = window.desktopBridge?.onTrackpadScrollEnd?.(() => trackpad?.endOrbit());
100
- resize();
101
- viewer.frameUpdated();
102
- })
103
- .catch(() => {
104
- if (!disposed) onUnavailable();
105
- });
106
- return () => {
107
- disposed = true;
108
- stopTrackpadEnd?.();
109
- trackpad?.dispose();
110
- interactionRef.current?.end();
111
- interactionRef.current = null;
112
- onInputCancel(null);
113
- onResetReady(null);
114
- onFrameListener(null);
115
- stopObserving();
116
- window.removeEventListener("blur", blur);
117
- viewerRef.current?.dispose();
118
- viewerRef.current = null;
119
- };
120
- }, [onInputCancel, onResetReady, client, onFrameListener, onUnavailable, source]);
81
+ return { frame: viewer.frameUpdated };
82
+ },
83
+ });
84
+ };
85
86
const point = (event: React.PointerEvent<HTMLCanvasElement>) => {
87
const rect = event.currentTarget.getBoundingClientRect();
@@ -128,13 +92,13 @@ export function DevicePhoneViewport(props: {
92
};
93
return (
94
<div className="absolute inset-0">
131
- <div ref={hostRef} className="absolute inset-0">
95
+ <div className="absolute inset-0">
96
<div
97
aria-hidden
98
className="pointer-events-none absolute bottom-[6%] left-1/2 h-5 w-2/5 -translate-x-1/2 rounded-full bg-foreground/10 blur-xl"
99
/>
100
<canvas
137
- ref={canvasRef}
101
+ ref={mountViewer}
102
aria-label="Interactive 3D device. Drag the screen to interact. Drag outside it or swipe with two fingers to turn."
103
className="size-full touch-none"
104
onPointerDown={(event) => {
apps/web/src/components/device/DeviceSetup.tsx
+4
-6
@@ -3,7 +3,8 @@ import type { DevicePlatform, DeviceServiceState, EnvironmentId } from "@t3tools
3
import { Check } from "lucide-react";
4
import { Check as CheckGlyph, CircleAlert } from "lucide";
5
import { AuthSettingsWriteScope } from "@t3tools/contracts";
6
-import { useEffect, useState } from "react";
6
+import { useAtomMount } from "@effect/atom-react";
7
+import { useState } from "react";
8
9
import { Button } from "~/components/ui/button";
10
import { DialogClose } from "~/components/ui/dialog";
@@ -11,7 +12,7 @@ import { MorphIcon } from "~/components/MorphIcon";
12
import { WizardHeader, WizardPanel, WizardSteps, WizardFooter } from "~/components/ui/wizard";
13
import { Spinner } from "~/components/ui/spinner";
14
import { Switch } from "~/components/ui/switch";
14
-import { deviceEnvironment } from "~/state/device";
15
+import { deviceEnvironment, deviceHostProbeAtom, NO_DEVICE_PROBE_ATOM } from "~/state/device";
16
import { useAtomCommand } from "~/state/use-atom-command";
17
import { readEnvironmentScope, useEnvironmentScope } from "~/state/session";
18
import { cn } from "~/lib/utils";
@@ -141,10 +142,7 @@ export function DeviceSetup(props: {
142
(host) => host.kind === "local" && !host.platforms.some((platform) => platform.available),
143
);
144
144
- // Remote hosts report platforms only after a probe, like the Device panel's refresh on open.
145
- useEffect(() => {
146
- if (enabled) void list({ environmentId: props.environmentId, input: {} });
147
- }, [enabled, list, props.environmentId]);
145
+ useAtomMount(enabled ? deviceHostProbeAtom(props.environmentId) : NO_DEVICE_PROBE_ATOM);
146
147
const update = async (
148
kind: NonNullable<typeof pending>,
apps/web/src/components/device/DeviceStreamView.tsx
+106
-179
@@ -1,31 +1,38 @@
1
import type { DevicePlatform, EnvironmentId } from "@t3tools/contracts";
2
-import { useEffect, useRef, useState, type ReactNode } from "react";
2
+import { useAtomValue } from "@effect/atom-react";
3
+import * as Option from "effect/Option";
4
+import { AsyncResult, Atom } from "effect/reactivity";
5
+import { useId, useRef, useState, type ReactNode } from "react";
6
7
import { cn } from "~/lib/utils";
8
import { Button } from "~/components/ui/button";
6
-import { refreshDeviceHubAccess, renewDeviceHubAccess, useDeviceHubAccess } from "~/state/device";
7
-import { createCanvasFrameSink } from "@t3tools/client-runtime/device/frame";
9
+import { renewDeviceHubAccess, useDeviceHubAccess } from "~/state/device";
10
import { resolveDeviceShape } from "@t3tools/client-runtime/device/shape-profile";
11
import { deviceKeyboard, deviceModel } from "./deviceModels";
12
import { fitDeviceFrame } from "./deviceFrameLayout";
13
import { DeviceDuoViewport } from "./DeviceDuoViewport";
14
import { DeviceDuoControls } from "./DeviceDuoControls";
15
import { DeviceAndroidFoldControls } from "./DeviceAndroidFoldControls";
14
-import type { DuoControlState } from "@t3tools/client-runtime/device/duo-control";
16
import { DevicePhoneViewport } from "./DevicePhoneViewport";
17
import { DeviceLoadingView } from "./DeviceLoadingView";
17
-import { type DeviceAxElement, fetchDeviceAxTree } from "./deviceHubApi";
18
+import type { DeviceAxElement } from "./deviceHubApi";
19
+import { androidFoldViewAtom, deviceAxElementsAtom } from "./deviceHubAtoms";
20
import {
19
- createDeviceStreamClient,
20
- type DeviceHardwareButton,
21
- type DeviceScreenSize,
22
- type DeviceStreamClient,
23
- type DeviceStreamStatus,
24
-} from "@t3tools/client-runtime/device/stream";
21
+ deviceScreenKey,
22
+ deviceStreamSessionAtom,
23
+ type DeviceStreamSession,
24
+ IDLE_DEVICE_STREAM_STATE,
25
+} from "./deviceStreamSession";
26
+import type { DeviceHardwareButton, DeviceScreenSize } from "@t3tools/client-runtime/device/stream";
27
import { observeResize } from "~/lib/observeResize";
26
-import { useStableCallback } from "~/hooks/useStableCallback";
28
28
-const AX_POLL_INTERVAL_MS = 2_000;
29
+const NO_SESSION_ATOM = Atom.make<DeviceStreamSession | null>(null).pipe(
30
+ Atom.withLabel("device-stream:none"),
31
+);
32
+const NO_AX_ELEMENTS: ReadonlyArray<DeviceAxElement> = [];
33
+const NO_AX_ATOM = Atom.make(AsyncResult.initial<ReadonlyArray<DeviceAxElement>, never>()).pipe(
34
+ Atom.withLabel("device-ax-elements:none"),
35
+);
36
const CONTROLS_RAIL_WIDTH = 56;
37
38
function screenAspect(platform: DevicePlatform, screen: DeviceScreenSize | null): number {
@@ -65,6 +72,7 @@ export interface DeviceViewControls {
72
readonly resetView: () => void;
73
readonly foldingControls?: ReactNode;
74
readonly keyboard: { readonly attached: boolean; readonly toggle: () => void } | null;
75
+ readonly handle: DeviceStreamHandle | null;
76
}
77
78
export interface DeviceStreamHandle {
@@ -84,120 +92,56 @@ export function DeviceStreamView(props: {
92
readonly allowPhoneView?: boolean;
93
readonly renderControls?: (view: DeviceViewControls) => ReactNode;
94
readonly axOverlay?: boolean;
87
- readonly onHandle?: (handle: DeviceStreamHandle | null) => void;
88
- readonly onScreen?: (screen: DeviceScreenSize | null) => void;
95
+ /** Lets a parent read this view's own stream state, such as its screen size. */
96
+ readonly viewId?: string;
97
}) {
90
- const [duoControl, setDuoControl] = useState<DuoControlState>({
91
- pending: false,
92
- requested: null,
93
- error: null,
94
- });
98
const model = deviceModel(props.platform, props.deviceName ?? "");
99
const isDuo = model?.id === "iphone-duo";
100
const [presentation, setPresentation] = useState<"phone" | "flat">("phone");
101
const [keyboardAttached, setKeyboardAttached] = useState(false);
99
- const [phoneUnavailable, setPhoneUnavailable] = useState(false);
100
- const onPhoneUnavailable = useStableCallback(() => setPhoneUnavailable(true));
102
const cancelPhoneInputRef = useRef<(() => void) | null>(null);
102
- const cancelPhoneInput = useStableCallback(() => cancelPhoneInputRef.current?.());
103
+ const cancelPhoneInput = () => cancelPhoneInputRef.current?.();
104
const resetViewRef = useRef<(() => void) | null>(null);
104
- const onResetReady = useStableCallback((reset: (() => void) | null) => {
105
+ const onResetReady = (reset: (() => void) | null) => {
106
resetViewRef.current = reset;
106
- });
107
- const frameListenerRef = useRef<(() => void) | null>(null);
108
- const onFrameListener = useStableCallback((listener: (() => void) | null) => {
109
- frameListenerRef.current = listener;
110
- });
111
- const onInputCancel = useStableCallback((cancel: (() => void) | null) => {
107
+ };
108
+ const onInputCancel = (cancel: (() => void) | null) => {
109
cancelPhoneInputRef.current = cancel;
113
- });
110
+ };
111
const access = useDeviceHubAccess(props.environmentId, props.hostId);
115
- const canvasRef = useRef<HTMLCanvasElement | null>(null);
116
- const clientRef = useRef<DeviceStreamClient | null>(null);
117
- const [status, setStatus] = useState<DeviceStreamStatus>("connecting");
118
- const [hasFrame, setHasFrame] = useState(false);
119
- const [detail, setDetail] = useState<string | undefined>(undefined);
120
- const [showRestartNotice, setShowRestartNotice] = useState(false);
121
- const [screen, setScreen] = useState<DeviceScreenSize | null>(null);
122
- const [foldAngle, setFoldAngle] = useState<number | null>(null);
123
- const [mjpegUrl, setMjpegUrl] = useState<string | null>(null);
124
- const [mjpegGeneration, setMjpegGeneration] = useState(0);
125
- const attachMjpegImage = useStableCallback((image: HTMLImageElement | null) => {
126
- clientRef.current?.setMjpegImage(image);
127
- });
128
- const [inputState, setInputState] = useState<{ connected: boolean; detail?: string }>({
129
- connected: false,
130
- });
131
- const { onHandle, onScreen } = props;
132
-
133
- useEffect(() => {
134
- const canvas = canvasRef.current;
135
- if (!access || !canvas || !props.visible) {
136
- setStatus("connecting");
137
- onHandle?.(null);
138
- return;
139
- }
140
- const client = createDeviceStreamClient(
141
- { platform: props.platform, deviceId: props.deviceId, access },
142
- createCanvasFrameSink(canvas, () => frameListenerRef.current?.()),
143
- {
144
- onDuoControl: setDuoControl,
145
- onDuoUnavailable: onPhoneUnavailable,
146
- onStatus: (next, nextDetail) => {
147
- setStatus(next);
148
- if (next === "streaming") setHasFrame(true);
149
- setDetail(nextDetail);
150
- if (next !== "connecting") setShowRestartNotice(false);
151
- },
152
- onScreen: (next) => {
153
- setScreen(next);
154
- onScreen?.(next);
155
- },
156
- onUnauthorized: () => {
157
- refreshDeviceHubAccess(props.environmentId);
158
- },
159
- onMjpegFallback: (url) => {
160
- setMjpegUrl(url);
161
- setMjpegGeneration((generation) => generation + 1);
162
- },
163
- onCanvasVideo: () => setMjpegUrl(null),
164
- onInputConnected: (connected, detail) => {
165
- setInputState({ connected, ...(detail ? { detail } : {}) });
166
- if (!connected) setShowRestartNotice(false);
167
- onHandle?.({
168
- pressButton: client.pressButton,
169
- rotate: client.rotate,
170
- inputConnected: connected,
171
- });
172
- },
173
- },
174
- );
175
- clientRef.current = client;
176
- setPhoneUnavailable(false);
177
- setHasFrame(false);
178
- setMjpegUrl(null);
179
- setInputState({ connected: false });
180
- client.start();
181
- onHandle?.({ pressButton: client.pressButton, rotate: client.rotate, inputConnected: false });
182
- return () => {
183
- cancelPhoneInput();
184
- client.stop();
185
- clientRef.current = null;
186
- onHandle?.(null);
187
- onScreen?.(null);
188
- setScreen(null);
189
- };
190
- }, [
191
- access,
192
- cancelPhoneInput,
193
- onHandle,
194
- onPhoneUnavailable,
195
- onScreen,
196
- props.deviceId,
197
- props.environmentId,
198
- props.platform,
199
- props.visible,
200
- ]);
112
+ const ownViewId = useId();
113
+ const viewId = props.viewId ?? ownViewId;
114
+ const session = useAtomValue(
115
+ access && props.visible
116
+ ? deviceStreamSessionAtom({
117
+ environmentId: props.environmentId,
118
+ hostId: props.hostId,
119
+ platform: props.platform,
120
+ deviceId: props.deviceId,
121
+ access,
122
+ viewId,
123
+ })
124
+ : NO_SESSION_ATOM,
125
+ );
126
+ const client = session?.stream ?? null;
127
+ const canvas = session?.canvas ?? null;
128
+ const { status, detail, hasFrame, screen, mjpegUrl, mjpegGeneration, duoControl } =
129
+ session?.state ?? IDLE_DEVICE_STREAM_STATE;
130
+ const inputState = session?.state.input ?? IDLE_DEVICE_STREAM_STATE.input;
131
+ const phoneUnavailable = session?.state.phoneUnavailable ?? false;
132
+ const foldAngle = useAtomValue(
133
+ androidFoldViewAtom(deviceScreenKey(props.environmentId, props.hostId, props.deviceId)),
134
+ ).angle;
135
+ const attachMjpegImage = (image: HTMLImageElement | null) => {
136
+ if (!client || !image) return;
137
+ client.setMjpegImage(image);
138
+ return () => client.setMjpegImage(null);
139
+ };
140
+ const mountCanvas = (node: HTMLDivElement | null) => {
141
+ if (!node || !canvas) return;
142
+ node.append(canvas);
143
+ return () => canvas.remove();
144
+ };
145
146
const aspect = screenAspect(props.platform, screen);
147
@@ -212,18 +156,11 @@ export function DeviceStreamView(props: {
156
!mjpegUrl &&
157
!props.axOverlay &&
158
(!isDuo || screen?.supportsHingeAngle === true);
215
- useEffect(() => {
216
- if (!retainingFrame || !showPhone) return;
217
- const timeout = window.setTimeout(() => setShowRestartNotice(true), 2_000);
218
- return () => window.clearTimeout(timeout);
219
- }, [retainingFrame, showPhone]);
159
const controlsInset = props.renderControls && !showPhone ? CONTROLS_RAIL_WIDTH : 0;
160
161
// Fit the frame in both dimensions; pinned CSS height plus a width clamp distorts aspect-ratio.
223
- const hostRef = useRef<HTMLDivElement | null>(null);
162
const [host, setHost] = useState({ width: 0, height: 0 });
225
- useEffect(() => {
226
- const element = hostRef.current;
163
+ const hostRef = (element: HTMLDivElement | null) => {
164
if (!element) return;
165
const update = () => {
166
const rect = element.getBoundingClientRect();
@@ -235,7 +172,7 @@ export function DeviceStreamView(props: {
172
};
173
update();
174
return observeResize(element, update);
238
- }, []);
175
+ };
176
const frame = fitDeviceFrame(aspect, host.width, host.height, controlsInset);
177
178
const rotation = screenRotation(props.platform, screen);
@@ -255,29 +192,12 @@ export function DeviceStreamView(props: {
192
...(rotation ? { transform: `rotate(${rotation}deg)` } : {}),
193
};
194
258
- const [axElements, setAxElements] = useState<ReadonlyArray<DeviceAxElement>>([]);
259
- useEffect(() => {
260
- if (!props.axOverlay || !access || !props.visible) return;
261
- const target = { access, platform: props.platform, deviceId: props.deviceId };
262
- let controller: AbortController | null = null;
263
- let timer: ReturnType<typeof setTimeout> | null = null;
264
- let stopped = false;
265
- const poll = async () => {
266
- controller = new AbortController();
267
- try {
268
- const tree = await fetchDeviceAxTree(target, controller.signal);
269
- if (!stopped) setAxElements(tree.elements);
270
- } catch {}
271
- if (!stopped) timer = setTimeout(() => void poll(), AX_POLL_INTERVAL_MS);
272
- };
273
- void poll();
274
- return () => {
275
- stopped = true;
276
- controller?.abort();
277
- if (timer) clearTimeout(timer);
278
- setAxElements([]);
279
- };
280
- }, [access, props.axOverlay, props.deviceId, props.platform, props.visible]);
195
+ const axResult = useAtomValue(
196
+ props.axOverlay && access && props.visible
197
+ ? deviceAxElementsAtom({ access, platform: props.platform, deviceId: props.deviceId })
198
+ : NO_AX_ATOM,
199
+ );
200
+ const axElements = Option.getOrElse(AsyncResult.value(axResult), () => NO_AX_ELEMENTS);
201
202
const pointerActive = useRef(false);
203
const normalizedPoint = (event: React.PointerEvent<HTMLElement>) => {
@@ -302,7 +222,7 @@ export function DeviceStreamView(props: {
222
const resetView = () => {
223
if (!isDuo) {
224
const orientation = keyboardAttached ? "landscape_right" : "portrait";
305
- if (screen?.orientation !== orientation) clientRef.current?.setOrientation(orientation);
225
+ if (screen?.orientation !== orientation) client?.setOrientation(orientation);
226
}
227
resetViewRef.current?.();
228
};
@@ -336,7 +256,7 @@ export function DeviceStreamView(props: {
256
enabled={status === "streaming"}
257
screenWidth={screen?.width}
258
screenHeight={screen?.height}
339
- onFoldAngle={setFoldAngle}
259
+ deviceKey={deviceScreenKey(props.environmentId, props.hostId, props.deviceId)}
260
renewAccess={() => renewDeviceHubAccess(props.environmentId, props.hostId)}
261
/>
262
) : showPhone && isDuo && screen?.supportsHingeAngle ? (
@@ -346,7 +266,7 @@ export function DeviceStreamView(props: {
266
enabled={inputState.connected}
267
onCommand={(command) => {
268
cancelPhoneInput();
349
- clientRef.current?.controlDuo(command);
269
+ client?.controlDuo(command);
270
}}
271
/>
272
) : null,
@@ -357,13 +277,20 @@ export function DeviceStreamView(props: {
277
toggle: () => {
278
cancelPhoneInput();
279
if (!keyboardAttached && screen?.orientation !== "landscape_right")
360
- clientRef.current?.setOrientation("landscape_right");
280
+ client?.setOrientation("landscape_right");
281
setKeyboardAttached(!keyboardAttached);
282
},
283
}
284
: null,
285
showPhone: () => setPresentation("phone"),
286
showFlat: () => setPresentation("flat"),
287
+ handle: client
288
+ ? {
289
+ pressButton: client.pressButton,
290
+ rotate: client.rotate,
291
+ inputConnected: inputState.connected,
292
+ }
293
+ : null,
294
}}
295
onResetView={resetView}
296
/>
@@ -390,16 +317,16 @@ export function DeviceStreamView(props: {
317
return;
318
}
319
event.preventDefault();
393
- clientRef.current?.sendKey(event.nativeEvent, "down");
320
+ client?.sendKey(event.nativeEvent, "down");
321
}}
322
onPaste={(event) => {
323
if (event.target !== event.currentTarget) return;
324
event.preventDefault();
398
- clientRef.current?.sendText(event.clipboardData.getData("text/plain"));
325
+ client?.sendText(event.clipboardData.getData("text/plain"));
326
}}
327
onKeyUp={(event) => {
328
if (event.target !== event.currentTarget) return;
402
- clientRef.current?.sendKey(event.nativeEvent, "up");
329
+ client?.sendKey(event.nativeEvent, "up");
330
}}
331
>
332
<div
@@ -411,28 +338,28 @@ export function DeviceStreamView(props: {
338
(event.currentTarget.parentElement as HTMLElement | null)?.focus();
339
pointerActive.current = true;
340
const { x, y } = normalizedPoint(event);
414
- clientRef.current?.sendTouch("begin", x, y);
341
+ client?.sendTouch("begin", x, y);
342
}}
343
onPointerMove={(event) => {
344
if (!pointerActive.current) return;
345
const { x, y } = normalizedPoint(event);
419
- clientRef.current?.sendTouch("move", x, y);
346
+ client?.sendTouch("move", x, y);
347
}}
348
onPointerUp={(event) => {
349
if (!pointerActive.current) return;
350
pointerActive.current = false;
351
const { x, y } = normalizedPoint(event);
425
- clientRef.current?.sendTouch("end", x, y);
352
+ client?.sendTouch("end", x, y);
353
}}
354
onPointerCancel={(event) => {
355
if (!pointerActive.current) return;
356
pointerActive.current = false;
357
const { x, y } = normalizedPoint(event);
431
- clientRef.current?.sendTouch("end", x, y);
358
+ client?.sendTouch("end", x, y);
359
}}
360
>
434
- <canvas
435
- ref={canvasRef}
361
+ <div
362
+ ref={mountCanvas}
363
className={cn("absolute top-0 left-0", mjpegUrl && "hidden")}
364
style={mediaStyle}
365
/>
@@ -469,34 +396,34 @@ export function DeviceStreamView(props: {
396
</div>
397
) : null}
398
</div>
472
- {showPhone && isDuo && model ? (
399
+ {session && showPhone && isDuo && model ? (
400
<DeviceDuoViewport
474
- onFrameListener={onFrameListener}
401
+ onFrameListener={session.setFrameListener}
402
+ onControlRejectedListener={session.setControlRejectedListener}
403
model={model}
476
- controlError={duoControl.error}
404
hingePreview={
405
duoControl.requested?.control === "angle" ? duoControl.requested.value : null
406
}
480
- source={canvasRef}
481
- client={clientRef}
407
+ source={session.canvas}
408
+ client={session.stream}
409
onInputCancel={onInputCancel}
410
onResetReady={onResetReady}
411
screen={screen}
485
- onUnavailable={onPhoneUnavailable}
412
+ onUnavailable={session.markPhoneUnavailable}
413
/>
487
- ) : showPhone ? (
414
+ ) : session && showPhone ? (
415
<DevicePhoneViewport
416
profile={profile}
417
model={deviceModel(props.platform, props.deviceName ?? "")}
418
accessory={keyboardAttached ? keyboardSource : null}
492
- source={canvasRef}
493
- onFrameListener={onFrameListener}
494
- client={clientRef}
419
+ source={session.canvas}
420
+ onFrameListener={session.setFrameListener}
421
+ client={session.stream}
422
onInputCancel={onInputCancel}
423
onResetReady={onResetReady}
424
screen={screen}
425
foldAngle={foldAngle}
499
- onUnavailable={onPhoneUnavailable}
426
+ onUnavailable={session.markPhoneUnavailable}
427
/>
428
) : null}
429
{props.allowPhoneView && !props.renderControls && status === "streaming" ? (
@@ -528,8 +455,9 @@ export function DeviceStreamView(props: {
455
</span>
456
</div>
457
) : null}
531
- {retainingFrame && showPhone && showRestartNotice ? (
532
- <div className="pointer-events-none absolute inset-x-0 bottom-0 flex justify-center p-2">
458
+ {retainingFrame && showPhone ? (
459
+ // Shown only once the device has been silent for two seconds.
460
+ <div className="pointer-events-none absolute inset-x-0 bottom-0 flex justify-center p-2 transition-opacity delay-2000 duration-0 starting:opacity-0">
461
<span className="rounded-md bg-background/85 px-2 py-1 text-xs text-muted-foreground">
462
Waiting for device video…
463
</span>
@@ -549,10 +477,9 @@ export function DeviceStreamView(props: {
477
size="sm"
478
variant="outline"
479
onClick={() => {
552
- // Restart's unauthorized response renews expired tickets through the effect.
553
- clientRef.current?.stop();
554
- setPhoneUnavailable(false);
555
- clientRef.current?.start();
480
+ // An expired ticket surfaces as unauthorized on restart and
481
+ // refreshes access, which starts a new session.
482
+ session?.reconnect();
483
}}
484
>
485
Reconnect
apps/web/src/components/device/DeviceToolsPanel.tsx
+32
-36
@@ -1,7 +1,8 @@
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 { useEffect, useState } from "react";
4
+import { useAtomValue } from "@effect/atom-react";
5
+import { useState } from "react";
6
7
import { Button } from "~/components/ui/button";
8
import { Collapsible, CollapsiblePanel, CollapsibleTrigger } from "~/components/ui/collapsible";
@@ -18,7 +19,7 @@ import { Switch } from "~/components/ui/switch";
19
import { Toggle, ToggleGroup } from "~/components/ui/toggle-group";
20
import { cn } from "~/lib/utils";
21
import type { DeviceControls } from "./useDeviceControls";
21
-import { type DeviceEventLogEntry, subscribeDeviceEventLog } from "./deviceHubApi";
22
+import { deviceEventLogAtom } from "./deviceHubAtoms";
23
24
const TEXT_SIZES: ReadonlyArray<{ value: DeviceTextSize; label: string }> = [
25
{ value: "small", label: "Small" },
@@ -599,31 +600,11 @@ function PermissionsSection(props: {
600
);
601
}
602
602
-const EVENT_LOG_LIMIT = 100;
603
-
603
function EventLogSection(props: {
604
readonly access: DeviceHubAccess;
605
readonly device: DeviceSummary;
606
}) {
607
const [open, setOpen] = useState(false);
609
- const [entries, setEntries] = useState<ReadonlyArray<DeviceEventLogEntry>>([]);
610
-
611
- useEffect(() => {
612
- if (!open) return;
613
- const unsubscribe = subscribeDeviceEventLog(
614
- { access: props.access, platform: props.device.platform, deviceId: props.device.id },
615
- (incoming, reset) => {
616
- setEntries((current) => {
617
- const merged = reset ? [...incoming] : [...current, ...incoming];
618
- return merged.length > EVENT_LOG_LIMIT ? merged.slice(-EVENT_LOG_LIMIT) : merged;
619
- });
620
- },
621
- );
622
- return () => {
623
- unsubscribe();
624
- setEntries([]);
625
- };
626
- }, [open, props.access, props.device.id, props.device.platform]);
608
609
return (
610
<Collapsible open={open} onOpenChange={setOpen}>
@@ -634,21 +615,36 @@ function EventLogSection(props: {
615
/>
616
</CollapsibleTrigger>
617
<CollapsiblePanel>
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">
644
- {entry.timestamp.slice(11, 19)}
645
- </span>
646
- <span className="truncate">{entry.summary}</span>
647
- </li>
648
- ))
649
- )}
650
- </ol>
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.
625
+function 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
+}
apps/web/src/components/device/DeviceWorkspace.tsx
+20
-33
@@ -1,15 +1,17 @@
1
import type { DeviceSummary, EnvironmentId } from "@t3tools/contracts";
2
-import { useEffect, useRef, useState } from "react";
2
+import { useRef, useState } from "react";
3
import {
4
captureDeviceScreenshot,
5
DeviceScreenshotError,
6
} from "@t3tools/client-runtime/device/screenshot";
7
import { refreshDeviceHubAccess, useDeviceHubAccess } from "~/state/device";
8
import { DeviceControlsRail } from "./DeviceControlsRail";
9
-import { DeviceStreamView, type DeviceStreamHandle } from "./DeviceStreamView";
9
+import { DeviceStreamView } from "./DeviceStreamView";
10
import { DeviceToolsPanel } from "./DeviceToolsPanel";
11
import { useDeviceControls } from "./useDeviceControls";
12
13
+const DOWNLOAD_URL_LIFETIME_MS = 60_000;
14
+
15
export function DeviceWorkspace(props: {
16
environmentId: EnvironmentId;
17
device: DeviceSummary;
@@ -20,59 +22,45 @@ export function DeviceWorkspace(props: {
22
onClose: () => void;
23
onPowerOff: () => void;
24
}) {
23
- const [handle, setHandle] = useState<DeviceStreamHandle | null>(null);
25
const [toolsOpen, setToolsOpen] = useState(false);
26
const [axOverlay, setAxOverlay] = useState(false);
27
const [screenshotPending, setScreenshotPending] = useState(false);
28
const [screenshotError, setScreenshotError] = useState<string | null>(null);
28
- const captureRef = useRef<AbortController | null>(null);
29
- const downloadRef = useRef<string | null>(null);
29
+ // A capture always finishes, even if the panel hides or closes meanwhile; the user asked for it.
30
+ const capturing = useRef(false);
31
const access = useDeviceHubAccess(props.environmentId, props.device.hostId);
32
const controls = useDeviceControls({ ...props, access });
32
- useEffect(() => {
33
- if (!access || !props.visible) captureRef.current?.abort();
34
- return () => {
35
- captureRef.current?.abort();
36
- if (downloadRef.current) URL.revokeObjectURL(downloadRef.current);
37
- downloadRef.current = null;
38
- };
39
- }, [access, props.visible]);
33
const saveScreenshot = () => {
41
- if (!access || captureRef.current || !props.visible) return;
42
- const controller = new AbortController();
43
- captureRef.current = controller;
34
+ if (!access || capturing.current || !props.visible) return;
35
+ capturing.current = true;
36
setScreenshotPending(true);
37
setScreenshotError(null);
38
return captureDeviceScreenshot(
39
{ access, platform: props.device.platform, deviceId: props.device.id },
48
- controller.signal,
40
+ // Never aborted: see `capturing` above.
41
+ new AbortController().signal,
42
)
43
.then((image) => {
51
- if (controller.signal.aborted) return;
52
- if (downloadRef.current) URL.revokeObjectURL(downloadRef.current);
44
const url = URL.createObjectURL(image);
54
- downloadRef.current = url;
45
const link = document.createElement("a");
46
link.href = url;
47
link.download = `${props.device.name.replace(/[^a-z0-9-]/gi, "-")}-${Date.now()}.png`;
48
document.body.append(link);
49
link.click();
50
link.remove();
51
+ // The browser reads the blob after the click returns.
52
+ window.setTimeout(() => URL.revokeObjectURL(url), DOWNLOAD_URL_LIFETIME_MS);
53
})
54
.catch((cause: unknown) => {
63
- if (!controller.signal.aborted) {
64
- if (cause instanceof DeviceScreenshotError && cause.status === 401)
65
- refreshDeviceHubAccess(props.environmentId);
66
- setScreenshotError(
67
- cause instanceof Error ? cause.message : "Screenshot capture failed. Try again.",
68
- );
69
- }
55
+ if (cause instanceof DeviceScreenshotError && cause.status === 401)
56
+ refreshDeviceHubAccess(props.environmentId);
57
+ setScreenshotError(
58
+ cause instanceof Error ? cause.message : "Screenshot capture failed. Try again.",
59
+ );
60
})
61
.finally(() => {
72
- if (captureRef.current === controller) {
73
- captureRef.current = null;
74
- setScreenshotPending(false);
75
- }
62
+ capturing.current = false;
63
+ setScreenshotPending(false);
64
});
65
};
66
return (
@@ -94,11 +82,10 @@ export function DeviceWorkspace(props: {
82
visible={props.visible}
83
axOverlay={axOverlay}
84
allowPhoneView
97
- onHandle={setHandle}
85
renderControls={(view) => (
86
<DeviceControlsRail
87
platform={props.device.platform}
101
- handle={handle}
88
+ handle={view.handle}
89
view={view}
90
controls={controls}
91
screenshotPending={screenshotPending}
apps/web/src/components/device/deviceHubApi.test.ts
+19
-1
@@ -1,5 +1,10 @@
1
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
2
-import { subscribeDeviceForeground } from "./deviceHubApi";
2
+import {
3
+ appendDeviceEventLog,
4
+ DEVICE_EVENT_LOG_LIMIT,
5
+ type DeviceEventLogEntry,
6
+ subscribeDeviceForeground,
7
+} from "./deviceHubApi";
8
9
describe("foreground app events", () => {
10
afterEach(() => vi.unstubAllGlobals());
@@ -42,3 +47,16 @@ describe("foreground app events", () => {
47
expect(source!.close).toHaveBeenCalledOnce();
48
});
49
});
50
+
51
+describe("event log buffer", () => {
52
+ const entry = (id: number): DeviceEventLogEntry => ({ id, timestamp: "", kind: "", summary: "" });
53
+
54
+ it("replaces the log on a reset batch and keeps only the newest entries", () => {
55
+ const full = Array.from({ length: DEVICE_EVENT_LOG_LIMIT }, (_, index) => entry(index));
56
+ const appended = appendDeviceEventLog(full, [entry(DEVICE_EVENT_LOG_LIMIT)], false);
57
+ expect(appended).toHaveLength(DEVICE_EVENT_LOG_LIMIT);
58
+ expect(appended[0]?.id).toBe(1);
59
+ expect(appended.at(-1)?.id).toBe(DEVICE_EVENT_LOG_LIMIT);
60
+ expect(appendDeviceEventLog(full, [entry(7)], true)).toEqual([entry(7)]);
61
+ });
62
+});
apps/web/src/components/device/deviceHubApi.ts
+24
-8
@@ -32,21 +32,25 @@ export interface DeviceForegroundInfo {
32
readonly isReactNative?: boolean;
33
}
34
35
-interface Target {
35
+export interface DeviceHubTarget {
36
readonly access: DeviceHubAccess;
37
readonly platform: DevicePlatform;
38
readonly deviceId: string;
39
}
40
41
-const vendorBase = (target: Target) =>
41
+const vendorBase = (target: DeviceHubTarget) =>
42
`${target.access.httpBase}${target.platform === "ios" ? "/vendor/serve-sim" : "/vendor/serve-emu"}`;
43
44
-const hubUrl = (target: Target, path: string, params?: Record<string, string>) => {
44
+const hubUrl = (target: DeviceHubTarget, path: string, params?: Record<string, string>) => {
45
const search = params ? `?${new URLSearchParams(params).toString()}` : "";
46
return withDeviceHubQuery(`${vendorBase(target)}${path}${search}`, target.access);
47
};
48
49
-const fetchJson = async (target: Target, url: string, signal?: AbortSignal): Promise<unknown> => {
49
+const fetchJson = async (
50
+ target: DeviceHubTarget,
51
+ url: string,
52
+ signal?: AbortSignal,
53
+): Promise<unknown> => {
54
const response = await fetch(url, {
55
cache: "no-store",
56
credentials: target.access.credentials ? "include" : "same-origin",
@@ -96,7 +100,7 @@ const flattenIosAxTree = (roots: ReadonlyArray<unknown>): ReadonlyArray<DeviceAx
100
};
101
102
export async function fetchDeviceAxTree(
99
- target: Target,
103
+ target: DeviceHubTarget,
104
signal?: AbortSignal,
105
): Promise<DeviceAxTree> {
106
if (target.platform === "ios") {
@@ -154,7 +158,7 @@ export async function fetchDeviceAxTree(
158
}
159
160
const openEventSource = (
157
- target: Target,
161
+ target: DeviceHubTarget,
162
url: string,
163
onMessage: (data: unknown) => void,
164
): (() => void) => {
@@ -168,7 +172,7 @@ const openEventSource = (
172
};
173
174
export function subscribeDeviceForeground(
171
- target: Target,
175
+ target: DeviceHubTarget,
176
onChange: (app: DeviceForegroundInfo | null) => void,
177
): () => void {
178
if (target.platform !== "ios") return () => {};
@@ -202,8 +206,20 @@ const toEventLogEntry = (raw: unknown): DeviceEventLogEntry | null => {
206
};
207
};
208
209
+export const DEVICE_EVENT_LOG_LIMIT = 100;
210
+
211
+/** A reset batch replaces the log; the log keeps the newest entries up to the limit. */
212
+export function appendDeviceEventLog(
213
+ current: ReadonlyArray<DeviceEventLogEntry>,
214
+ incoming: ReadonlyArray<DeviceEventLogEntry>,
215
+ reset: boolean,
216
+): ReadonlyArray<DeviceEventLogEntry> {
217
+ const merged = reset ? [...incoming] : [...current, ...incoming];
218
+ return merged.length > DEVICE_EVENT_LOG_LIMIT ? merged.slice(-DEVICE_EVENT_LOG_LIMIT) : merged;
219
+}
220
+
221
export function subscribeDeviceEventLog(
206
- target: Target,
222
+ target: DeviceHubTarget,
223
onEvents: (entries: ReadonlyArray<DeviceEventLogEntry>, reset: boolean) => void,
224
): () => void {
225
if (target.platform !== "ios") return () => {};
apps/web/src/components/device/deviceHubAtoms.ts
new
+117
@@ -0,0 +1,117 @@
1
+import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess";
2
+import * as Effect from "effect/Effect";
3
+import * as Schedule from "effect/Schedule";
4
+import * as Stream from "effect/Stream";
5
+import { AsyncResult, Atom } from "effect/reactivity";
6
+
7
+import {
8
+ appendDeviceEventLog,
9
+ type DeviceAxElement,
10
+ type DeviceEventLogEntry,
11
+ type DeviceForegroundInfo,
12
+ type DeviceHubTarget,
13
+ fetchDeviceAxTree,
14
+ subscribeDeviceEventLog,
15
+ subscribeDeviceForeground,
16
+} from "./deviceHubApi";
17
+import { AndroidFoldError, readAndroidFold, type AndroidFoldState } from "./deviceFold";
18
+
19
+const AX_POLL_INTERVAL_MS = 2_000;
20
+const FOLD_RETRY_MS = 3_000;
21
+
22
+/** `undefined` until the hub reports an app; the device detail's app fills in until then. */
23
+export const deviceForegroundAtom = Atom.family((target: DeviceHubTarget) =>
24
+ Atom.make<DeviceForegroundInfo | null | undefined>((get) => {
25
+ get.addFinalizer(subscribeDeviceForeground(target, (app) => get.setSelf(app)));
26
+ return undefined;
27
+ }).pipe(Atom.withLabel(`device-foreground:${target.deviceId}`)),
28
+);
29
+
30
+export const deviceEventLogAtom = Atom.family((target: DeviceHubTarget) =>
31
+ Atom.make<ReadonlyArray<DeviceEventLogEntry>>((get) => {
32
+ let entries: ReadonlyArray<DeviceEventLogEntry> = [];
33
+ get.addFinalizer(
34
+ subscribeDeviceEventLog(target, (incoming, reset) => {
35
+ entries = appendDeviceEventLog(entries, incoming, reset);
36
+ get.setSelf(entries);
37
+ }),
38
+ );
39
+ return entries;
40
+ }).pipe(Atom.withLabel(`device-event-log:${target.deviceId}`)),
41
+);
42
+
43
+// A failed poll keeps the last frames on screen; the next poll replaces them.
44
+export const deviceAxElementsAtom = Atom.family((target: DeviceHubTarget) =>
45
+ Atom.make(
46
+ Stream.fromEffectSchedule(
47
+ Effect.tryPromise((signal) => fetchDeviceAxTree(target, signal)).pipe(
48
+ Effect.map((tree): ReadonlyArray<DeviceAxElement> | null => tree.elements),
49
+ Effect.orElseSucceed(() => null),
50
+ ),
51
+ Schedule.spaced(AX_POLL_INTERVAL_MS),
52
+ ).pipe(Stream.filter((elements) => elements !== null)),
53
+ ).pipe(Atom.withLabel(`device-ax-elements:${target.deviceId}`)),
54
+);
55
+
56
+export interface AndroidFoldView {
57
+ readonly fold: AndroidFoldState | null;
58
+ /** Hinge angle the 3D phone shows, including an optimistic one while a change is in flight. */
59
+ readonly angle: number | null;
60
+}
61
+
62
+export const foldAngle = (fold: AndroidFoldState) =>
63
+ fold.hingeAngle ?? (fold.posture === "closed" ? 0 : 180);
64
+
65
+/**
66
+ * The fold controls and the 3D phone share one view of a device's fold. Keyed by
67
+ * host and device, not by hub access, so a renewed ticket keeps the known posture.
68
+ */
69
+export const androidFoldViewAtom = Atom.family((deviceKey: string) =>
70
+ Atom.make<AndroidFoldView>({ fold: null, angle: null }).pipe(
71
+ Atom.withLabel(`android-fold-view:${deviceKey}`),
72
+ ),
73
+);
74
+
75
+/**
76
+ * Reads the fold while mounted and records it in the device's fold view. The
77
+ * screen size is part of the key because folding resizes the screen, so a new
78
+ * size means the posture may have changed. An expired ticket stops retrying:
79
+ * renewed access is a new key and reads again.
80
+ */
81
+export const androidFoldReadAtom = Atom.family(
82
+ (input: {
83
+ readonly deviceKey: string;
84
+ readonly access: DeviceHubAccess;
85
+ readonly deviceId: string;
86
+ readonly screenWidth: number;
87
+ readonly screenHeight: number;
88
+ }) =>
89
+ Atom.make((get) =>
90
+ Effect.tryPromise({
91
+ try: (signal) => readAndroidFold(input.access, input.deviceId, signal),
92
+ catch: (cause) => ({ expired: cause instanceof AndroidFoldError && cause.status === 401 }),
93
+ }).pipe(
94
+ Effect.retry({
95
+ while: (failure) => !failure.expired,
96
+ schedule: Schedule.spaced(FOLD_RETRY_MS),
97
+ }),
98
+ Effect.tap((fold) =>
99
+ Effect.sync(() =>
100
+ get.set(androidFoldViewAtom(input.deviceKey), {
101
+ fold,
102
+ angle: fold.supported ? foldAngle(fold) : null,
103
+ }),
104
+ ),
105
+ ),
106
+ Effect.asVoid,
107
+ ),
108
+ ).pipe(Atom.withLabel(`android-fold-read:${input.deviceKey}`)),
109
+);
110
+
111
+export const NOOP_ATOM = Atom.make(AsyncResult.initial<void, never>()).pipe(
112
+ Atom.withLabel("device:noop"),
113
+);
114
+
115
+export const NO_FOREGROUND_ATOM = Atom.make<DeviceForegroundInfo | null | undefined>(
116
+ undefined,
117
+).pipe(Atom.withLabel("device-foreground:none"));
apps/web/src/components/device/deviceStreamSession.ts
new
+139
@@ -0,0 +1,139 @@
1
+import { createCanvasFrameSink } from "@t3tools/client-runtime/device/frame";
2
+import type { DuoControlState } from "@t3tools/client-runtime/device/duo-control";
3
+import {
4
+ createDeviceStreamClient,
5
+ type DeviceScreenSize,
6
+ type DeviceStreamClient,
7
+ type DeviceStreamStatus,
8
+} from "@t3tools/client-runtime/device/stream";
9
+import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess";
10
+import type { DevicePlatform, EnvironmentId } from "@t3tools/contracts";
11
+import { Atom } from "effect/reactivity";
12
+
13
+import { refreshDeviceHubAccess } from "~/state/device";
14
+
15
+export interface DeviceStreamState {
16
+ readonly status: DeviceStreamStatus;
17
+ readonly detail: string | undefined;
18
+ /** A frame arrived during this session, so a reconnect can keep showing it. */
19
+ readonly hasFrame: boolean;
20
+ readonly screen: DeviceScreenSize | null;
21
+ readonly mjpegUrl: string | null;
22
+ readonly mjpegGeneration: number;
23
+ readonly input: { readonly connected: boolean; readonly detail?: string };
24
+ readonly duoControl: DuoControlState;
25
+ readonly phoneUnavailable: boolean;
26
+}
27
+
28
+export interface DeviceStreamSession {
29
+ readonly stream: DeviceStreamClient;
30
+ /** The stream draws here; the view mounts it and the 3D viewers sample it. */
31
+ readonly canvas: HTMLCanvasElement;
32
+ readonly setFrameListener: (listener: (() => void) | null) => void;
33
+ /** Called when the hub rejects a Duo control, so the 3D viewer drops its optimistic pose. */
34
+ readonly setControlRejectedListener: (listener: (() => void) | null) => void;
35
+ readonly markPhoneUnavailable: () => void;
36
+ readonly reconnect: () => void;
37
+ readonly state: DeviceStreamState;
38
+}
39
+
40
+export interface DeviceStreamKey {
41
+ readonly environmentId: EnvironmentId;
42
+ readonly hostId: string;
43
+ readonly platform: DevicePlatform;
44
+ readonly deviceId: string;
45
+ readonly access: DeviceHubAccess;
46
+ /** Each mounted view owns its own client: one canvas cannot be in two places. */
47
+ readonly viewId: string;
48
+}
49
+
50
+export const IDLE_DEVICE_STREAM_STATE: DeviceStreamState = {
51
+ status: "connecting",
52
+ detail: undefined,
53
+ hasFrame: false,
54
+ screen: null,
55
+ mjpegUrl: null,
56
+ mjpegGeneration: 0,
57
+ input: { connected: false },
58
+ duoControl: { pending: false, requested: null, error: null },
59
+ phoneUnavailable: false,
60
+};
61
+
62
+export const deviceScreenKey = (environmentId: EnvironmentId, hostId: string, deviceId: string) =>
63
+ `${environmentId}\u0000${hostId}\u0000${deviceId}`;
64
+
65
+/**
66
+ * The screen one view's stream last reported, null while that view streams nothing.
67
+ * Keyed by view, not device: two views of one device stream independently, and one
68
+ * ending must not clear the other's size. The mini player lays out its frame from it.
69
+ */
70
+export const deviceStreamScreenAtom = Atom.family((viewId: string) =>
71
+ Atom.make<DeviceScreenSize | null>(null).pipe(Atom.withLabel(`device-stream-screen:${viewId}`)),
72
+);
73
+
74
+/** A device's video and input connection, live while a view reads it. */
75
+export const deviceStreamSessionAtom = Atom.family((key: DeviceStreamKey) =>
76
+ Atom.make((get): DeviceStreamSession => {
77
+ const screenAtom = deviceStreamScreenAtom(key.viewId);
78
+ const canvas = document.createElement("canvas");
79
+ canvas.style.display = "block";
80
+ canvas.style.width = "100%";
81
+ canvas.style.height = "100%";
82
+ let frameListener: (() => void) | null = null;
83
+ let controlRejectedListener: (() => void) | null = null;
84
+ let state = IDLE_DEVICE_STREAM_STATE;
85
+ let session: DeviceStreamSession;
86
+ const update = (patch: Partial<DeviceStreamState>) => {
87
+ state = { ...state, ...patch };
88
+ session = { ...session, state };
89
+ get.setSelf(session);
90
+ };
91
+ const client = createDeviceStreamClient(
92
+ { platform: key.platform, deviceId: key.deviceId, access: key.access },
93
+ createCanvasFrameSink(canvas, () => frameListener?.()),
94
+ {
95
+ onDuoControl: (duoControl) => {
96
+ if (duoControl.error && duoControl.error !== state.duoControl.error)
97
+ controlRejectedListener?.();
98
+ update({ duoControl });
99
+ },
100
+ onDuoUnavailable: () => update({ phoneUnavailable: true }),
101
+ onStatus: (status, detail) =>
102
+ update({ status, detail, ...(status === "streaming" ? { hasFrame: true } : {}) }),
103
+ onScreen: (screen) => {
104
+ get.set(screenAtom, screen);
105
+ update({ screen });
106
+ },
107
+ onUnauthorized: () => refreshDeviceHubAccess(key.environmentId),
108
+ onMjpegFallback: (mjpegUrl) =>
109
+ update({ mjpegUrl, mjpegGeneration: state.mjpegGeneration + 1 }),
110
+ onCanvasVideo: () => update({ mjpegUrl: null }),
111
+ onInputConnected: (connected, detail) =>
112
+ update({ input: { connected, ...(detail ? { detail } : {}) } }),
113
+ },
114
+ );
115
+ session = {
116
+ stream: client,
117
+ canvas,
118
+ setFrameListener: (listener) => {
119
+ frameListener = listener;
120
+ },
121
+ setControlRejectedListener: (listener) => {
122
+ controlRejectedListener = listener;
123
+ },
124
+ markPhoneUnavailable: () => update({ phoneUnavailable: true }),
125
+ reconnect: () => {
126
+ client.stop();
127
+ update({ phoneUnavailable: false });
128
+ client.start();
129
+ },
130
+ state,
131
+ };
132
+ get.addFinalizer(() => {
133
+ client.stop();
134
+ get.registry.set(screenAtom, null);
135
+ });
136
+ client.start();
137
+ return session;
138
+ }).pipe(Atom.withLabel(`device-stream:${key.platform}:${key.deviceId}`)),
139
+);
apps/web/src/components/device/mountDeviceViewer.ts
new
+110
@@ -0,0 +1,110 @@
1
+import { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
2
+import { observeResize } from "../../lib/observeResize";
3
+import { bindPhoneTrackpad } from "./phoneTrackpad";
4
+
5
+type PhoneInteraction = ReturnType<typeof createPhoneInteraction>;
6
+type TrackpadPinch = NonNullable<Parameters<typeof bindPhoneTrackpad>[2]>;
7
+
8
+interface MountableViewer {
9
+ readonly resize: (width: number, height: number, pixelRatio: number) => void;
10
+ readonly screenPoint: (
11
+ x: number,
12
+ y: number,
13
+ captured?: boolean,
14
+ ) => { x: number; y: number } | null;
15
+ readonly orbit: (deltaX: number, deltaY: number) => void;
16
+ readonly setInteractionActive: (active: boolean, mode: "touch" | "orbit") => void;
17
+ readonly resetPose: () => void;
18
+ readonly dispose: () => void;
19
+}
20
+
21
+export interface DeviceViewerReady {
22
+ /** Draws the latest decoded frame; registered as the stream's frame listener. */
23
+ readonly frame: () => void;
24
+ readonly pinch?: TrackpadPinch;
25
+ readonly dispose?: () => void;
26
+}
27
+
28
+/**
29
+ * Mounts a lazily loaded 3D device viewer on a canvas and returns its teardown, for use from
30
+ * the canvas ref callback. Owns host resizing, input cancellation, trackpad gestures, and
31
+ * disposal shared by every viewport.
32
+ */
33
+export function mountDeviceViewer<V extends MountableViewer>(
34
+ canvas: HTMLCanvasElement,
35
+ host: HTMLElement,
36
+ options: {
37
+ /** Resolves to the constructor, so a canvas unmounted while loading never builds a viewer. */
38
+ readonly load: () => Promise<() => V>;
39
+ readonly touch: (phase: "begin" | "move" | "end", point: { x: number; y: number }) => void;
40
+ readonly viewerRef: { current: V | null };
41
+ readonly interactionRef: { current: PhoneInteraction | null };
42
+ readonly onInputCancel: (cancel: (() => void) | null) => void;
43
+ readonly onResetReady: (reset: (() => void) | null) => void;
44
+ readonly onFrameListener: (listener: (() => void) | null) => void;
45
+ readonly onUnavailable: () => void;
46
+ readonly cancelInput?: (viewer: V) => void;
47
+ readonly ready: (context: {
48
+ readonly viewer: V;
49
+ readonly cancelTrackpad: () => void;
50
+ }) => DeviceViewerReady;
51
+ },
52
+) {
53
+ const { viewerRef, interactionRef } = options;
54
+ let disposed = false;
55
+ let trackpad: ReturnType<typeof bindPhoneTrackpad> | null = null;
56
+ let stopTrackpadEnd: (() => void) | undefined;
57
+ let mounted: DeviceViewerReady | null = null;
58
+ const resize = () => {
59
+ const { width, height } = host.getBoundingClientRect();
60
+ viewerRef.current?.resize(width, height, window.devicePixelRatio);
61
+ };
62
+ const stopObserving = observeResize(host, resize);
63
+ const blur = () => {
64
+ interactionRef.current?.end();
65
+ trackpad?.cancel();
66
+ if (viewerRef.current) options.cancelInput?.(viewerRef.current);
67
+ };
68
+ options.onInputCancel(blur);
69
+ window.addEventListener("blur", blur);
70
+ void options
71
+ .load()
72
+ .then((create) => {
73
+ if (disposed) return;
74
+ const viewer = create();
75
+ viewerRef.current = viewer;
76
+ options.onResetReady(viewer.resetPose);
77
+ const interaction = createPhoneInteraction({
78
+ screenPoint: (point, captured) => viewer.screenPoint(point.x, point.y, captured),
79
+ touch: options.touch,
80
+ orbit: viewer.orbit,
81
+ zoomBy: () => {},
82
+ onInteractionActive: viewer.setInteractionActive,
83
+ });
84
+ interactionRef.current = interaction;
85
+ resize();
86
+ mounted = options.ready({ viewer, cancelTrackpad: () => trackpad?.cancel() });
87
+ options.onFrameListener(mounted.frame);
88
+ mounted.frame();
89
+ trackpad = bindPhoneTrackpad(canvas, interaction, mounted.pinch);
90
+ stopTrackpadEnd = window.desktopBridge?.onTrackpadScrollEnd?.(() => trackpad?.endOrbit());
91
+ })
92
+ .catch(() => {
93
+ if (!disposed) options.onUnavailable();
94
+ });
95
+ return () => {
96
+ disposed = true;
97
+ stopTrackpadEnd?.();
98
+ trackpad?.dispose();
99
+ mounted?.dispose?.();
100
+ interactionRef.current?.end();
101
+ interactionRef.current = null;
102
+ options.onInputCancel(null);
103
+ options.onResetReady(null);
104
+ options.onFrameListener(null);
105
+ stopObserving();
106
+ window.removeEventListener("blur", blur);
107
+ viewerRef.current?.dispose();
108
+ viewerRef.current = null;
109
+ };
110
+}
apps/web/src/components/device/useDeviceControls.browser.test.tsx
+71
-30
@@ -1,6 +1,9 @@
1
import { EnvironmentId, type DeviceDetail, type DeviceSummary } from "@t3tools/contracts";
2
import type { DeviceHubAccess } from "@t3tools/client-runtime/device/hub-access";
3
+import type { AtomCommandConcurrency } from "@t3tools/client-runtime/state/runtime";
4
+import { RegistryContext } from "@effect/atom-react";
5
import * as Cause from "effect/Cause";
6
+import { AtomRegistry } from "effect/reactivity";
7
import { useEffect } from "react";
8
import { expect, it, vi } from "vite-plus/test";
9
@@ -13,19 +16,41 @@ const { read, action, subscribe } = vi.hoisted(() => ({
16
subscribe:
17
vi.fn<(_target: unknown, onChange: (app: { id: string } | null) => void) => () => void>(),
18
}));
16
-vi.mock(import("~/state/use-atom-command"), async (original) => {
17
- const { deviceEnvironment } = await import("~/state/device");
18
- return {
19
- ...(await original()),
20
- useAtomCommand: ((command: unknown) =>
21
- command === deviceEnvironment.detail ? read : action) as never,
19
+// Stubs only the transport; the device atoms' real scheduler and lanes decide ordering.
20
+vi.mock(import("@t3tools/client-runtime/state/runtime"), async (original) => {
21
+ const runtime = await original();
22
+ const createEnvironmentRpcCommand = (
23
+ _runtime: unknown,
24
+ options: {
25
+ readonly label: string;
26
+ readonly scheduler?: {
27
+ readonly schedule: (
28
+ registry: AtomRegistry.AtomRegistry,
29
+ concurrency: AtomCommandConcurrency<unknown>,
30
+ input: unknown,
31
+ execute: () => Promise<Result>,
32
+ ) => Promise<Result>;
33
+ };
34
+ readonly concurrency?: AtomCommandConcurrency<unknown>;
35
+ },
36
+ ) => {
37
+ const send = options.label === "environment-data:device:detail" ? read : action;
38
+ return {
39
+ label: options.label,
40
+ run: (registry: AtomRegistry.AtomRegistry, input: unknown) =>
41
+ options.scheduler && options.concurrency
42
+ ? options.scheduler.schedule(registry, options.concurrency, input, () => send())
43
+ : send(),
44
+ };
45
};
46
+ return { ...runtime, createEnvironmentRpcCommand: createEnvironmentRpcCommand as never };
47
});
48
vi.mock(import("./deviceHubApi"), async (original) => ({
49
...(await original()),
50
subscribeDeviceForeground: subscribe as never,
51
}));
52
53
+import { deviceDetailReadAtom } from "~/state/device";
54
import { useDeviceControls, type DeviceControls } from "./useDeviceControls";
55
56
const offline = Cause.fail(new Error("Device offline"));
@@ -56,13 +81,9 @@ function deferred() {
81
return { promise, resolve };
82
}
83
let controls: DeviceControls;
84
+const environmentId = EnvironmentId.make("environment");
85
function Probe({ visible, access = null }: { visible: boolean; access?: DeviceHubAccess | null }) {
60
- const next = useDeviceControls({
61
- environmentId: EnvironmentId.make("environment"),
62
- device,
63
- access,
64
- visible,
65
- });
86
+ const next = useDeviceControls({ environmentId, device, access, visible });
87
useEffect(() => {
88
controls = next;
89
}, [next]);
@@ -74,11 +95,25 @@ function setup() {
95
subscribe.mockReset();
96
subscribe.mockReturnValue(() => {});
97
read.mockResolvedValue(snapshot("light"));
98
+ // No idle TTL, as in the app: hiding the panel ends its read.
99
+ const registry = AtomRegistry.make();
100
+ const view = (visible: boolean) => (
101
+ <RegistryContext.Provider value={registry}>
102
+ <Probe visible={visible} />
103
+ </RegistryContext.Provider>
104
+ );
105
+ const readAtom = deviceDetailReadAtom({ environmentId, hostId: "remote", deviceId: "phone" });
106
+ const hide = async (screen: { rerender: (element: React.ReactElement) => Promise<unknown> }) => {
107
+ await screen.rerender(view(false));
108
+ await vi.waitFor(() => expect(registry.getNodes().has(readAtom)).toBe(false));
109
+ };
110
+ return { view, hide };
111
}
112
113
it("serializes rapid actions and retains confirmed settings while pending or after failure", async () => {
80
- setup();
81
- await using _screen = await render(<Probe visible />);
114
+ const { view } = setup();
115
+ await using _screen = await render(view(true));
116
+ await vi.waitFor(() => expect(controls.detail?.settings.appearance).toBe("light"));
117
const response = deferred();
118
action.mockReturnValueOnce(response.promise);
119
let pending!: Promise<void>;
@@ -101,45 +136,51 @@ it("serializes rapid actions and retains confirmed settings while pending or aft
136
expect(controls.disabled).toBe(false);
137
});
138
104
-it("keeps actions disabled when settings cannot be confirmed after a detached command", async () => {
105
- setup();
106
- await using screen = await render(<Probe visible />);
139
+it("confirms settings again after a command sent before the panel reopened", async () => {
140
+ const { view, hide } = setup();
141
+ await using screen = await render(view(true));
142
+ await vi.waitFor(() => expect(controls.detail).not.toBeNull());
143
const response = deferred();
144
action.mockReturnValueOnce(response.promise);
145
let pending!: Promise<void>;
146
await act(async () => {
147
pending = controls.act({ type: "setAppearance", value: "dark" });
148
});
113
- await screen.rerender(<Probe visible={false} />);
114
- await screen.rerender(<Probe visible />);
149
+ await hide(screen);
150
read.mockResolvedValueOnce({ _tag: "Failure", cause: offline });
151
+ await screen.rerender(view(true));
152
await act(async () => {
153
response.resolve(snapshot("dark"));
154
await pending;
155
});
156
+ await vi.waitFor(() => expect(controls.error).toBe("Device offline"));
157
expect(controls.detail).toBeNull();
158
expect(controls.disabled).toBe(true);
122
- expect(controls.error).toBe("Device offline");
159
await act(async () => controls.act({ type: "setAppearance", value: "light" }));
160
expect(action).toHaveBeenCalledOnce();
125
- await screen.rerender(<Probe visible={false} />);
126
- await screen.rerender(<Probe visible />);
127
- expect(controls.disabled).toBe(false);
161
+ await hide(screen);
162
+ await screen.rerender(view(true));
163
+ await vi.waitFor(() => expect(controls.disabled).toBe(false));
164
expect(controls.error).toBeNull();
165
});
166
131
-it("does not let an older refresh overwrite a newer confirmed action", async () => {
132
- setup();
133
- await using screen = await render(<Probe visible />);
134
- await screen.rerender(<Probe visible={false} />);
167
+it("does not let a read sent before an action overwrite the action's answer", async () => {
168
+ const { view, hide } = setup();
169
+ await using screen = await render(view(true));
170
+ await vi.waitFor(() => expect(controls.detail).not.toBeNull());
171
+ await hide(screen);
172
const refresh = deferred();
173
read.mockReturnValueOnce(refresh.promise);
137
- await screen.rerender(<Probe visible />);
174
+ await screen.rerender(view(true));
175
+ await vi.waitFor(() => expect(read).toHaveBeenCalledTimes(2));
176
action.mockResolvedValueOnce(snapshot("dark"));
139
- await act(async () => controls.act({ type: "setAppearance", value: "dark" }));
177
+ let pending!: Promise<void>;
178
+ await act(async () => {
179
+ pending = controls.act({ type: "setAppearance", value: "dark" });
180
+ });
181
await act(async () => {
182
refresh.resolve(snapshot("light"));
142
- await refresh.promise;
183
+ await pending;
184
});
185
expect(controls.detail?.settings.appearance).toBe("dark");
186
});
apps/web/src/components/device/useDeviceControls.ts
+25
-76
@@ -1,15 +1,11 @@
1
+import { useAtomMount, useAtomSet, useAtomValue } from "@effect/atom-react";
2
import type { DeviceHubAccess } from "@t3tools/client-runtime/state/deviceHubAccess";
2
-import type {
3
- DeviceActionInput,
4
- DeviceDetail,
5
- DeviceSummary,
6
- EnvironmentId,
7
-} from "@t3tools/contracts";
8
-import { useEffect, useRef, useState } from "react";
9
-import { deviceEnvironment } from "~/state/device";
3
+import type { DeviceActionInput, DeviceSummary, EnvironmentId } from "@t3tools/contracts";
4
+import { useRef, useState } from "react";
5
+import { deviceDetailAtom, deviceDetailReadAtom, deviceEnvironment } from "~/state/device";
6
import { formatEnvironmentQueryError } from "~/state/query";
7
import { useAtomCommand } from "~/state/use-atom-command";
12
-import { subscribeDeviceForeground, type DeviceForegroundInfo } from "./deviceHubApi";
8
+import { deviceForegroundAtom, NO_FOREGROUND_ATOM, NOOP_ATOM } from "./deviceHubAtoms";
9
10
type ActionBody = DeviceActionInput extends infer A
11
? A extends { readonly type: string }
@@ -25,86 +21,39 @@ export function useDeviceControls(options: {
21
visible: boolean;
22
}) {
23
const { environmentId, device, access, visible } = options;
28
- const readDetail = useAtomCommand(deviceEnvironment.detail, { reportFailure: false });
24
const runAction = useAtomCommand(deviceEnvironment.action, { reportFailure: false });
30
- const [detail, setDetail] = useState<DeviceDetail | null>(null);
25
+ const { hostId, id: deviceId } = device;
26
+ const key = { environmentId, hostId, deviceId };
27
+ const { detail, error } = useAtomValue(deviceDetailAtom(key));
28
+ const setView = useAtomSet(deviceDetailAtom(key));
29
+ useAtomMount(visible ? deviceDetailReadAtom(key) : NOOP_ATOM);
30
+ const foreground = useAtomValue(
31
+ access && visible
32
+ ? deviceForegroundAtom({ access, platform: device.platform, deviceId })
33
+ : NO_FOREGROUND_ATOM,
34
+ );
35
const [pending, setPending] = useState(false);
32
- const [error, setError] = useState<string | null>(null);
33
- const [foreground, setForeground] = useState<DeviceForegroundInfo | null | undefined>();
34
- // Hiding invalidates results without cancelling host commands; serialize through settlement across reopen.
36
+ // Hiding does not cancel host commands, so actions stay serialized until settlement
37
+ // rather than letting a reopened panel race the previous command.
38
const busy = useRef(false);
36
- const generation = useRef(0);
37
- const visibleRead = useRef<Parameters<typeof readDetail>[0] | null>(null);
38
- const { hostId, id: deviceId } = device;
39
-
40
- useEffect(() => {
41
- const revision = ++generation.current;
42
- if (!visible) return;
43
- const request = { environmentId, input: { hostId, deviceId } };
44
- visibleRead.current = request;
45
- void readDetail(request).then((result) => {
46
- if (generation.current !== revision) return;
47
- if (result._tag === "Success") {
48
- setDetail(result.value);
49
- setError(null);
50
- } else {
51
- setDetail(null);
52
- setError(formatEnvironmentQueryError(result.cause));
53
- }
54
- });
55
- return () => {
56
- visibleRead.current = null;
57
- generation.current++;
58
- };
59
- }, [deviceId, environmentId, hostId, readDetail, visible]);
60
-
61
- useEffect(() => {
62
- if (!access || !visible) return;
63
- let active = true;
64
- const unsubscribe = subscribeDeviceForeground(
65
- { access, platform: device.platform, deviceId: device.id },
66
- (app) => {
67
- if (active) setForeground(app);
68
- },
69
- );
70
- return () => {
71
- active = false;
72
- unsubscribe();
73
- setForeground(undefined);
74
- };
75
- }, [access, device.id, device.platform, visible]);
39
40
const available = detail !== null && visible;
41
const act = async (body: ActionBody) => {
42
if (busy.current || !available) return;
43
busy.current = true;
81
- const revision = ++generation.current;
44
setPending(true);
83
- setError(null);
45
+ setView((current) => ({ ...current, error: null }));
46
return runAction({
47
environmentId,
48
input: { hostId, deviceId, ...body } as DeviceActionInput,
49
})
88
- .then((result) => {
89
- if (generation.current !== revision) {
90
- const request = visibleRead.current;
91
- if (!request) return;
92
- // Reopening can read before pending commands settle; refresh again before allowing actions.
93
- const refreshRevision = ++generation.current;
94
- return readDetail(request).then((refreshed) => {
95
- if (generation.current !== refreshRevision) return;
96
- if (refreshed._tag === "Success") {
97
- setDetail(refreshed.value);
98
- setError(null);
99
- } else {
100
- setDetail(null);
101
- setError(formatEnvironmentQueryError(refreshed.cause));
102
- }
103
- });
104
- }
105
- if (result._tag === "Success") setDetail(result.value);
106
- else setError(formatEnvironmentQueryError(result.cause));
107
- })
50
+ .then((result) =>
51
+ setView((current) =>
52
+ result._tag === "Success"
53
+ ? { detail: result.value, error: null }
54
+ : { ...current, error: formatEnvironmentQueryError(result.cause) },
55
+ ),
56
+ )
57
.finally(() => {
58
busy.current = false;
59
setPending(false);
apps/web/src/components/preview/ThreadPreviewMiniPlayer.tsx
+6
-3
@@ -1,11 +1,13 @@
1
import { FILL_PREVIEW_VIEWPORT, type ScopedThreadRef } from "@t3tools/contracts";
2
import { previewPdfAssetUrl } from "@t3tools/shared/preview";
3
+import { useAtomValue } from "@effect/atom-react";
4
import { useEnvironmentHttpBaseUrl } from "~/state/environments";
5
import { PanelRightIcon, PictureInPicture2, XIcon } from "lucide-react";
6
import {
7
type PointerEvent as ReactPointerEvent,
8
type ReactNode,
9
useEffect,
10
+ useId,
11
useLayoutEffect,
12
useRef,
13
useState,
@@ -46,7 +48,7 @@ import { useRightPanelStore } from "~/rightPanelStore";
48
import { useDeviceState } from "~/state/device";
49
50
import { DeviceStreamView } from "../device/DeviceStreamView";
49
-import type { DeviceScreenSize } from "@t3tools/client-runtime/device/stream";
51
+import { deviceStreamScreenAtom } from "../device/deviceStreamSession";
52
import type { PreviewStreamViewport } from "@t3tools/client-runtime/preview/server-browser-stream";
53
import { previewBridge } from "./previewBridge";
54
import { showPreviewPopup } from "./showPreviewPopup";
@@ -266,7 +268,8 @@ function DeviceMiniPlayer({
268
miniPlayer,
269
}: Props & { readonly source: Extract<PreviewMiniPlayerSource, { kind: "device" }> }) {
270
const { state: deviceState } = useDeviceState(threadRef.environmentId);
269
- const [screen, setScreen] = useState<DeviceScreenSize | null>(null);
271
+ const viewId = useId();
272
+ const screen = useAtomValue(deviceStreamScreenAtom(viewId));
273
const sourceSize = resolveDeviceMiniPlayerSourceSize(source.platform, screen);
274
const device = deviceState.devices.find(
275
(entry) => entry.hostId === source.hostId && entry.id === source.deviceId,
@@ -308,7 +311,7 @@ function DeviceMiniPlayer({
311
deviceName={device?.name ?? source.name}
312
deviceDescription={`${hostLabel} · ${device?.version ?? source.platform}`}
313
visible
311
- onScreen={setScreen}
314
+ viewId={viewId}
315
/>
316
</div>
317
)}
apps/web/src/components/settings/EasDeviceHostsSettings.tsx
+29
-16
@@ -1,4 +1,5 @@
1
-import { useEffect, useEffectEvent, useId, useRef, useState } from "react";
1
+import { useAtomValue } from "@effect/atom-react";
2
+import { useId, useRef, useState } from "react";
3
import * as Cause from "effect/Cause";
4
import * as Option from "effect/Option";
5
import {
@@ -10,7 +11,7 @@ import {
11
import { CloudIcon, PlusIcon, XIcon } from "lucide-react";
12
13
import { randomUUID } from "../../lib/utils";
13
-import { deviceEnvironment } from "../../state/device";
14
+import { deviceEasAccountCheck, deviceEnvironment } from "../../state/device";
15
import { serverEnvironment } from "../../state/server";
16
import { readEnvironmentScope, useEnvironmentScope } from "../../state/session";
17
import { useAtomCommand } from "../../state/use-atom-command";
@@ -154,6 +155,12 @@ export function EasDeviceHostsSettings(props: { environmentId: EnvironmentId | n
155
);
156
}
157
158
+type AccountCheck =
159
+ | { status: "idle" }
160
+ | { status: "checking" }
161
+ | { status: "signed-in"; account: DeviceEasAccount }
162
+ | { status: "failed"; error: string };
163
+
164
function EasDeviceHostEditor(props: {
165
environmentId: EnvironmentId;
166
host: EasDeviceHostConfig;
@@ -169,13 +176,26 @@ function EasDeviceHostEditor(props: {
176
const [token, setToken] = useState<
177
{ kind: "saved" } | { kind: "new"; value: string } | { kind: "cleared" }
178
>(props.host.accessToken ? { kind: "saved" } : { kind: "cleared" });
172
- const [account, setAccount] = useState<
173
- | { status: "idle" }
174
- | { status: "checking" }
175
- | { status: "signed-in"; account: DeviceEasAccount }
176
- | { status: "failed"; error: string }
177
- // The editor checks the account as it opens.
178
- >({ status: "checking" });
179
+ // Checking on open names the saved project and shows a rejected credential
180
+ // before anything is changed. A manual check or a token edit replaces it.
181
+ const openCheck = useAtomValue(
182
+ deviceEasAccountCheck({
183
+ environmentId: props.environmentId,
184
+ input: props.host.accessToken
185
+ ? props.isNew
186
+ ? {}
187
+ : { hostId: props.host.id }
188
+ : { accessToken: "" },
189
+ }),
190
+ );
191
+ const [checked, setAccount] = useState<AccountCheck | null>(null);
192
+ const account: AccountCheck =
193
+ checked ??
194
+ (openCheck._tag === "Initial" || openCheck.waiting
195
+ ? { status: "checking" }
196
+ : openCheck._tag === "Success"
197
+ ? { status: "signed-in", account: openCheck.value }
198
+ : { status: "failed", error: failureMessage(openCheck.cause) });
199
const next: EasDeviceHostConfig = {
200
...draft,
201
label: draft.label.trim(),
@@ -225,13 +245,6 @@ function EasDeviceHostEditor(props: {
245
void requestCheck();
246
};
247
228
- // Checking on open names the saved project and shows a rejected credential
229
- // before anything is changed.
230
- const checkOnOpen = useEffectEvent(() => void requestCheck());
231
- useEffect(() => {
232
- checkOnOpen();
233
- }, []);
234
-
248
const limit = (
249
field: "maxSessionMinutes" | "idleMinutes" | "maxConcurrentSessions",
250
label: string,
apps/web/src/state/device.ts
+88
-2
@@ -4,7 +4,16 @@ import {
4
type DeviceHubAccess,
5
resolveDeviceHubAccess,
6
} from "@t3tools/client-runtime/state/deviceHubAccess";
7
-import type { DeviceServiceState, EnvironmentId } from "@t3tools/contracts";
7
+import {
8
+ createEnvironmentRpcQueryAtomFamily,
9
+ runAtomCommand,
10
+} from "@t3tools/client-runtime/state/runtime";
11
+import {
12
+ type DeviceDetail,
13
+ type DeviceServiceState,
14
+ type EnvironmentId,
15
+ WS_METHODS,
16
+} from "@t3tools/contracts";
17
import * as Effect from "effect/Effect";
18
import * as Option from "effect/Option";
19
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
@@ -12,7 +21,7 @@ import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
21
import { connectionAtomRuntime } from "../connection/runtime";
22
import { appAtomRegistry } from "../rpc/atomRegistry";
23
import { environmentSession } from "./session";
15
-import { useEnvironmentQuery } from "./query";
24
+import { formatEnvironmentQueryError, useEnvironmentQuery } from "./query";
25
26
export const deviceEnvironment = createDeviceEnvironmentAtoms(connectionAtomRuntime);
27
@@ -85,3 +94,80 @@ export function renewDeviceHubAccess(
94
),
95
);
96
}
97
+
98
+export interface DeviceDetailKey {
99
+ readonly environmentId: EnvironmentId;
100
+ readonly hostId: string;
101
+ readonly deviceId: string;
102
+}
103
+
104
+export interface DeviceDetailView {
105
+ /** Settings the host last confirmed, by a read or by an action's answer. */
106
+ readonly detail: DeviceDetail | null;
107
+ readonly error: string | null;
108
+}
109
+
110
+export const deviceDetailAtom = Atom.family((key: DeviceDetailKey) =>
111
+ Atom.make<DeviceDetailView>({ detail: null, error: null }).pipe(
112
+ Atom.withLabel(`device-detail:${key.hostId}:${key.deviceId}`),
113
+ ),
114
+);
115
+
116
+/**
117
+ * Reads a device's settings into its detail view while mounted. Reads and
118
+ * actions share one serial lane per environment, so a read never answers
119
+ * before an action that was sent ahead of it.
120
+ */
121
+export const deviceDetailReadAtom = Atom.family((key: DeviceDetailKey) =>
122
+ Atom.make((get) =>
123
+ Effect.promise(() =>
124
+ runAtomCommand(
125
+ get.registry,
126
+ deviceEnvironment.detail,
127
+ { environmentId: key.environmentId, input: { hostId: key.hostId, deviceId: key.deviceId } },
128
+ { reportFailure: false },
129
+ ),
130
+ ).pipe(
131
+ Effect.map((result) =>
132
+ get.set(
133
+ deviceDetailAtom(key),
134
+ result._tag === "Success"
135
+ ? { detail: result.value, error: null }
136
+ : { detail: null, error: formatEnvironmentQueryError(result.cause) },
137
+ ),
138
+ ),
139
+ ),
140
+ ).pipe(Atom.withLabel(`device-detail-read:${key.hostId}:${key.deviceId}`)),
141
+);
142
+
143
+/**
144
+ * Probes the environment's device hosts while mounted. Remote hosts report
145
+ * their platforms only after a probe. Probing never installs or starts helpers.
146
+ */
147
+export const deviceHostProbeAtom = Atom.family((environmentId: EnvironmentId) =>
148
+ Atom.make((get) =>
149
+ Effect.promise(() =>
150
+ runAtomCommand(
151
+ get.registry,
152
+ deviceEnvironment.list,
153
+ { environmentId, input: {} },
154
+ { reportFailure: false },
155
+ ),
156
+ ).pipe(Effect.asVoid),
157
+ ).pipe(Atom.withLabel(`device-host-probe:${environmentId}`)),
158
+);
159
+
160
+export const NO_DEVICE_PROBE_ATOM = Atom.make(AsyncResult.initial<void, never>()).pipe(
161
+ Atom.withLabel("device-host-probe:none"),
162
+);
163
+
164
+/**
165
+ * The Expo account an EAS host editor shows as it opens, for the saved
166
+ * credential. Each open checks again; edits and manual checks use the command.
167
+ */
168
+export const deviceEasAccountCheck = createEnvironmentRpcQueryAtomFamily(connectionAtomRuntime, {
169
+ label: "environment-data:device:eas-account-check",
170
+ tag: WS_METHODS.deviceEasAccount,
171
+ staleTimeMs: 0,
172
+ idleTtlMs: 0,
173
+});