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

main
tsx 153 lines 5.35 KB
Raw
1 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2 import { useGT } from "gt-react-native";
3 import type { SourceControlSignInPrompt } from "@t3tools/contracts";
4 import {
5 readHostCredentialSettings,
6 type SourceControlAppSignIn,
7 type SourceControlDefinition,
8 } from "@t3tools/client-runtime/source-control-clients";
9 import * as Cause from "effect/Cause";
10 import * as Clipboard from "expo-clipboard";
11 import { useRef, useState } from "react";
12 import { tryOpenExternalUrl } from "../../../lib/openExternalUrl";
13 import { m } from "../../../i18n/messages";
14
15 import * as Dialog from "../../../components/ui/Dialog";
16 import { serverEnvironment } from "../../../state/server";
17 import { sourceControlEnvironment } from "../../../state/sourceControl";
18 import { useAtomCommand } from "../../../state/use-atom-command";
19 import type { SettingsTarget } from "../settings-environment-filter";
20 import { SettingsRow } from "./SettingsRow";
21
22 const failureMessage = (cause: Cause.Cause<unknown>) => {
23 const error = Cause.squash(cause);
24 return error instanceof Error ? (errorDisplayMessage(error, m) ?? error.message) : String(error);
25 };
26
27 const openUrl = (url: string) => void tryOpenExternalUrl(url, "provider-auth");
28
29 /** Signs an environment in through a forge's own app, such as T3 Code for GitHub. */
30 export function AppSignInRow(props: {
31 readonly environment: SettingsTarget;
32 readonly label: string;
33 readonly definition: SourceControlDefinition;
34 readonly appSignIn: SourceControlAppSignIn;
35 readonly disabled: boolean;
36 }) {
37 const gt = useGT();
38 const { environment, definition, appSignIn } = props;
39 const environmentId = environment.environmentId;
40 const host = appSignIn.hosts[0] ?? "";
41 const { signIns } = readHostCredentialSettings(
42 environment.serverConfig.settings,
43 definition.kind,
44 );
45 const signedIn = (signIns[host] ?? "").length > 0;
46 const start = useAtomCommand(sourceControlEnvironment.startSignIn, { reportFailure: false });
47 const wait = useAtomCommand(sourceControlEnvironment.waitForSignIn, { reportFailure: false });
48 const cancel = useAtomCommand(sourceControlEnvironment.cancelSignIn, { reportFailure: false });
49 // The attempt a pending `wait` belongs to; a cancel or a newer attempt makes its outcome stale.
50 const attempt = useRef<SourceControlSignInPrompt | null>(null);
51 const input = { provider: definition.kind, host };
52 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
53 label: `sign out of ${appSignIn.appName}`,
54 });
55 const [pending, setPending] = useState<SourceControlSignInPrompt | "starting" | null>(null);
56
57 const showCode = (prompt: SourceControlSignInPrompt) => {
58 void Clipboard.setStringAsync(prompt.userCode).catch(() => undefined);
59 Dialog.alert(
60 prompt.userCode,
61 gt(
62 "Enter this code on {host}, then approve {app}. It is copied. The server finishes signing in even if you leave the app.",
63 { host: "GitHub", app: appSignIn.appName },
64 ),
65 [
66 {
67 text: gt("Cancel"),
68 style: "cancel",
69 onPress: () => {
70 attempt.current = null;
71 setPending(null);
72 void cancel({ environmentId, input });
73 },
74 },
75 {
76 text: gt("Open {host}", { host: "GitHub" }),
77 onPress: () => openUrl(prompt.verificationUri),
78 },
79 ],
80 );
81 };
82
83 const signIn = async () => {
84 setPending("starting");
85 const started = await start({ environmentId, input });
86 if (started._tag !== "Success") {
87 setPending(null);
88 Dialog.alert(gt("Could not start the sign-in"), failureMessage(started.cause));
89 return;
90 }
91 const prompt = started.value;
92 attempt.current = prompt;
93 setPending(prompt);
94 showCode(prompt);
95 const finished = await wait({ environmentId, input });
96 if (attempt.current !== prompt) return;
97 attempt.current = null;
98 setPending(null);
99 if (finished._tag !== "Success") {
100 Dialog.alert(gt("Sign-in did not finish"), failureMessage(finished.cause));
101 }
102 };
103
104 const signOut = () =>
105 void updateSettings({
106 environmentId,
107 // Send the whole map; other hosts' redaction markers preserve their sessions.
108 input: {
109 patch: {
110 sourceControlHosts: { [definition.kind]: { signIns: { ...signIns, [host]: "" } } },
111 },
112 },
113 });
114
115 const onPress = () => {
116 if (signedIn) {
117 Dialog.alert(
118 appSignIn.appName,
119 gt(
120 "Signed in. {host} requests use the app's higher rate limit, in repositories where the app is installed.",
121 { host: "GitHub" },
122 ),
123 [
124 { text: gt("Install on an organization"), onPress: () => openUrl(appSignIn.installUrl) },
125 { text: gt("Sign out"), style: "destructive", onPress: signOut },
126 { text: gt("Cancel"), style: "cancel" },
127 ],
128 );
129 } else if (pending !== null && pending !== "starting") {
130 showCode(pending);
131 } else if (pending === null) {
132 void signIn();
133 }
134 };
135
136 return (
137 <SettingsRow
138 icon="person.crop.circle"
139 label={props.label}
140 value={
141 signedIn
142 ? gt("Signed in")
143 : pending === "starting"
144 ? gt("Starting…")
145 : pending !== null
146 ? gt("Enter {code}", { code: pending.userCode })
147 : gt("Sign in")
148 }
149 disabled={props.disabled}
150 onPress={onPress}
151 />
152 );
153 }