|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { |
| 3 | + gitHubOwnersOnHost, |
| 4 | + gitHubRepositoryOf, |
| 5 | + groupGitHubAccounts, |
| 6 | + nextGitHubHosts, |
| 7 | + nextGitHubRules, |
| 8 | + type GitHubHostGroup, |
| 9 | +} from "@t3tools/client-runtime/github-accounts"; |
| 10 | +import { readGitHubSettings } from "@t3tools/client-runtime/source-control-clients"; |
| 11 | +import { AuthSettingsWriteScope, type ProjectId } from "@t3tools/contracts"; |
| 12 | +import { useState } from "react"; |
| 13 | +import { View } from "react-native"; |
| 14 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 15 | + |
| 16 | +import { AppText as Text } from "../../components/ui/AppText"; |
| 17 | +import { ScreenScrollView } from "../../components/ui/ScreenScrollView"; |
| 18 | +import { environmentProjects } from "../../state/projects"; |
| 19 | +import { useEnvironmentQuery } from "../../state/query"; |
| 20 | +import { serverEnvironment } from "../../state/server"; |
| 21 | +import { useEnvironmentsWithScope } from "../../state/session"; |
| 22 | +import { sourceControlEnvironment } from "../../state/sourceControl"; |
| 23 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 24 | +import { |
| 25 | + AndroidSettingsEnvironmentFilter, |
| 26 | + SettingsEnvironmentFilterHeader, |
| 27 | +} from "./components/SettingsEnvironmentFilterHeader"; |
| 28 | +import { SettingsChoiceRow } from "./components/SettingsChoiceRow"; |
| 29 | +import { SettingsScreen } from "./components/SettingsScreen"; |
| 30 | +import { SettingsSection } from "./components/SettingsSection"; |
| 31 | +import { useSettingsEnvironmentFilter, type SettingsTarget } from "./settings-environment-filter"; |
| 32 | + |
| 33 | +/** |
| 34 | + * Which `gh` login reads each GitHub host, and each repository owner on it, for the selected |
| 35 | + * environments. The logins are the server's own, so only a host signed in more than once has a |
| 36 | + * choice to make here. |
| 37 | + */ |
| 38 | +export function SettingsGitHubAccountsRouteScreen() { |
| 39 | + const { selectedTargets } = useSettingsEnvironmentFilter(); |
| 40 | + const insets = useSafeAreaInsets(); |
| 41 | + return ( |
| 42 | + <> |
| 43 | + <SettingsEnvironmentFilterHeader /> |
| 44 | + <SettingsScreen title="GitHub accounts" trailing={<AndroidSettingsEnvironmentFilter />}> |
| 45 | + <ScreenScrollView |
| 46 | + className="flex-1" |
| 47 | + contentInsetAdjustmentBehavior="automatic" |
| 48 | + contentContainerClassName="gap-6 px-5 pt-4" |
| 49 | + contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }} |
| 50 | + > |
| 51 | + {selectedTargets.length === 0 ? ( |
| 52 | + <Text className="text-foreground-muted">Select a connected environment.</Text> |
| 53 | + ) : ( |
| 54 | + selectedTargets.map((environment) => ( |
| 55 | + <EnvironmentGitHubAccounts |
| 56 | + key={environment.environmentId} |
| 57 | + environment={environment} |
| 58 | + labelled={selectedTargets.length > 1} |
| 59 | + /> |
| 60 | + )) |
| 61 | + )} |
| 62 | + </ScreenScrollView> |
| 63 | + </SettingsScreen> |
| 64 | + </> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +/** |
| 69 | + * One environment's GitHub logins and account choices, and how to change them: the hosts its |
| 70 | + * `gh` holds more than one working login for, and a save that replaces one host's choices. |
| 71 | + */ |
| 72 | +function useGitHubAccountChoices(environment: SettingsTarget) { |
| 73 | + const environmentId = environment.environmentId; |
| 74 | + const discovery = useEnvironmentQuery( |
| 75 | + sourceControlEnvironment.discovery({ environmentId, input: {} }), |
| 76 | + ); |
| 77 | + const projects = useAtomValue(environmentProjects.environmentProjectsAtom(environmentId)); |
| 78 | + const writable = useEnvironmentsWithScope([environment], AuthSettingsWriteScope).has( |
| 79 | + environmentId, |
| 80 | + ); |
| 81 | + const updateSettings = useAtomCommand(serverEnvironment.updateSettings, { |
| 82 | + label: "save GitHub account settings", |
| 83 | + reportFailure: true, |
| 84 | + }); |
| 85 | + const [saving, setSaving] = useState(false); |
| 86 | + const hosts = readGitHubSettings(environment.serverConfig.settings).hosts; |
| 87 | + const github = discovery.data?.sourceControlProviders.find( |
| 88 | + (provider) => provider.kind === "github", |
| 89 | + ); |
| 90 | + const groups = groupGitHubAccounts(github?.auth.accounts ?? []).filter( |
| 91 | + (group) => group.selectable.length > 1, |
| 92 | + ); |
| 93 | + |
| 94 | + const save = async (host: string, change: Parameters<typeof nextGitHubHosts>[2]) => { |
| 95 | + if (saving) return; |
| 96 | + setSaving(true); |
| 97 | + try { |
| 98 | + await updateSettings({ |
| 99 | + environmentId, |
| 100 | + input: { |
| 101 | + patch: { |
| 102 | + sourceControlHosts: { github: { hosts: nextGitHubHosts(hosts, host, change) } }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }); |
| 106 | + } finally { |
| 107 | + setSaving(false); |
| 108 | + } |
| 109 | + }; |
| 110 | + return { discovery, github, projects, writable, saving, hosts, groups, save }; |
| 111 | +} |
| 112 | + |
| 113 | +function EnvironmentGitHubAccounts({ |
| 114 | + environment, |
| 115 | + labelled, |
| 116 | +}: { |
| 117 | + readonly environment: SettingsTarget; |
| 118 | + readonly labelled: boolean; |
| 119 | +}) { |
| 120 | + const { discovery, github, projects, writable, saving, hosts, groups, save } = |
| 121 | + useGitHubAccountChoices(environment); |
| 122 | + const identities = projects.map((project) => project.repositoryIdentity); |
| 123 | + const prefix = labelled ? `${environment.label} · ` : ""; |
| 124 | + |
| 125 | + if (groups.length === 0) { |
| 126 | + return ( |
| 127 | + <SettingsSection title={`${prefix}GitHub`}> |
| 128 | + <Text className="p-4 text-foreground-muted"> |
| 129 | + {discovery.data === null |
| 130 | + ? "Checking which GitHub logins this server holds…" |
| 131 | + : "This server is signed in to one GitHub login per host. Run gh auth login on the server to add another."} |
| 132 | + </Text> |
| 133 | + </SettingsSection> |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + return ( |
| 138 | + <View className="gap-6"> |
| 139 | + {groups.map((group) => ( |
| 140 | + <HostAccounts |
| 141 | + key={group.host} |
| 142 | + group={group} |
| 143 | + prefix={prefix} |
| 144 | + choice={hosts[group.host]} |
| 145 | + owners={gitHubOwnersOnHost(group.host, identities, hosts[group.host]?.owners)} |
| 146 | + disabled={saving || !writable || hosts[group.host]?.enabled === false} |
| 147 | + onAccount={(account) => void save(group.host, { account })} |
| 148 | + onOwner={(owner, account) => |
| 149 | + void save(group.host, { |
| 150 | + owners: nextGitHubRules(hosts[group.host]?.owners, owner, account), |
| 151 | + }) |
| 152 | + } |
| 153 | + onRepository={(repository, account) => |
| 154 | + void save(group.host, { |
| 155 | + repositories: nextGitHubRules(hosts[group.host]?.repositories, repository, account), |
| 156 | + }) |
| 157 | + } |
| 158 | + /> |
| 159 | + ))} |
| 160 | + {github?.auth.accounts?.some((account) => account.environmentVariable) ? ( |
| 161 | + <Text className="px-2 text-sm text-foreground-muted"> |
| 162 | + A GitHub token is set in the server's environment, so it overrides the accounts chosen |
| 163 | + here until it is unset. |
| 164 | + </Text> |
| 165 | + ) : null} |
| 166 | + </View> |
| 167 | + ); |
| 168 | +} |
| 169 | + |
| 170 | +function HostAccounts(props: { |
| 171 | + readonly group: GitHubHostGroup; |
| 172 | + readonly prefix: string; |
| 173 | + readonly choice: ReturnType<typeof readGitHubSettings>["hosts"][string] | undefined; |
| 174 | + readonly owners: ReadonlyArray<string>; |
| 175 | + readonly disabled: boolean; |
| 176 | + readonly onAccount: (account: string | null) => void; |
| 177 | + readonly onOwner: (owner: string, account: string | null) => void; |
| 178 | + readonly onRepository: (repository: string, account: string | null) => void; |
| 179 | +}) { |
| 180 | + const { group, choice } = props; |
| 181 | + const pinned = |
| 182 | + choice?.account !== undefined && group.selectable.includes(choice.account) |
| 183 | + ? choice.account |
| 184 | + : null; |
| 185 | + // What an owner without a rule reads as: the host's pinned login, else gh's active one. |
| 186 | + const hostAccount = pinned ?? group.activeAccount; |
| 187 | + // Repositories a project gave a rule of its own, which wins over its owner's. |
| 188 | + const repositories = Object.keys(choice?.repositories ?? {}).sort((left, right) => |
| 189 | + left.localeCompare(right), |
| 190 | + ); |
| 191 | + return ( |
| 192 | + <> |
| 193 | + <SettingsSection title={`${props.prefix}${group.host}`}> |
| 194 | + <AccountChoices |
| 195 | + accounts={group.selectable} |
| 196 | + selected={pinned} |
| 197 | + defaultLabel={`Active gh account${group.activeAccount ? ` (${group.activeAccount})` : ""}`} |
| 198 | + disabled={props.disabled} |
| 199 | + onSelect={props.onAccount} |
| 200 | + /> |
| 201 | + </SettingsSection> |
| 202 | + {props.owners.map((owner) => ( |
| 203 | + <SettingsSection key={owner} title={owner}> |
| 204 | + <AccountChoices |
| 205 | + accounts={group.selectable} |
| 206 | + selected={choice?.owners?.[owner] ?? null} |
| 207 | + defaultLabel={`Default${hostAccount ? ` (${hostAccount})` : ""}`} |
| 208 | + disabled={props.disabled} |
| 209 | + onSelect={(account) => props.onOwner(owner, account)} |
| 210 | + /> |
| 211 | + </SettingsSection> |
| 212 | + ))} |
| 213 | + {repositories.map((repository) => { |
| 214 | + const ownerAccount = choice?.owners?.[repository.split("/")[0]!] ?? hostAccount; |
| 215 | + return ( |
| 216 | + <SettingsSection key={repository} title={repository}> |
| 217 | + <AccountChoices |
| 218 | + accounts={group.selectable} |
| 219 | + selected={choice?.repositories?.[repository] ?? null} |
| 220 | + defaultLabel={`Owner's account${ownerAccount ? ` (${ownerAccount})` : ""}`} |
| 221 | + disabled={props.disabled} |
| 222 | + onSelect={(account) => props.onRepository(repository, account)} |
| 223 | + /> |
| 224 | + </SettingsSection> |
| 225 | + ); |
| 226 | + })} |
| 227 | + {props.owners.length > 0 ? ( |
| 228 | + <Text className="px-2 text-sm text-foreground-muted"> |
| 229 | + Each owner's repositories are read as the account chosen for it, and agents in its |
| 230 | + projects use it too. A project can choose one for its own repository in its settings. |
| 231 | + </Text> |
| 232 | + ) : null} |
| 233 | + </> |
| 234 | + ); |
| 235 | +} |
| 236 | + |
| 237 | +/** The default first, then every login; `null` is the default. */ |
| 238 | +function AccountChoices(props: { |
| 239 | + readonly accounts: ReadonlyArray<string>; |
| 240 | + readonly selected: string | null; |
| 241 | + readonly defaultLabel: string; |
| 242 | + readonly disabled: boolean; |
| 243 | + readonly onSelect: (account: string | null) => void; |
| 244 | +}) { |
| 245 | + const stale = props.selected !== null && !props.accounts.includes(props.selected); |
| 246 | + return ( |
| 247 | + <> |
| 248 | + <SettingsChoiceRow |
| 249 | + label={props.defaultLabel} |
| 250 | + selected={props.selected === null || stale} |
| 251 | + disabled={props.disabled} |
| 252 | + onPress={() => props.onSelect(null)} |
| 253 | + /> |
| 254 | + {props.accounts.map((account) => ( |
| 255 | + <SettingsChoiceRow |
| 256 | + key={account} |
| 257 | + label={account} |
| 258 | + separated |
| 259 | + selected={props.selected === account} |
| 260 | + disabled={props.disabled} |
| 261 | + onPress={() => props.onSelect(account)} |
| 262 | + /> |
| 263 | + ))} |
| 264 | + </> |
| 265 | + ); |
| 266 | +} |
| 267 | + |
| 268 | +/** |
| 269 | + * The GitHub login one project's repository reads as, for a project selected in Settings: its |
| 270 | + * own rule, else its owner's. Only for a repository on a host the server holds more than one |
| 271 | + * login for; the rule is the repository's, so every checkout of it shares the choice. |
| 272 | + */ |
| 273 | +export function ProjectGitHubAccountSection({ |
| 274 | + environment, |
| 275 | + projectId, |
| 276 | + labelled, |
| 277 | +}: { |
| 278 | + readonly environment: SettingsTarget; |
| 279 | + readonly projectId: ProjectId; |
| 280 | + readonly labelled: boolean; |
| 281 | +}) { |
| 282 | + const { projects, writable, saving, hosts, groups, save } = useGitHubAccountChoices(environment); |
| 283 | + const repository = gitHubRepositoryOf( |
| 284 | + projects.find((project) => project.id === projectId)?.repositoryIdentity, |
| 285 | + ); |
| 286 | + const group = |
| 287 | + repository === null ? undefined : groups.find((entry) => entry.host === repository.host); |
| 288 | + if (repository === null || group === undefined) return null; |
| 289 | + |
| 290 | + const choice = hosts[repository.host]; |
| 291 | + const owner = repository.repository.split("/")[0]!; |
| 292 | + const pinned = |
| 293 | + choice?.account !== undefined && group.selectable.includes(choice.account) |
| 294 | + ? choice.account |
| 295 | + : null; |
| 296 | + const ownerAccount = choice?.owners?.[owner] ?? pinned ?? group.activeAccount; |
| 297 | + return ( |
| 298 | + <SettingsSection |
| 299 | + title={`${labelled ? `${environment.label} · ` : ""}GitHub account`} |
| 300 | + footer={`Pull requests in ${repository.repository} are read as this login, and agents in its projects use it. Owner's account follows the ${owner} rule under GitHub accounts.`} |
| 301 | + > |
| 302 | + <AccountChoices |
| 303 | + accounts={group.selectable} |
| 304 | + selected={choice?.repositories?.[repository.repository] ?? null} |
| 305 | + defaultLabel={`Owner's account${ownerAccount ? ` (${ownerAccount})` : ""}`} |
| 306 | + disabled={saving || !writable || choice?.enabled === false} |
| 307 | + onSelect={(account) => |
| 308 | + void save(repository.host, { |
| 309 | + repositories: nextGitHubRules(choice?.repositories, repository.repository, account), |
| 310 | + }) |
| 311 | + } |
| 312 | + /> |
| 313 | + </SettingsSection> |
| 314 | + ); |
| 315 | +} |
0 commit comments