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

SecretsSettings.tsx

229 lines8.4 KB

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