Open History to see the last commit
1import type { EnvironmentId, SourceControlProviderAuth } from "@t3tools/contracts";
2import { EyeIcon, EyeOffIcon } from "lucide-react";
3import { useState } from "react";
4
5import { useEnvironmentSettings } from "../../hooks/useSettings";
6import { serverEnvironment } from "../../state/server";
7import { useAtomCommand } from "../../state/use-atom-command";
8import { Button } from "../ui/button";
9import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
10import { Switch } from "../ui/switch";
11import { groupGitHubAccounts, nextGitHubHosts } from "./GitHubAccountSettings.logic";
12import { readGitHubSettings } from "@t3tools/client-runtime/source-control-clients";
13import { redactedPlaceholder } from "./RedactedSensitiveText";
14
15/** Sentinel select value for "follow gh's active login"; logins never contain spaces. */
16const ACTIVE_ACCOUNT = "active gh account";
17
18function RedactedLogin(props: {
19 readonly account: string;
20 readonly revealed: boolean;
21 readonly label?: string;
22}) {
23 return props.revealed ? (
24 <span className="min-w-0 truncate font-mono text-2xs">{props.account}</span>
25 ) : (
26 <span className="min-w-0 truncate font-mono text-2xs">
27 <span className="select-none blur-xs" aria-hidden>
28 {redactedPlaceholder(props.account)}
29 </span>
30 <span className="sr-only">{props.label ?? "Hidden account"}</span>
31 </span>
32 );
33}
34
35export function GitHubAccountSettings({
36 environmentId,
37 auth,
38 onSaved,
39}: {
40 readonly environmentId: EnvironmentId;
41 readonly auth: SourceControlProviderAuth;
42 readonly onSaved: () => void;
43}) {
44 const github = useEnvironmentSettings(environmentId, readGitHubSettings);
45 const hosts = github.hosts;
46 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
47 label: "save GitHub account settings",
48 });
49 const [saving, setSaving] = useState(false);
50 const [revealed, setRevealed] = useState(false);
51 const groups = groupGitHubAccounts(auth.accounts ?? []);
52
53 const save = async (
54 host: string,
55 change: { readonly enabled?: boolean; readonly account?: string | null },
56 ) => {
57 setSaving(true);
58 const result = await updateSettings({
59 environmentId,
60 input: {
61 patch: {
62 sourceControlHosts: { github: { hosts: nextGitHubHosts(hosts, host, change) } },
63 },
64 },
65 });
66 setSaving(false);
67 if (result._tag === "Success") onSaved();
68 };
69
70 if (groups.length === 0) {
71 return (
72 <p className="text-xs text-muted-foreground">
73 Sign in with <code className="rounded bg-muted px-1 py-px text-2xs">gh auth login</code> on
74 the server host, then rescan to choose accounts here.
75 </p>
76 );
77 }
78
79 return (
80 <div className="grid gap-4">
81 <div className="flex items-center justify-between gap-3">
82 <p className="max-w-2xl text-xs leading-relaxed text-muted-foreground">
83 Choose which <code className="rounded bg-muted px-1 py-px text-2xs">gh</code> login each
84 GitHub host uses, or turn a host off.
85 </p>
86 <Button
87 size="icon-xs"
88 variant="ghost-muted"
89 onClick={() => setRevealed((current) => !current)}
90 aria-label={revealed ? "Hide GitHub accounts" : "Reveal GitHub accounts"}
91 aria-pressed={revealed}
92 >
93 {revealed ? <EyeOffIcon /> : <EyeIcon />}
94 </Button>
95 </div>
96 {groups.map((group) => {
97 const choice = hosts[group.host];
98 const enabled = choice?.enabled ?? true;
99 const stalePin =
100 choice?.account !== undefined && !group.selectable.includes(choice.account);
101 const pinned = choice?.account !== undefined && !stalePin ? choice.account : ACTIVE_ACCOUNT;
102 return (
103 <div key={group.host} className="grid gap-2">
104 <div className="flex items-center justify-between gap-3">
105 <div className="min-w-0 space-y-0.5">
106 <span className="text-xs font-medium text-foreground">{group.host}</span>
107 {group.selectable.length === 1 && group.selectable[0] !== undefined ? (
108 <p className="flex min-w-0 items-center gap-1 text-xs text-muted-foreground">
109 <span>Signed in as</span>
110 <RedactedLogin revealed={revealed} account={group.selectable[0]} />
111 </p>
112 ) : null}
113 </div>
114 <Switch
115 checked={enabled}
116 disabled={saving}
117 aria-label={`Use GitHub on ${group.host}`}
118 onCheckedChange={(checked) => void save(group.host, { enabled: checked })}
119 />
120 </div>
121 {group.selectable.length > 1 ? (
122 <div className="flex items-center justify-between gap-3">
123 <span className="text-xs text-muted-foreground">Account</span>
124 <div className="w-64 max-w-full">
125 <Select
126 value={pinned}
127 disabled={saving || !enabled}
128 onValueChange={(value) => {
129 if (typeof value !== "string") return;
130 void save(group.host, {
131 account: value === ACTIVE_ACCOUNT ? null : value,
132 });
133 }}
134 >
135 <SelectTrigger size="sm" aria-label={`GitHub account for ${group.host}`}>
136 <SelectValue>
137 {(value: string) =>
138 value === ACTIVE_ACCOUNT ? (
139 <span className="flex min-w-0 items-center gap-1">
140 Active gh account
141 {group.activeAccount ? (
142 <>
143 (
144 <RedactedLogin
145 revealed={revealed}
146 account={group.activeAccount}
147 />
148 )
149 </>
150 ) : null}
151 </span>
152 ) : (
153 <RedactedLogin revealed={revealed} account={value} />
154 )
155 }
156 </SelectValue>
157 </SelectTrigger>
158 <SelectPopup align="end" alignItemWithTrigger={false}>
159 <SelectItem value={ACTIVE_ACCOUNT}>
160 <span className="flex min-w-0 items-center gap-1">
161 Active gh account
162 {group.activeAccount ? (
163 <>
164 (<RedactedLogin revealed={revealed} account={group.activeAccount} />)
165 </>
166 ) : null}
167 </span>
168 </SelectItem>
169 {group.selectable.map((account, index) => (
170 <SelectItem key={account} value={account}>
171 <RedactedLogin
172 revealed={revealed}
173 account={account}
174 label={`Account ${index + 1}`}
175 />
176 </SelectItem>
177 ))}
178 </SelectPopup>
179 </Select>
180 </div>
181 </div>
182 ) : null}
183 {stalePin ? (
184 <div className="flex items-center justify-between gap-3">
185 <p className="text-xs text-warning">
186 The chosen login is no longer signed in, so the active gh login is used.
187 </p>
188 <Button
189 size="xs"
190 variant="outline"
191 disabled={saving}
192 onClick={() => void save(group.host, { account: null })}
193 >
194 Use active login
195 </Button>
196 </div>
197 ) : null}
198 {group.broken.map((entry) => (
199 <p
200 key={entry.account}
201 className="flex min-w-0 flex-wrap items-center gap-1 text-xs text-muted-foreground/70"
202 >
203 <RedactedLogin revealed={revealed} account={entry.account} />
204 <span>can't be used: {entry.error ?? "gh reports this login as invalid."}</span>
205 </p>
206 ))}
207 {group.environmentVariable ? (
208 <p className="text-xs text-warning">
209 {group.environmentVariable} is set on the server, so it overrides the account chosen
210 here until it is unset.
211 </p>
212 ) : null}
213 </div>
214 );
215 })}
216 </div>
217 );
218}