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

SourceControlAccountSettings.tsx

310 lines11.5 KB

1import type { EnvironmentId, SourceControlProviderAuth } from "@t3tools/contracts";
2import { EyeIcon, EyeOffIcon } from "lucide-react";
3import { useState } from "react";
4import { T, useGT, Var } from "gt-react";
5​
6import { useEnvironmentSettings } from "../../hooks/useSettings";
7import { serverEnvironment } from "../../state/server";
8import { useAtomCommand } from "../../state/use-atom-command";
9import { Button } from "../ui/button";
10import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select";
11import { Switch } from "../ui/switch";
12import { groupHostAccounts, nextHostAccountChoices } from "./SourceControlAccountSettings.logic";
13import {
14 readHostCredentialSettings,
15 type SourceControlDefinition,
16 type SourceControlHostCredentials,
17} from "@t3tools/client-runtime/source-control-clients";
18import { redactedPlaceholder } from "./RedactedSensitiveText";
19​
20/** Sentinel select value for "follow the CLI's active login"; logins never contain spaces. */
21const ACTIVE_ACCOUNT = "active cli account";
22​
23function RedactedLogin(props: {
24 readonly account: string;
25 readonly revealed: boolean;
26 readonly label?: string;
27 readonly onToggle?: () => void;
28}) {
29 const gt = useGT();
30 const content = props.revealed ? (
31 <span className="min-w-0 truncate font-mono text-2xs">{props.account}</span>
32 ) : (
33 <span className="min-w-0 truncate font-mono text-2xs">
34 <span className="select-none blur-xs" aria-hidden>
35 {redactedPlaceholder(props.account)}
36 </span>
37 <span className="sr-only">{props.label ?? gt("Hidden account")}</span>
38 </span>
39 );
40 return props.onToggle ? (
41 <button
42 type="button"
43 className="min-w-0 cursor-pointer rounded-sm text-left outline-none hover:text-foreground focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
44 aria-label={
45 props.revealed
46 ? gt("Toggle source control account visibility: {account}", { account: props.account })
47 : gt("Toggle source control account visibility")
48 }
49 aria-pressed={props.revealed}
50 onClick={props.onToggle}
51 >
52 {content}
53 </button>
54 ) : (
55 content
56 );
58​
59export function SourceControlAccountSettings({
60 environmentId,
61 definition,
62 credentials,
63 auth,
64 onSaved,
65}: {
66 readonly environmentId: EnvironmentId;
67 readonly definition: SourceControlDefinition;
68 readonly credentials: SourceControlHostCredentials;
69 readonly auth: SourceControlProviderAuth;
70 readonly onSaved: () => void;
71}) {
72 const gt = useGT();
73 const { hosts } = useEnvironmentSettings(environmentId, (settings) =>
74 readHostCredentialSettings(settings, definition.kind),
75 );
76 const { label } = definition;
77 const { cliName } = credentials;
78 const updateSettings = useAtomCommand(serverEnvironment.updateSettings, {
79 label: `save ${label} account settings`,
80 });
81 const [saving, setSaving] = useState(false);
82 const [revealed, setRevealed] = useState(false);
83 const groups = groupHostAccounts(auth.accounts ?? []);
84​
85 const save = async (
86 host: string,
87 change: { readonly enabled?: boolean; readonly account?: string | null },
88 ) => {
89 setSaving(true);
90 const result = await updateSettings({
91 environmentId,
92 input: {
93 patch: {
94 sourceControlHosts: {
95 [definition.kind]: { hosts: nextHostAccountChoices(hosts, host, change) },
96 },
97 },
98 },
99 });
100 setSaving(false);
101 if (result._tag === "Success") onSaved();
102 };
103​
104 if (groups.length === 0) {
105 return (
106 <p className="text-xs text-muted-foreground">
107 <T>
108 Sign in with{" "}
109 <code className="rounded bg-muted px-1 py-px text-2xs">
110 <Var>{credentials.loginCommand}</Var>
111 </code>{" "}
112 on the server host, then rescan to choose accounts here.
113 </T>
114 </p>
115 );
116 }
117​
118 return (
119 <div className="grid gap-4">
120 <div className="flex items-center justify-between gap-3">
121 <p className="max-w-2xl text-xs leading-relaxed text-muted-foreground">
122 <T>
123 Choose which{" "}
124 <code className="rounded bg-muted px-1 py-px text-2xs">
125 <Var>{cliName}</Var>
126 </code>{" "}
127 login each <Var>{label}</Var> host uses, or turn a host off.
128 </T>
129 </p>
130 <Button
131 size="icon-xs"
132 variant="ghost-muted"
133 onClick={() => setRevealed((current) => !current)}
134 aria-label={
135 revealed
136 ? gt("Hide {label} accounts", { label })
137 : gt("Reveal {label} accounts", { label })
138 }
139 aria-pressed={revealed}
140 >
141 {revealed ? <EyeOffIcon /> : <EyeIcon />}
142 </Button>
143 </div>
144 {groups.map((group) => {
145 const choice = hosts[group.host];
146 const enabled = choice?.enabled ?? true;
147 const stalePin =
148 choice?.account !== undefined && !group.selectable.includes(choice.account);
149 const pinned = choice?.account !== undefined && !stalePin ? choice.account : ACTIVE_ACCOUNT;
150 return (
151 <div key={group.host} className="grid gap-2">
152 <div className="flex items-center justify-between gap-3">
153 <div className="min-w-0 space-y-0.5">
154 <span className="text-xs font-medium text-foreground">{group.host}</span>
155 {group.selectable.length === 1 && group.selectable[0] !== undefined ? (
156 <p className="flex min-w-0 items-center gap-1 text-xs text-muted-foreground">
157 <span>
158 <T>Signed in as</T>
159 </span>
160 <RedactedLogin
161 revealed={revealed}
162 account={group.selectable[0]}
163 onToggle={() => setRevealed((current) => !current)}
164 />
165 </p>
166 ) : null}
167 </div>
168 <Switch
169 checked={enabled}
170 disabled={saving}
171 aria-label={gt("Use {label} on {host}", { label, host: group.host })}
172 onCheckedChange={(checked) => void save(group.host, { enabled: checked })}
173 />
174 </div>
175 {group.selectable.length > 1 ? (
176 <div className="flex items-center justify-between gap-3">
177 <span className="text-xs text-muted-foreground">
178 <T>Account</T>
179 </span>
180 <div className="w-64 max-w-full">
181 <Select
182 value={pinned}
183 disabled={saving || !enabled}
184 onValueChange={(value) => {
185 if (typeof value !== "string") return;
186 void save(group.host, {
187 account: value === ACTIVE_ACCOUNT ? null : value,
188 });
189 }}
190 >
191 <SelectTrigger
192 size="sm"
193 aria-label={gt("{label} account for {host}", { label, host: group.host })}
194 >
195 <SelectValue>
196 {(value: string) =>
197 value === ACTIVE_ACCOUNT ? (
198 <span className="flex min-w-0 items-center gap-1">
199 {group.activeAccount ? (
200 <T>
201 Active <Var>{cliName}</Var> account (
202 <RedactedLogin
203 revealed={revealed}
204 account={group.activeAccount}
205 />
206 )
207 </T>
208 ) : (
209 <T>
210 Active <Var>{cliName}</Var> account
211 </T>
212 )}
213 </span>
214 ) : (
215 <RedactedLogin revealed={revealed} account={value} />
216 )
217 }
218 </SelectValue>
219 </SelectTrigger>
220 <SelectPopup align="end" alignItemWithTrigger={false}>
221 <SelectItem value={ACTIVE_ACCOUNT}>
222 <span className="flex min-w-0 items-center gap-1">
223 {group.activeAccount ? (
224 <T>
225 Active <Var>{cliName}</Var> account (
226 <RedactedLogin revealed={revealed} account={group.activeAccount} />)
227 </T>
228 ) : (
229 <T>
230 Active <Var>{cliName}</Var> account
231 </T>
232 )}
233 </span>
234 </SelectItem>
235 {group.selectable.map((account, index) => (
236 <SelectItem key={account} value={account}>
237 <RedactedLogin
238 revealed={revealed}
239 account={account}
240 label={gt("Account {number}", { number: index + 1 })}
241 />
242 </SelectItem>
243 ))}
244 </SelectPopup>
245 </Select>
246 </div>
247 </div>
248 ) : null}
249 {stalePin ? (
250 <div className="flex items-center justify-between gap-3">
251 <p className="text-xs text-warning">
252 <T>
253 The chosen login is no longer signed in, so the active <Var>{cliName}</Var>{" "}
254 login is used.
255 </T>
256 </p>
257 <Button
258 size="xs"
259 variant="outline"
260 disabled={saving}
261 onClick={() => void save(group.host, { account: null })}
262 >
263 <T>Use active login</T>
264 </Button>
265 </div>
266 ) : null}
267 {group.broken.map((entry) => (
268 <p
269 key={entry.account}
270 className="flex min-w-0 flex-wrap items-center gap-1 text-xs text-muted-foreground/70"
271 >
272 {entry.error ? (
273 <T>
274 <RedactedLogin
275 revealed={revealed}
276 account={entry.account}
277 onToggle={() => setRevealed((current) => !current)}
278 />{" "}
279 <span>
280 can&apos;t be used: <Var>{entry.error}</Var>
281 </span>
282 </T>
283 ) : (
284 <T>
285 <RedactedLogin
286 revealed={revealed}
287 account={entry.account}
288 onToggle={() => setRevealed((current) => !current)}
289 />{" "}
290 <span>
291 can&apos;t be used: <Var>{cliName}</Var> reports this login as invalid.
292 </span>
293 </T>
294 )}
295 </p>
296 ))}
297 {group.environmentVariable ? (
298 <p className="text-xs text-warning">
299 <T>
300 <Var>{group.environmentVariable}</Var> is set on the server, so it overrides the
301 account chosen here until it is unset.
302 </T>
303 </p>
304 ) : null}
305 </div>
306 );
307 })}
308 </div>
309 );