@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
tsx 128 lines 5.3 KB
Raw
1 import { useGT } from "gt-react";
2 import { useEffect, useRef } from "react";
3 import type { PhoneViewer } from "@t3tools/client-runtime/device/phone-viewer";
4 import type {
5 DeviceAccessorySource,
6 DeviceModelSource,
7 } from "@t3tools/client-runtime/device/model";
8 import type { DeviceShapeProfile } from "@t3tools/client-runtime/device/shape-profile";
9 import type { createPhoneInteraction } from "@t3tools/client-runtime/device/phone-interaction";
10 import type { DeviceScreenSize, DeviceStreamClient } from "@t3tools/client-runtime/device/stream";
11 import { mountDeviceViewer } from "./mountDeviceViewer";
12
13 const loadPhoneViewer = () => import("@t3tools/client-runtime/device/phone-viewer");
14
15 export function DevicePhoneViewport(props: {
16 readonly profile: DeviceShapeProfile;
17 readonly model: DeviceModelSource | null;
18 readonly accessory: DeviceAccessorySource | null;
19 readonly source: HTMLCanvasElement;
20 readonly onFrameListener: (listener: (() => void) | null) => void;
21 readonly onResetReady: (reset: (() => void) | null) => void;
22 readonly onInputCancel: (cancel: (() => void) | null) => void;
23 readonly client: DeviceStreamClient;
24 readonly screen: DeviceScreenSize | null;
25 readonly foldAngle: number | null;
26 readonly onUnavailable: () => void;
27 }) {
28 const gt = useGT();
29 const viewerRef = useRef<PhoneViewer | null>(null);
30 const interactionRef = useRef<ReturnType<typeof createPhoneInteraction> | null>(null);
31 const screenRef = useRef(props.screen);
32 const foldAngleRef = useRef(props.foldAngle);
33 const profileRef = useRef(props.profile);
34 const modelRef = useRef(props.model);
35 const accessoryRef = useRef(props.accessory);
36 const { source, onFrameListener, client, onInputCancel, onResetReady, onUnavailable } = props;
37
38 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes the fold angle into the imperative 3D phone viewer
39 useEffect(() => {
40 foldAngleRef.current = props.foldAngle;
41 viewerRef.current?.setFoldAngle(props.foldAngle);
42 }, [props.foldAngle]);
43
44 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- pushes screen, profile, model and accessory changes into the imperative 3D phone viewer
45 useEffect(() => {
46 screenRef.current = props.screen;
47 profileRef.current = props.profile;
48 modelRef.current = props.model;
49 accessoryRef.current = props.accessory;
50 interactionRef.current?.end();
51 viewerRef.current?.setModel(props.model);
52 viewerRef.current?.setAccessory(props.accessory);
53 viewerRef.current?.setScreen(props.screen, props.profile);
54 }, [props.screen, props.profile, props.model, props.accessory]);
55
56 // The 3D viewer lives as long as its canvas; later prop changes are pushed into it above.
57 const mountViewer = (canvas: HTMLCanvasElement | null) => {
58 const host = canvas?.parentElement;
59 if (!canvas || !host) return;
60 return mountDeviceViewer(canvas, host, {
61 load: () =>
62 loadPhoneViewer().then(
63 ({ createPhoneViewer }) =>
64 () =>
65 createPhoneViewer({
66 canvas,
67 source,
68 onUnavailable,
69 onModelError: (cause) => console.warn("Device 3D asset could not load", cause),
70 profile: profileRef.current,
71 model: modelRef.current,
72 accessory: accessoryRef.current,
73 foldAngle: foldAngleRef.current,
74 }),
75 ),
76 touch: (phase, point) => client.sendTouch(phase, point.x, point.y),
77 viewerRef,
78 interactionRef,
79 onInputCancel,
80 onResetReady,
81 onFrameListener,
82 onUnavailable,
83 ready: ({ viewer }) => {
84 viewer.setScreen(screenRef.current);
85 return { frame: viewer.frameUpdated };
86 },
87 });
88 };
89
90 const point = (event: React.PointerEvent<HTMLCanvasElement>) => {
91 const rect = event.currentTarget.getBoundingClientRect();
92 return {
93 x: (event.clientX - rect.left) / rect.width,
94 y: (event.clientY - rect.top) / rect.height,
95 };
96 };
97 return (
98 <div className="absolute inset-0">
99 <div className="absolute inset-0">
100 <div
101 aria-hidden
102 className="pointer-events-none absolute bottom-[6%] left-1/2 h-5 w-2/5 -translate-x-1/2 rounded-full bg-foreground/10 blur-xl"
103 />
104 <canvas
105 ref={mountViewer}
106 aria-label={gt(
107 "Interactive 3D device. Drag the screen to interact. Drag outside it or swipe with two fingers to turn.",
108 )}
109 className="size-full touch-none"
110 onPointerDown={(event) => {
111 if (event.button !== 0) return;
112 if (!interactionRef.current?.begin(event.pointerId, point(event), event.altKey)) return;
113 event.preventDefault();
114 event.currentTarget.setPointerCapture(event.pointerId);
115 (event.currentTarget.closest('[role="application"]') as HTMLElement | null)?.focus();
116 }}
117 onPointerMove={(event) => interactionRef.current?.move(event.pointerId, point(event))}
118 onPointerUp={(event) => {
119 interactionRef.current?.move(event.pointerId, point(event));
120 interactionRef.current?.end(event.pointerId);
121 }}
122 onPointerCancel={(event) => interactionRef.current?.end(event.pointerId)}
123 onLostPointerCapture={(event) => interactionRef.current?.end(event.pointerId)}
124 />
125 </div>
126 </div>
127 );
128 }