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