Open History to see the last commit
1import type { DeviceHostSummary, 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 hostKind: DeviceHostSummary["kind"] | undefined;
20 hostDiagnostics: string | undefined;
21 visible: boolean;
22 onFloat: () => void;
23 onClose: () => void;
24 onPowerOff: () => void;
25}) {
26 const [toolsOpen, setToolsOpen] = useState(false);
27 const [axOverlay, setAxOverlay] = useState(false);
28 const [screenshotPending, setScreenshotPending] = useState(false);
29 const [screenshotError, setScreenshotError] = useState<string | null>(null);
30 // A capture always finishes, even if the panel hides or closes meanwhile; the user asked for it.
31 const capturing = useRef(false);
32 const access = useDeviceHubAccess(props.environmentId, props.device.hostId);
33 // Settings and tools run commands on the device's own machine, which EAS and Simlock do not expose.
34 const runsHostCommands = props.hostKind === "local" || props.hostKind === "ssh";
35 const controls = useDeviceControls({
36 ...props,
37 access,
38 visible: props.visible && runsHostCommands,
39 });
40 const saveScreenshot = () => {
41 if (!access || capturing.current || !props.visible) return;
42 capturing.current = true;
43 setScreenshotPending(true);
44 setScreenshotError(null);
45 return captureDeviceScreenshot(
46 { access, platform: props.device.platform, deviceId: props.device.id },
47 // Never aborted: see `capturing` above.
48 new AbortController().signal,
49 )
50 .then((image) => {
51 const url = URL.createObjectURL(image);
52 const link = document.createElement("a");
53 link.href = url;
54 link.download = `${props.device.name.replace(/[^a-z0-9-]/gi, "-")}-${Date.now()}.png`;
55 document.body.append(link);
56 link.click();
57 link.remove();
58 // The browser reads the blob after the click returns.
59 window.setTimeout(() => URL.revokeObjectURL(url), DOWNLOAD_URL_LIFETIME_MS);
60 })
61 .catch((cause: unknown) => {
62 if (cause instanceof DeviceScreenshotError && cause.status === 401)
63 refreshDeviceHubAccess(props.environmentId);
64 setScreenshotError(
65 cause instanceof Error ? cause.message : "Screenshot capture failed. Try again.",
66 );
67 })
68 .finally(() => {
69 capturing.current = false;
70 setScreenshotPending(false);
71 });
72 };
73 return (
74 <>
75 <div className="relative flex min-h-0 min-w-0 flex-1 flex-col">
76 {screenshotError || (controls.error && !toolsOpen) ? (
77 <p role="alert" className="px-3 py-2 text-xs text-destructive">
78 {screenshotError ?? controls.error}
79 </p>
80 ) : null}
81 <div className="min-h-0 flex-1">
82 <DeviceStreamView
83 environmentId={props.environmentId}
84 platform={props.device.platform}
85 deviceName={props.device.name}
86 deviceDescription={`${props.hostLabel} · ${props.device.version}`}
87 deviceId={props.device.id}
88 hostId={props.device.hostId}
89 visible={props.visible}
90 serveSimOnly={props.hostKind === "simlock"}
91 axOverlay={axOverlay}
92 allowPhoneView
93 renderControls={(view) => (
94 <DeviceControlsRail
95 platform={props.device.platform}
96 hostKind={props.hostKind}
97 handle={view.handle}
98 view={view}
99 controls={controls}
100 screenshotPending={screenshotPending}
101 onScreenshot={() => void saveScreenshot()}
102 toolsOpen={toolsOpen}
103 onTools={() => setToolsOpen(!toolsOpen)}
104 onFloat={props.onFloat}
105 onClose={props.onClose}
106 onPowerOff={props.onPowerOff}
107 />
108 )}
109 />
110 </div>
111 </div>
112 {toolsOpen ? (
113 <DeviceToolsPanel
114 device={props.device}
115 controls={controls}
116 hostDiagnostics={props.hostDiagnostics}
117 access={access}
118 axOverlay={axOverlay}
119 onAxOverlayChange={setAxOverlay}
120 onClose={() => setToolsOpen(false)}
121 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"
122 />
123 ) : null}
124 </>
125 );
126}