Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SourceControlSignInSection.tsx

140 lines4.9 KB

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