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

GitHubAccountSettings.tsx

221 lines8.7 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 { 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 );
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 try {
59 const result = await updateSettings({
60 environmentId,
61 input: {
62 patch: {
63 sourceControlHosts: { github: { hosts: nextGitHubHosts(hosts, host, change) } },
64 },
65 },
66 });
67 if (result._tag === "Success") onSaved();
68 } finally {
69 setSaving(false);
70 }
71 };
72​
73 if (groups.length === 0) {
74 return (
75 <p className="text-xs text-muted-foreground">
76 Sign in with <code className="rounded bg-muted px-1 py-px text-2xs">gh auth login</code> on
77 the server host, then rescan to choose accounts here.
78 </p>
79 );
80 }
81​
82 return (
83 <div className="grid gap-4">
84 <div className="flex items-center justify-between gap-3">
85 <p className="max-w-2xl text-xs leading-relaxed text-muted-foreground">
86 Choose which <code className="rounded bg-muted px-1 py-px text-2xs">gh</code> login each
87 GitHub host uses, or turn a host off.
88 </p>
89 <Button
90 size="icon-xs"
91 variant="ghost-muted"
92 onClick={() => setRevealed((current) => !current)}
93 aria-label={revealed ? "Hide GitHub accounts" : "Reveal GitHub accounts"}
94 aria-pressed={revealed}
95 >
96 {revealed ? <EyeOffIcon /> : <EyeIcon />}
97 </Button>
98 </div>
99 {groups.map((group) => {
100 const choice = hosts[group.host];
101 const enabled = choice?.enabled ?? true;
102 const stalePin =
103 choice?.account !== undefined && !group.selectable.includes(choice.account);
104 const pinned = choice?.account !== undefined && !stalePin ? choice.account : ACTIVE_ACCOUNT;
105 return (
106 <div key={group.host} className="grid gap-2">
107 <div className="flex items-center justify-between gap-3">
108 <div className="min-w-0 space-y-0.5">
109 <span className="text-xs font-medium text-foreground">{group.host}</span>
110 {group.selectable.length === 1 && group.selectable[0] !== undefined ? (
111 <p className="flex min-w-0 items-center gap-1 text-xs text-muted-foreground">
112 <span>Signed in as</span>
113 <RedactedLogin revealed={revealed} account={group.selectable[0]} />
114 </p>
115 ) : null}
116 </div>
117 <Switch
118 checked={enabled}
119 disabled={saving}
120 aria-label={`Use GitHub on ${group.host}`}
121 onCheckedChange={(checked) => void save(group.host, { enabled: checked })}
122 />
123 </div>
124 {group.selectable.length > 1 ? (
125 <div className="flex items-center justify-between gap-3">
126 <span className="text-xs text-muted-foreground">Account</span>
127 <div className="w-64 max-w-full">
128 <Select
129 value={pinned}
130 disabled={saving || !enabled}
131 onValueChange={(value) => {
132 if (typeof value !== "string") return;
133 void save(group.host, {
134 account: value === ACTIVE_ACCOUNT ? null : value,
135 });
136 }}
137 >
138 <SelectTrigger size="sm" aria-label={`GitHub account for ${group.host}`}>
139 <SelectValue>
140 {(value: string) =>
141 value === ACTIVE_ACCOUNT ? (
142 <span className="flex min-w-0 items-center gap-1">
143 Active gh account
144 {group.activeAccount ? (
145 <>
146 (
147 <RedactedLogin
148 revealed={revealed}
149 account={group.activeAccount}
150 />
151 )
152 </>
153 ) : null}
154 </span>
155 ) : (
156 <RedactedLogin revealed={revealed} account={value} />
157 )
158 }
159 </SelectValue>
160 </SelectTrigger>
161 <SelectPopup align="end" alignItemWithTrigger={false}>
162 <SelectItem value={ACTIVE_ACCOUNT}>
163 <span className="flex min-w-0 items-center gap-1">
164 Active gh account
165 {group.activeAccount ? (
166 <>
167 (<RedactedLogin revealed={revealed} account={group.activeAccount} />)
168 </>
169 ) : null}
170 </span>
171 </SelectItem>
172 {group.selectable.map((account, index) => (
173 <SelectItem key={account} value={account}>
174 <RedactedLogin
175 revealed={revealed}
176 account={account}
177 label={`Account ${index + 1}`}
178 />
179 </SelectItem>
180 ))}
181 </SelectPopup>
182 </Select>
183 </div>
184 </div>
185 ) : null}
186 {stalePin ? (
187 <div className="flex items-center justify-between gap-3">
188 <p className="text-xs text-warning">
189 The chosen login is no longer signed in, so the active gh login is used.
190 </p>
191 <Button
192 size="xs"
193 variant="outline"
194 disabled={saving}
195 onClick={() => void save(group.host, { account: null })}
196 >
197 Use active login
198 </Button>
199 </div>
200 ) : null}
201 {group.broken.map((entry) => (
202 <p
203 key={entry.account}
204 className="flex min-w-0 flex-wrap items-center gap-1 text-xs text-muted-foreground/70"
205 >
206 <RedactedLogin revealed={revealed} account={entry.account} />
207 <span>can't be used: {entry.error ?? "gh reports this login as invalid."}</span>
208 </p>
209 ))}
210 {group.environmentVariable ? (
211 <p className="text-xs text-warning">
212 {group.environmentVariable} is set on the server, so it overrides the account chosen
213 here until it is unset.
214 </p>
215 ) : null}
216 </div>
217 );
218 })}
219 </div>
220 );