Open History to see the last commit
1import type { DeviceSummary, EnvironmentId } from "@t3tools/contracts";
2import { useRef, useState } from "react";
3import {
4 captureDeviceScreenshot,
5 DeviceScreenshotError,
6} from "@t3tools/client-runtime/device/screenshot";
7import { refreshDeviceHubAccess, useDeviceHubAccess } from "~/state/device";
8import { DeviceControlsRail } from "./DeviceControlsRail";
9import { DeviceStreamView } from "./DeviceStreamView";
10import { DeviceToolsPanel } from "./DeviceToolsPanel";
11import { useDeviceControls } from "./useDeviceControls";
12
13const DOWNLOAD_URL_LIFETIME_MS = 60_000;
14
15export function DeviceWorkspace(props: {
16 environmentId: EnvironmentId;
17 device: DeviceSummary;
18 hostLabel: string;
19 /** Leased from a Simlock pool: serve-sim input, no host commands. */
20 simlock?: boolean;
21 hostDiagnostics: string | undefined;
22 visible: boolean;
23 onFloat: () => void;
24 onClose: () => void;
25 onPowerOff: () => void;
26}) {
27 const [toolsOpen, setToolsOpen] = useState(false);
28 const [axOverlay, setAxOverlay] = useState(false);
29 const [screenshotPending, setScreenshotPending] = useState(false);
30 const [screenshotError, setScreenshotError] = useState<string | null>(null);
31 // A capture always finishes, even if the panel hides or closes meanwhile; the user asked for it.
32 const capturing = useRef(false);
33 const access = useDeviceHubAccess(props.environmentId, props.device.hostId);
34 const controls = useDeviceControls({ ...props, access });
35 const saveScreenshot = () => {
36 if (!access || capturing.current || !props.visible) return;
37 capturing.current = true;
38 setScreenshotPending(true);
39 setScreenshotError(null);
40 return captureDeviceScreenshot(
41 { access, platform: props.device.platform, deviceId: props.device.id },
42 // Never aborted: see `capturing` above.
43 new AbortController().signal,
44 )
45 .then((image) => {
46 const url = URL.createObjectURL(image);
47 const link = document.createElement("a");
48 link.href = url;
49 link.download = `${props.device.name.replace(/[^a-z0-9-]/gi, "-")}-${Date.now()}.png`;
50 document.body.append(link);
51 link.click();
52 link.remove();
53 // The browser reads the blob after the click returns.
54 window.setTimeout(() => URL.revokeObjectURL(url), DOWNLOAD_URL_LIFETIME_MS);
55 })
56 .catch((cause: unknown) => {
57 if (cause instanceof DeviceScreenshotError && cause.status === 401)
58 refreshDeviceHubAccess(props.environmentId);
59 setScreenshotError(
60 cause instanceof Error ? cause.message : "Screenshot capture failed. Try again.",
61 );
62 })
63 .finally(() => {
64 capturing.current = false;
65 setScreenshotPending(false);
66 });
67 };
68 return (
69 <>
70 <div className="relative flex min-h-0 min-w-0 flex-1 flex-col">
71 {screenshotError || (controls.error && !toolsOpen) ? (
72 <p role="alert" className="px-3 py-2 text-xs text-destructive">
73 {screenshotError ?? controls.error}
74 </p>
75 ) : null}
76 <div className="min-h-0 flex-1">
77 <DeviceStreamView
78 environmentId={props.environmentId}
79 platform={props.device.platform}
80 deviceName={props.device.name}
81 deviceDescription={`${props.hostLabel} · ${props.device.version}`}
82 deviceId={props.device.id}
83 hostId={props.device.hostId}
84 visible={props.visible}
85 serveSimOnly={props.simlock === true}
86 axOverlay={axOverlay}
87 allowPhoneView
88 renderControls={(view) => (
89 <DeviceControlsRail
90 platform={props.device.platform}
91 handle={view.handle}
92 view={view}
93 controls={controls}
94 screenshotPending={screenshotPending}
95 onScreenshot={() => void saveScreenshot()}
96 toolsOpen={toolsOpen}
97 onTools={() => setToolsOpen(!toolsOpen)}
98 onFloat={props.onFloat}
99 onClose={props.onClose}
100 onPowerOff={props.onPowerOff}
101 inputOnly={props.simlock === true}
102 />
103 )}
104 />
105 </div>
106 </div>
107 {toolsOpen ? (
108 <DeviceToolsPanel
109 device={props.device}
110 controls={controls}
111 hostDiagnostics={props.hostDiagnostics}
112 access={access}
113 axOverlay={axOverlay}
114 onAxOverlayChange={setAxOverlay}
115 onClose={() => setToolsOpen(false)}
116 className="absolute inset-y-0 right-0 z-10 w-full max-w-72 border-l shadow-lg @[700px]:static @[700px]:w-72 @[700px]:shrink-0 @[700px]:shadow-none"
117 />
118 ) : null}
119 </>
120 );
121}