Open History to see the last commit
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: 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 );
57}
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={revealed ? gt`Hide ${{ label }} accounts` : gt`Reveal ${{ label }} accounts`}
135 aria-pressed={revealed}
136 >
137 {revealed ? <EyeOffIcon /> : <EyeIcon />}
138 </Button>
139 </div>
140 {groups.map((group) => {
141 const choice = hosts[group.host];
142 const enabled = choice?.enabled ?? true;
143 const stalePin =
144 choice?.account !== undefined && !group.selectable.includes(choice.account);
145 const pinned = choice?.account !== undefined && !stalePin ? choice.account : ACTIVE_ACCOUNT;
146 return (
147 <div key={group.host} className="grid gap-2">
148 <div className="flex items-center justify-between gap-3">
149 <div className="min-w-0 space-y-0.5">
150 <span className="text-xs font-medium text-foreground">{group.host}</span>
151 {group.selectable.length === 1 && group.selectable[0] !== undefined ? (
152 <p className="flex min-w-0 items-center gap-1 text-xs text-muted-foreground">
153 <span>
154 <T>Signed in as</T>
155 </span>
156 <RedactedLogin
157 revealed={revealed}
158 account={group.selectable[0]}
159 onToggle={() => setRevealed((current) => !current)}
160 />
161 </p>
162 ) : null}
163 </div>
164 <Switch
165 checked={enabled}
166 disabled={saving}
167 aria-label={gt`Use ${{ label }} on ${{ host: group.host }}`}
168 onCheckedChange={(checked) => void save(group.host, { enabled: checked })}
169 />
170 </div>
171 {group.selectable.length > 1 ? (
172 <div className="flex items-center justify-between gap-3">
173 <span className="text-xs text-muted-foreground">
174 <T>Account</T>
175 </span>
176 <div className="w-64 max-w-full">
177 <Select
178 value={pinned}
179 disabled={saving || !enabled}
180 onValueChange={(value) => {
181 if (typeof value !== "string") return;
182 void save(group.host, {
183 account: value === ACTIVE_ACCOUNT ? null : value,
184 });
185 }}
186 >
187 <SelectTrigger
188 size="sm"
189 aria-label={gt`${{ label }} account for ${{ host: group.host }}`}
190 >
191 <SelectValue>
192 {(value: string) =>
193 value === ACTIVE_ACCOUNT ? (
194 <span className="flex min-w-0 items-center gap-1">
195 {group.activeAccount ? (
196 <T>
197 Active <Var>{cliName}</Var> account (
198 <RedactedLogin
199 revealed={revealed}
200 account={group.activeAccount}
201 />
202 )
203 </T>
204 ) : (
205 <T>
206 Active <Var>{cliName}</Var> account
207 </T>
208 )}
209 </span>
210 ) : (
211 <RedactedLogin revealed={revealed} account={value} />
212 )
213 }
214 </SelectValue>
215 </SelectTrigger>
216 <SelectPopup align="end" alignItemWithTrigger={false}>
217 <SelectItem value={ACTIVE_ACCOUNT}>
218 <span className="flex min-w-0 items-center gap-1">
219 {group.activeAccount ? (
220 <T>
221 Active <Var>{cliName}</Var> account (
222 <RedactedLogin revealed={revealed} account={group.activeAccount} />)
223 </T>
224 ) : (
225 <T>
226 Active <Var>{cliName}</Var> account
227 </T>
228 )}
229 </span>
230 </SelectItem>
231 {group.selectable.map((account, index) => (
232 <SelectItem key={account} value={account}>
233 <RedactedLogin
234 revealed={revealed}
235 account={account}
236 label={gt`Account ${{ number: index + 1 }}`}
237 />
238 </SelectItem>
239 ))}
240 </SelectPopup>
241 </Select>
242 </div>
243 </div>
244 ) : null}
245 {stalePin ? (
246 <div className="flex items-center justify-between gap-3">
247 <p className="text-xs text-warning">
248 <T>
249 The chosen login is no longer signed in, so the active <Var>{cliName}</Var>{" "}
250 login is used.
251 </T>
252 </p>
253 <Button
254 size="xs"
255 variant="outline"
256 disabled={saving}
257 onClick={() => void save(group.host, { account: null })}
258 >
259 <T>Use active login</T>
260 </Button>
261 </div>
262 ) : null}
263 {group.broken.map((entry) => (
264 <p
265 key={entry.account}
266 className="flex min-w-0 flex-wrap items-center gap-1 text-xs text-muted-foreground/70"
267 >
268 {entry.error ? (
269 <T>
270 <RedactedLogin
271 revealed={revealed}
272 account={entry.account}
273 onToggle={() => setRevealed((current) => !current)}
274 />{" "}
275 <span>
276 can't be used: <Var>{entry.error}</Var>
277 </span>
278 </T>
279 ) : (
280 <T>
281 <RedactedLogin
282 revealed={revealed}
283 account={entry.account}
284 onToggle={() => setRevealed((current) => !current)}
285 />{" "}
286 <span>
287 can't be used: <Var>{cliName}</Var> reports this login as invalid.
288 </span>
289 </T>
290 )}
291 </p>
292 ))}
293 {group.environmentVariable ? (
294 <p className="text-xs text-warning">
295 <T>
296 <Var>{group.environmentVariable}</Var> is set on the server, so it overrides the
297 account chosen here until it is unset.
298 </T>
299 </p>
300 ) : null}
301 </div>
302 );
303 })}
304 </div>
305 );
306}