The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { m } from "~/i18n/messages"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import type { DeviceHostSummary, DeviceSummary, EnvironmentId } from "@t3tools/contracts"; |
| 5 | import { useGT } from "gt-react"; |
| 6 | import { useRef, useState } from "react"; |
| 7 | import { |
| 8 | captureDeviceScreenshot, |
| 9 | DeviceScreenshotError, |
| 10 | } from "@t3tools/client-runtime/device/screenshot"; |
| 11 | import { refreshDeviceHubAccess, useDeviceHubAccess } from "~/state/device"; |
| 12 | import { DeviceControlsRail } from "./DeviceControlsRail"; |
| 13 | import { DeviceStreamView } from "./DeviceStreamView"; |
| 14 | import { DeviceToolsPanel } from "./DeviceToolsPanel"; |
| 15 | import { useDeviceControls } from "./useDeviceControls"; |
| 16 | |
| 17 | const isDeviceScreenshotError = Schema.is(DeviceScreenshotError); |
| 18 | |
| 19 | const DOWNLOAD_URL_LIFETIME_MS = 60_000; |
| 20 | |
| 21 | export function DeviceWorkspace(props: { |
| 22 | environmentId: EnvironmentId; |
| 23 | device: DeviceSummary; |
| 24 | hostLabel: string; |
| 25 | hostKind: DeviceHostSummary["kind"] | undefined; |
| 26 | hostDiagnostics: string | undefined; |
| 27 | visible: boolean; |
| 28 | onFloat: () => void; |
| 29 | onClose: () => void; |
| 30 | onPowerOff: () => void; |
| 31 | }) { |
| 32 | const gt = useGT(); |
| 33 | const [toolsOpen, setToolsOpen] = useState(false); |
| 34 | const [axOverlay, setAxOverlay] = useState(false); |
| 35 | const [screenshotPending, setScreenshotPending] = useState(false); |
| 36 | const [screenshotError, setScreenshotError] = useState<string | null>(null); |
| 37 | // A capture always finishes, even if the panel hides or closes meanwhile; the user asked for it. |
| 38 | const capturing = useRef(false); |
| 39 | const access = useDeviceHubAccess(props.environmentId, props.device.hostId); |
| 40 | // Settings and tools run commands on the device's own machine, which Expo and Simlock do not expose. |
| 41 | const runsHostCommands = props.hostKind === "local" || props.hostKind === "ssh"; |
| 42 | const controls = useDeviceControls({ |
| 43 | ...props, |
| 44 | access, |
| 45 | visible: props.visible && runsHostCommands, |
| 46 | }); |
| 47 | const saveScreenshot = () => { |
| 48 | if (!access || capturing.current || !props.visible) return; |
| 49 | capturing.current = true; |
| 50 | setScreenshotPending(true); |
| 51 | setScreenshotError(null); |
| 52 | return captureDeviceScreenshot( |
| 53 | { access, platform: props.device.platform, deviceId: props.device.id }, |
| 54 | // Never aborted: see `capturing` above. |
| 55 | new AbortController().signal, |
| 56 | ) |
| 57 | .then((image) => { |
| 58 | const url = URL.createObjectURL(image); |
| 59 | const link = document.createElement("a"); |
| 60 | link.href = url; |
| 61 | link.download = `${props.device.name.replace(/[^a-z0-9-]/gi, "-")}-${Date.now()}.png`; |
| 62 | document.body.append(link); |
| 63 | link.click(); |
| 64 | link.remove(); |
| 65 | // The browser reads the blob after the click returns. |
| 66 | window.setTimeout(() => URL.revokeObjectURL(url), DOWNLOAD_URL_LIFETIME_MS); |
| 67 | }) |
| 68 | .catch((cause: unknown) => { |
| 69 | if (isDeviceScreenshotError(cause) && cause.status === 401) |
| 70 | refreshDeviceHubAccess(props.environmentId); |
| 71 | setScreenshotError( |
| 72 | thrownErrorMessage(cause, m) ?? gt("Screenshot capture failed. Try again."), |
| 73 | ); |
| 74 | }) |
| 75 | .finally(() => { |
| 76 | capturing.current = false; |
| 77 | setScreenshotPending(false); |
| 78 | }); |
| 79 | }; |
| 80 | return ( |
| 81 | <> |
| 82 | <div className="relative flex min-h-0 min-w-0 flex-1 flex-col"> |
| 83 | {screenshotError || (controls.error && !toolsOpen) ? ( |
| 84 | <p role="alert" className="px-3 py-2 text-xs text-destructive"> |
| 85 | {screenshotError ?? controls.error} |
| 86 | </p> |
| 87 | ) : null} |
| 88 | <div className="min-h-0 flex-1"> |
| 89 | <DeviceStreamView |
| 90 | environmentId={props.environmentId} |
| 91 | platform={props.device.platform} |
| 92 | deviceName={props.device.name} |
| 93 | deviceDescription={`${props.hostLabel} · ${props.device.version}`} |
| 94 | deviceId={props.device.id} |
| 95 | hostId={props.device.hostId} |
| 96 | visible={props.visible} |
| 97 | serveSimOnly={props.hostKind === "simlock"} |
| 98 | axOverlay={axOverlay} |
| 99 | allowPhoneView |
| 100 | renderControls={(view) => ( |
| 101 | <DeviceControlsRail |
| 102 | platform={props.device.platform} |
| 103 | hostKind={props.hostKind} |
| 104 | handle={view.handle} |
| 105 | view={view} |
| 106 | controls={controls} |
| 107 | screenshotPending={screenshotPending} |
| 108 | onScreenshot={() => void saveScreenshot()} |
| 109 | toolsOpen={toolsOpen} |
| 110 | onTools={() => setToolsOpen(!toolsOpen)} |
| 111 | onFloat={props.onFloat} |
| 112 | onClose={props.onClose} |
| 113 | onPowerOff={props.onPowerOff} |
| 114 | /> |
| 115 | )} |
| 116 | /> |
| 117 | </div> |
| 118 | </div> |
| 119 | {toolsOpen ? ( |
| 120 | <DeviceToolsPanel |
| 121 | device={props.device} |
| 122 | controls={controls} |
| 123 | hostDiagnostics={props.hostDiagnostics} |
| 124 | access={access} |
| 125 | axOverlay={axOverlay} |
| 126 | onAxOverlayChange={setAxOverlay} |
| 127 | onClose={() => setToolsOpen(false)} |
| 128 | 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" |
| 129 | /> |
| 130 | ) : null} |
| 131 | </> |
| 132 | ); |
| 133 | } |