@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 287 lines 9.91 KB
Raw
1 import type { EnvironmentId, SecretProviderConfig, SecretProviderId } from "@t3tools/contracts";
2 import { PlusIcon } from "lucide-react";
3 import { useState } from "react";
4 import { T, useGT, useMessages, Var } from "gt-react";
5
6 import { useEnvironmentSettings, useUpdateEnvironmentSettings } from "../../hooks/useSettings";
7 import type { EnvironmentPresentation } from "../../state/environments";
8 import { useEnvironmentSessionState } from "../../state/session";
9 import { OnePasswordIcon, type Icon } from "../Icons";
10 import {
11 AlertDialog,
12 AlertDialogClose,
13 AlertDialogDescription,
14 AlertDialogFooter,
15 AlertDialogHeader,
16 AlertDialogPopup,
17 AlertDialogTitle,
18 } from "../ui/alert-dialog";
19 import { Button } from "../ui/button";
20 import { SecretProviderDialog } from "./SecretProviderDialog";
21 import { classifySecretProvider } from "./SecretProviderDialog.logic";
22 import { secretProviderUses } from "./SecretsSettings.logic";
23 import {
24 classifyProviderEnvironmentAccess,
25 resolveRemoteOperateAccess,
26 } from "./ProviderSettingsPanel.logic";
27 import { useSettingsScope } from "./SettingsScopeContext";
28 import { RedactedSensitiveText } from "./RedactedSensitiveText";
29 import { useSearchableSetting } from "./settingsSearch";
30 import { SettingsPageContainer, SettingsRow, SettingsSection } from "./settingsLayout";
31
32 const SECRET_PROVIDER_KINDS: Readonly<
33 Record<string, { readonly name: string; readonly icon: Icon }>
34 > = {
35 one_password: { name: "1Password", icon: OnePasswordIcon },
36 };
37
38 export 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 );
72 }
73
74 function 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(
129 "Add a 1Password account, then pick it as the source of a provider variable.",
130 )}
131 />
132 ) : (
133 entries.map(([id, config]) => {
134 const classified = classifySecretProvider(config);
135 const kind =
136 classified.kind === "unsupported"
137 ? undefined
138 : SECRET_PROVIDER_KINDS[classified.kind];
139 return (
140 <SettingsRow
141 key={id}
142 title={
143 <span className="flex min-w-0 items-center gap-2">
144 {kind ? (
145 <kind.icon className="size-4.5 shrink-0 text-foreground/80" aria-hidden />
146 ) : null}
147 <span className="min-w-0 wrap-anywhere">{config.label}</span>
148 </span>
149 }
150 description={
151 classified.kind === "one_password" && kind ? (
152 <span className="flex min-w-0 items-center gap-1">
153 <span className="shrink-0">{kind.name} ·</span>
154 <RedactedSensitiveText
155 value={classified.account}
156 ariaLabel={gt("Toggle account visibility")}
157 revealTooltip={gt("Click to reveal account")}
158 hideTooltip={gt("Click to hide account")}
159 className="max-w-full truncate"
160 />
161 </span>
162 ) : (
163 gt("Unsupported provider ({kind})", { kind: config.kind })
164 )
165 }
166 control={
167 !readOnly ? (
168 <div className="flex items-center gap-1">
169 {kind ? (
170 <Button
171 size="xs"
172 variant="ghost"
173 onClick={() => setDialog({ mode: "edit", id })}
174 >
175 <T>Edit</T>
176 </Button>
177 ) : null}
178 <RemoveSecretProviderButton
179 environmentId={environmentId}
180 id={id}
181 label={config.label}
182 onConfirm={() => updateSettings({ secretProviders: { [id]: null } })}
183 />
184 </div>
185 ) : null
186 }
187 />
188 );
189 })
190 )}
191 </SettingsSection>
192 {dialog !== null && !readOnly && (dialog.mode === "add" || editing !== undefined) ? (
193 <SecretProviderDialog
194 onClose={() => setDialog(null)}
195 environmentId={environmentId}
196 environmentLabel={environment.label}
197 existingIds={new Set(Object.keys(providers))}
198 {...(editing === undefined ? {} : { existing: editing })}
199 />
200 ) : null}
201 </>
202 );
203 }
204
205 function RemoveSecretProviderButton({
206 environmentId,
207 id,
208 label,
209 onConfirm,
210 }: {
211 readonly environmentId: EnvironmentId;
212 readonly id: SecretProviderId;
213 readonly label: string;
214 readonly onConfirm: () => void;
215 }) {
216 const [open, setOpen] = useState(false);
217 return (
218 <>
219 <Button size="xs" variant="ghost" onClick={() => setOpen(true)}>
220 <T>Remove</T>
221 </Button>
222 <AlertDialog open={open} onOpenChange={setOpen}>
223 <AlertDialogPopup>
224 <AlertDialogHeader>
225 <AlertDialogTitle>
226 <T>
227 Remove <Var>{label}</Var>?
228 </T>
229 </AlertDialogTitle>
230 <SecretProviderUses environmentId={environmentId} id={id} label={label} />
231 </AlertDialogHeader>
232 <AlertDialogFooter>
233 <AlertDialogClose render={<Button variant="outline" />}>
234 <T>Cancel</T>
235 </AlertDialogClose>
236 <Button
237 variant="destructive"
238 onClick={() => {
239 setOpen(false);
240 onConfirm();
241 }}
242 >
243 <T>Remove provider</T>
244 </Button>
245 </AlertDialogFooter>
246 </AlertDialogPopup>
247 </AlertDialog>
248 </>
249 );
250 }
251
252 function SecretProviderUses({
253 environmentId,
254 id,
255 label,
256 }: {
257 readonly environmentId: EnvironmentId;
258 readonly id: SecretProviderId;
259 readonly label: string;
260 }) {
261 const messages = useMessages();
262 const uses = secretProviderUses(useEnvironmentSettings(environmentId), id, messages);
263 if (uses.length === 0) {
264 return (
265 <AlertDialogDescription>
266 <T>
267 Nothing reads from <Var>{label}</Var>. The secrets themselves are untouched.
268 </T>
269 </AlertDialogDescription>
270 );
271 }
272 return (
273 <>
274 <AlertDialogDescription>
275 <T>
276 These read from <Var>{label}</Var> and will be unset. The secrets themselves are
277 untouched.
278 </T>
279 </AlertDialogDescription>
280 <ul className="max-h-48 list-disc space-y-0.5 overflow-y-auto ps-5 text-sm wrap-anywhere">
281 {uses.map((use) => (
282 <li key={use.id}>{use.label}</li>
283 ))}
284 </ul>
285 </>
286 );
287 }