The best way to control Claude Code, Codex and more from any computer or phone

main
tsx 94 lines 3.11 KB
Raw
1 import type { DesktopPreviewPointerEvent } from "@t3tools/contracts";
2 import { MousePointer2 } from "lucide-react";
3 import { useEffect, useState } from "react";
4
5 import { useBrowserPointerStore } from "~/browser/browserPointerStore";
6 import { useBrowserSurfaceStore } from "~/browser/browserSurfaceStore";
7
8 import { agentBrowserCursorOpacity, type BrowserController } from "./agentBrowserCursorLogic";
9
10 const CURSOR_ACTIVE_MS = 700;
11
12 export function AgentBrowserCursor(props: {
13 readonly tabId: string;
14 readonly zoomFactor: number;
15 readonly controller: BrowserController;
16 }) {
17 const { tabId, zoomFactor, controller } = props;
18 const event = useBrowserPointerStore((state) => state.byTabId[tabId] ?? null);
19 const content = useBrowserSurfaceStore((state) => state.byTabId[tabId]?.content ?? null);
20
21 if (!event) return null;
22
23 return (
24 <AgentBrowserCursorEvent
25 event={event}
26 content={content}
27 zoomFactor={zoomFactor}
28 controller={controller}
29 />
30 );
31 }
32
33 function AgentBrowserCursorEvent(props: {
34 readonly event: DesktopPreviewPointerEvent;
35 readonly content: {
36 readonly x: number;
37 readonly y: number;
38 readonly scale: number;
39 readonly scrollLeft: number;
40 readonly scrollTop: number;
41 } | null;
42 readonly zoomFactor: number;
43 readonly controller: BrowserController;
44 }) {
45 const { event, content, zoomFactor, controller } = props;
46 const scale = zoomFactor * (content?.scale ?? 1);
47 return (
48 <AgentCursorMark
49 phase={event.phase}
50 sequence={event.sequence}
51 left={event.x * scale + (content?.x ?? 0) - (content?.scrollLeft ?? 0)}
52 top={event.y * scale + (content?.y ?? 0) - (content?.scrollTop ?? 0)}
53 controller={controller}
54 />
55 );
56 }
57
58 export function AgentCursorMark(props: {
59 readonly phase: "move" | "click";
60 readonly sequence: number;
61 readonly left: number;
62 readonly top: number;
63 readonly controller: BrowserController;
64 }) {
65 const { phase, sequence, left, top, controller } = props;
66 const [inactiveSequence, setInactiveSequence] = useState<number | null>(null);
67 const active = inactiveSequence !== sequence;
68
69 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- timer fades the agent cursor after each move or click
70 useEffect(() => {
71 const timeout = window.setTimeout(() => setInactiveSequence(sequence), CURSOR_ACTIVE_MS);
72 return () => window.clearTimeout(timeout);
73 }, [sequence]);
74
75 return (
76 <div
77 className="pointer-events-none absolute left-0 top-0 z-40 transition-[opacity,transform] duration-150 ease-out motion-reduce:transition-none"
78 style={{
79 opacity: agentBrowserCursorOpacity(active, controller),
80 transform: `translate3d(${left}px, ${top}px, 0)`,
81 }}
82 aria-hidden="true"
83 data-agent-browser-cursor
84 >
85 {phase === "click" ? (
86 <span
87 key={sequence}
88 className="absolute left-0.5 top-0.5 size-4 animate-status-ping rounded-full bg-primary/25 motion-reduce:animate-none"
89 />
90 ) : null}
91 <MousePointer2 className="relative size-5 -translate-x-0.5 -translate-y-0.5 fill-background stroke-2 text-primary drop-shadow-sm" />
92 </div>
93 );
94 }