Open History to see the last commit
1import type { DeviceSummary, EnvironmentId } from "@t3tools/contracts";
2import { useEffect, 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, type DeviceStreamHandle } from "./DeviceStreamView";
10import { DeviceToolsPanel } from "./DeviceToolsPanel";
11import { useDeviceControls } from "./useDeviceControls";
12
13export function DeviceWorkspace(props: {
14 environmentId: EnvironmentId;
15 device: DeviceSummary;
16 hostLabel: string;
17 hostDiagnostics: string | undefined;
18 visible: boolean;
19 onFloat: () => void;
20 onClose: () => void;
21 onPowerOff: () => void;
22}) {
23 const [handle, setHandle] = useState<DeviceStreamHandle | null>(null);
24 const [toolsOpen, setToolsOpen] = useState(false);
25 const [axOverlay, setAxOverlay] = useState(false);
26 const [screenshotPending, setScreenshotPending] = useState(false);
27 const [screenshotError, setScreenshotError] = useState<string | null>(null);
28 const captureRef = useRef<AbortController | null>(null);
29 const downloadRef = useRef<string | null>(null);
30 const access = useDeviceHubAccess(props.environmentId, props.device.hostId);
31 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]);
40 const saveScreenshot = () => {
41 if (!access || captureRef.current || !props.visible) return;
42 const controller = new AbortController();
43 captureRef.current = controller;
44 setScreenshotPending(true);
45 setScreenshotError(null);
46 return captureDeviceScreenshot(
47 { access, platform: props.device.platform, deviceId: props.device.id },
48 controller.signal,
49 )
50 .then((image) => {
51 if (controller.signal.aborted) return;
52 if (downloadRef.current) URL.revokeObjectURL(downloadRef.current);
53 const url = URL.createObjectURL(image);
54 downloadRef.current = url;
55 const link = document.createElement("a");
56 link.href = url;
57 link.download = `${props.device.name.replace(/[^a-z0-9-]/gi, "-")}-${Date.now()}.png`;
58 document.body.append(link);
59 link.click();
60 link.remove();
61 })
62 .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 }
70 })
71 .finally(() => {
72 if (captureRef.current === controller) {
73 captureRef.current = null;
74 setScreenshotPending(false);
75 }
76 });
77 };
78 return (
79 <>
80 <div className="relative flex min-h-0 min-w-0 flex-1 flex-col">
81 {screenshotError || (controls.error && !toolsOpen) ? (
82 <p role="alert" className="px-3 py-2 text-xs text-destructive">
83 {screenshotError ?? controls.error}
84 </p>
85 ) : null}
86 <div className="min-h-0 flex-1">
87 <DeviceStreamView
88 environmentId={props.environmentId}
89 platform={props.device.platform}
90 deviceName={props.device.name}
91 deviceDescription={`${props.hostLabel} · ${props.device.version}`}
92 deviceId={props.device.id}
93 hostId={props.device.hostId}
94 visible={props.visible}
95 axOverlay={axOverlay}
96 allowPhoneView
97 onHandle={setHandle}
98 renderControls={(view) => (
99 <DeviceControlsRail
100 platform={props.device.platform}
101 handle={handle}
102 view={view}
103 controls={controls}
104 screenshotPending={screenshotPending}
105 onScreenshot={() => void saveScreenshot()}
106 toolsOpen={toolsOpen}
107 onTools={() => setToolsOpen(!toolsOpen)}
108 onFloat={props.onFloat}
109 onClose={props.onClose}
110 onPowerOff={props.onPowerOff}
111 />
112 )}
113 />
114 </div>
115 </div>
116 {toolsOpen ? (
117 <DeviceToolsPanel
118 device={props.device}
119 controls={controls}
120 hostDiagnostics={props.hostDiagnostics}
121 access={access}
122 axOverlay={axOverlay}
123 onAxOverlayChange={setAxOverlay}
124 onClose={() => setToolsOpen(false)}
125 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"
126 />
127 ) : null}
128 </>
129 );
130}