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

GitHubAccountSettings.tsx

224 lines9.0 KB

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