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

main
tsx 82 lines 2.98 KB
Raw
1 import type { ProviderAuthResponse } from "@t3tools/contracts";
2 import { useGT } from "gt-react";
3 import { useEffect, useRef, useState } from "react";
4 import { GhosttyTerminalSurface } from "../../terminal/ghostty/surface";
5 import { ensureLocalApi } from "../../localApi";
6 import { useStableCallback } from "../../hooks/useStableCallback";
7 import { terminalThemeFromApp } from "../ThreadTerminalDrawer";
8
9 export default function ProviderAuthTerminal({
10 output,
11 outputOffset,
12 onResponse,
13 }: {
14 readonly output: string;
15 readonly outputOffset?: number | undefined;
16 readonly onResponse: (response: Extract<ProviderAuthResponse, { type: "terminal" }>) => void;
17 }) {
18 const gt = useGT();
19 const [surface, setSurface] = useState<GhosttyTerminalSurface | null>(null);
20 const written = useRef(0);
21 const [error, setError] = useState<string | null>(null);
22 // The surface captures its callbacks once at creation.
23 const respond = useStableCallback(onResponse);
24 const offset = outputOffset ?? output.length;
25 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- writes new auth output into the imperative Ghostty terminal surface
26 useEffect(() => {
27 if (!surface) return;
28 const delta = offset - written.current;
29 if (delta > 0 && delta <= output.length) surface.write(output.slice(-delta));
30 else if (delta !== 0) surface.resetAndWrite(output);
31 written.current = offset;
32 }, [surface, output, offset]);
33 const mountSurface = (element: HTMLDivElement) => {
34 let disposed = false;
35 let created: GhosttyTerminalSurface | null = null;
36 void GhosttyTerminalSurface.create(element, {
37 theme: terminalThemeFromApp(element),
38 font: { size: 13 },
39 onData: (data) => respond({ type: "terminal", data }),
40 onBinary: (data) => respond({ type: "terminal", data, binary: true }),
41 onReply: (data) => respond({ type: "terminal", data }),
42 onResize: (cols, rows) => respond({ type: "terminal", data: "", size: { cols, rows } }),
43 onSelectionChange: () => {},
44 beforeKey: (event) => event.key !== "Tab",
45 onLinkActivate: (url) => {
46 if (/^https?:\/\//i.test(url))
47 void ensureLocalApi()
48 .shell.openExternal(url)
49 .catch(() => setError(gt("Could not open the provider link.")));
50 },
51 })
52 .then((terminal) => {
53 if (disposed) {
54 terminal.dispose();
55 return;
56 }
57 created = terminal;
58 written.current = 0;
59 setSurface(terminal);
60 })
61 .catch(() => setError(gt("Could not load the sign-in terminal. Cancel and retry sign-in.")));
62 return () => {
63 disposed = true;
64 created?.dispose();
65 setSurface(null);
66 };
67 };
68 return (
69 <>
70 <div
71 ref={mountSurface}
72 aria-label={gt("Provider sign-in terminal")}
73 className="relative h-64 overflow-hidden rounded-md border border-border"
74 />
75 {error ? (
76 <p role="alert" className="text-destructive">
77 {error}
78 </p>
79 ) : null}
80 </>
81 );
82 }