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

DeviceWorkspace.tsx

117 lines4.4 KB

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 hostDiagnostics: string | undefined;
20 visible: boolean;
21 onFloat: () => void;
22 onClose: () => void;
23 onPowerOff: () => void;
24}) {
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);
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 });
33 const saveScreenshot = () => {
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 },
40 // Never aborted: see `capturing` above.
41 new AbortController().signal,
42 )
43 .then((image) => {
44 const url = URL.createObjectURL(image);
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) => {
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(() => {
62 capturing.current = false;
63 setScreenshotPending(false);
64 });
65 };
66 return (
67 <>
68 <div className="relative flex min-h-0 min-w-0 flex-1 flex-col">
69 {screenshotError || (controls.error && !toolsOpen) ? (
70 <p role="alert" className="px-3 py-2 text-xs text-destructive">
71 {screenshotError ?? controls.error}
72 </p>
73 ) : null}
74 <div className="min-h-0 flex-1">
75 <DeviceStreamView
76 environmentId={props.environmentId}
77 platform={props.device.platform}
78 deviceName={props.device.name}
79 deviceDescription={`${props.hostLabel} · ${props.device.version}`}
80 deviceId={props.device.id}
81 hostId={props.device.hostId}
82 visible={props.visible}
83 axOverlay={axOverlay}
84 allowPhoneView
85 renderControls={(view) => (
86 <DeviceControlsRail
87 platform={props.device.platform}
88 handle={view.handle}
89 view={view}
90 controls={controls}
91 screenshotPending={screenshotPending}
92 onScreenshot={() => void saveScreenshot()}
93 toolsOpen={toolsOpen}
94 onTools={() => setToolsOpen(!toolsOpen)}
95 onFloat={props.onFloat}
96 onClose={props.onClose}
97 onPowerOff={props.onPowerOff}
98 />
99 )}
100 />
101 </div>
102 </div>
103 {toolsOpen ? (
104 <DeviceToolsPanel
105 device={props.device}
106 controls={controls}
107 hostDiagnostics={props.hostDiagnostics}
108 access={access}
109 axOverlay={axOverlay}
110 onAxOverlayChange={setAxOverlay}
111 onClose={() => setToolsOpen(false)}
112 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"
113 />
114 ) : null}
115 </>
116 );