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

SecretsSettings.tsx

285 lines9.9 KB

1import type { EnvironmentId, SecretProviderConfig, SecretProviderId } from "@t3tools/contracts";
2import { PlusIcon } from "lucide-react";
3import { useState } from "react";
4import { T, useGT, useMessages, Var } from "gt-react";
5​
6import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
7import type { EnvironmentPresentation } from "../../state/environments";
8import { useEnvironmentSessionState } from "../../state/session";
9import { OnePasswordIcon, type Icon } from "../Icons";
10import {
11 AlertDialog,
12 AlertDialogClose,
13 AlertDialogDescription,
14 AlertDialogFooter,
15 AlertDialogHeader,
16 AlertDialogPopup,
17 AlertDialogTitle,
18} from "../ui/alert-dialog";
19import { Button } from "../ui/button";
20import { SecretProviderDialog } from "./SecretProviderDialog";
21import { classifySecretProvider } from "./SecretProviderDialog.logic";
22import { secretProviderUses } from "./SecretsSettings.logic";
23import {
24 classifyProviderEnvironmentAccess,
25 resolveRemoteOperateAccess,
26} from "./ProviderSettingsPanel.logic";
27import { useSettingsScope } from "./SettingsScopeContext";
28import { RedactedSensitiveText } from "./RedactedSensitiveText";
29import { useSearchableSetting } from "./settingsSearch";
30import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
31​
32const SECRET_PROVIDER_KINDS: Readonly<
33 Record<string, { readonly name: string; readonly icon: Icon }>
34> = {
35 one_password: { name: "1Password", icon: OnePasswordIcon },
36};
37​
38export function SecretsSettingsPanel() {
39 const setting = useSearchableSetting();
40 const { scope, environment } = useSettingsScope();
41 const connected = environment?.connection.phase === "connected";
42 return (
43 <SettingsPageContainer>
44 {connected &&
45 environment.serverConfig !== null &&
46 environment.serverConfig.secretProviderSettings !== true ? (
47 <SettingsSection {...setting("secret-providers")}>
48 <p className="px-4 py-3 text-sm text-muted-foreground">
49 <T>
50 Update T3 Code on <Var>{environment.label}</Var> to manage its secret providers.
51 </T>
52 </p>
53 </SettingsSection>
54 ) : connected ? (
55 <EnvironmentSecretProviders
56 key={environment.environmentId}
57 environment={environment}
58 projectScoped={scope.kind === "project" || scope.kind === "checkout"}
59 />
60 ) : (
61 <SettingsSection {...setting("secret-providers")}>
62 <p className="px-4 py-3 text-sm text-muted-foreground">
63 <T>
64 Connect an environment to manage the secret providers its provider variables read
65 from.
66 </T>
67 </p>
68 </SettingsSection>
69 )}
70 </SettingsPageContainer>
71 );
73​
74function EnvironmentSecretProviders({
75 environment,
76 projectScoped,
77}: {
78 readonly environment: EnvironmentPresentation;
79 readonly projectScoped: boolean;
80}) {
81 const setting = useSearchableSetting();
82 const gt = useGT();
83 const environmentId = environment.environmentId;
84 const sessionState = useEnvironmentSessionState(environmentId);
85 // The server requires the same grant for secret providers as for provider settings.
86 const access = classifyProviderEnvironmentAccess({
87 connectionPhase: environment.connection.phase,
88 hasServerConfig: environment.serverConfig !== null,
89 operateAccess: resolveRemoteOperateAccess({
90 session: sessionState.data,
91 isPending: sessionState.isPending,
92 hasError: sessionState.hasError,
93 }),
94 });
95 const readOnly = access.kind !== "editable" || projectScoped;
96 const providers = useEnvironmentSettings(environmentId, (settings) => settings.secretProviders);
97 const updateSettings = useUpdateEnvironmentSettings(environmentId);
98 const [dialog, setDialog] = useState<
99 null | { readonly mode: "add" } | { readonly mode: "edit"; readonly id: SecretProviderId }
100 >(null);
101 const entries = Object.entries(providers) as Array<[SecretProviderId, SecretProviderConfig]>;
102 const editingConfig = dialog?.mode === "edit" ? providers[dialog.id] : undefined;
103 const editing =
104 dialog?.mode === "edit" && editingConfig !== undefined
105 ? { id: dialog.id, config: editingConfig }
106 : undefined;
107 return (
108 <>
109 <SettingsSection
110 {...setting("secret-providers")}
111 headerAction={
112 !readOnly ? (
113 <Button size="xs" variant="outline" onClick={() => setDialog({ mode: "add" })}>
114 <PlusIcon className="size-3" aria-hidden />
115 <T>Add provider</T>
116 </Button>
117 ) : null
118 }
119 >
120 {projectScoped ? (
121 <p className="px-4 pt-3 text-xs text-muted-foreground">
122 <T>Environment-wide setting. Select an environment to change it.</T>
123 </p>
124 ) : null}
125 {entries.length === 0 ? (
126 <SettingsRow
127 title={gt`No secret providers configured.`}
128 description={gt`Add a 1Password account, then pick it as the source of a provider variable.`}
129 />
130 ) : (
131 entries.map(([id, config]) => {
132 const classified = classifySecretProvider(config);
133 const kind =
134 classified.kind === "unsupported"
135 ? undefined
136 : SECRET_PROVIDER_KINDS[classified.kind];
137 return (
138 <SettingsRow
139 key={id}
140 title={
141 <span className="flex min-w-0 items-center gap-2">
142 {kind ? (
143 <kind.icon className="size-4.5 shrink-0 text-foreground/80" aria-hidden />
144 ) : null}
145 <span className="min-w-0 wrap-anywhere">{config.label}</span>
146 </span>
147 }
148 description={
149 classified.kind === "one_password" && kind ? (
150 <span className="flex min-w-0 items-center gap-1">
151 <span className="shrink-0">{kind.name} ·</span>
152 <RedactedSensitiveText
153 value={classified.account}
154 ariaLabel={gt`Toggle account visibility`}
155 revealTooltip={gt`Click to reveal account`}
156 hideTooltip={gt`Click to hide account`}
157 className="max-w-full truncate"
158 />
159 </span>
160 ) : (
161 gt`Unsupported provider (${{ kind: config.kind }})`
162 )
163 }
164 control={
165 !readOnly ? (
166 <div className="flex items-center gap-1">
167 {kind ? (
168 <Button
169 size="xs"
170 variant="ghost"
171 onClick={() => setDialog({ mode: "edit", id })}
172 >
173 <T>Edit</T>
174 </Button>
175 ) : null}
176 <RemoveSecretProviderButton
177 environmentId={environmentId}
178 id={id}
179 label={config.label}
180 onConfirm={() => updateSettings({ secretProviders: { [id]: null } })}
181 />
182 </div>
183 ) : null
184 }
185 />
186 );
187 })
188 )}
189 </SettingsSection>
190 {dialog !== null && !readOnly && (dialog.mode === "add" || editing !== undefined) ? (
191 <SecretProviderDialog
192 onClose={() => setDialog(null)}
193 environmentId={environmentId}
194 environmentLabel={environment.label}
195 existingIds={new Set(Object.keys(providers))}
196 {...(editing === undefined ? {} : { existing: editing })}
197 />
198 ) : null}
199 </>
200 );
202​
203function RemoveSecretProviderButton({
204 environmentId,
205 id,
206 label,
207 onConfirm,
208}: {
209 readonly environmentId: EnvironmentId;
210 readonly id: SecretProviderId;
211 readonly label: string;
212 readonly onConfirm: () => void;
213}) {
214 const [open, setOpen] = useState(false);
215 return (
216 <>
217 <Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
218 <T>Remove</T>
219 </Button>
220 <AlertDialog open={open} onOpenChange={setOpen}>
221 <AlertDialogPopup>
222 <AlertDialogHeader>
223 <AlertDialogTitle>
224 <T>
225 Remove <Var>{label}</Var>?
226 </T>
227 </AlertDialogTitle>
228 <SecretProviderUses environmentId={environmentId} id={id} label={label} />
229 </AlertDialogHeader>
230 <AlertDialogFooter>
231 <AlertDialogClose render={<Button variant="outline" />}>
232 <T>Cancel</T>
233 </AlertDialogClose>
234 <Button
235 variant="destructive"
236 onClick={() => {
237 setOpen(false);
238 onConfirm();
239 }}
240 >
241 <T>Remove provider</T>
242 </Button>
243 </AlertDialogFooter>
244 </AlertDialogPopup>
245 </AlertDialog>
246 </>
247 );
249​
250function SecretProviderUses({
251 environmentId,
252 id,
253 label,
254}: {
255 readonly environmentId: EnvironmentId;
256 readonly id: SecretProviderId;
257 readonly label: string;
258}) {
259 const messages = useMessages();
260 const uses = secretProviderUses(useEnvironmentSettings(environmentId), id, messages);
261 if (uses.length === 0) {
262 return (
263 <AlertDialogDescription>
264 <T>
265 Nothing reads from <Var>{label}</Var>. The secrets themselves are untouched.
266 </T>
267 </AlertDialogDescription>
268 );
269 }
270 return (
271 <>
272 <AlertDialogDescription>
273 <T>
274 These read from <Var>{label}</Var> and will be unset. The secrets themselves are
275 untouched.
276 </T>
277 </AlertDialogDescription>
278 <ul className="max-h-48 list-disc space-y-0.5 overflow-y-auto ps-5 text-sm wrap-anywhere">
279 {uses.map((use) => (
280 <li key={use.id}>{use.label}</li>
281 ))}
282 </ul>
283 </>
284 );