Open History to see the last commit
1import type { EnvironmentId, SecretProviderConfig, SecretProviderId } from "@t3tools/contracts";
2import * as Result from "effect/Result";
3import { useState } from "react";
4import { T, useGT, useMessages, Var } from "gt-react";
5
6import { useUpdateEnvironmentSettings } from "../../hooks/useSettings";
7import { Button } from "../ui/button";
8import {
9 Dialog,
10 DialogDescription,
11 DialogFooter,
12 DialogHeader,
13 DialogPanel,
14 DialogPopup,
15 DialogTitle,
16} from "../ui/dialog";
17import { Input } from "../ui/input";
18import { Label } from "../ui/label";
19import {
20 draftFromSecretProvider,
21 secretProviderFromDraft,
22 type SecretProviderDraft,
23} from "./SecretProviderDialog.logic";
24
25/** Mounted per open, so each open starts from a fresh draft. */
26export function SecretProviderDialog({
27 onClose,
28 environmentId,
29 environmentLabel,
30 existingIds,
31 existing,
32}: {
33 readonly onClose: () => void;
34 readonly environmentId: EnvironmentId;
35 readonly environmentLabel: string;
36 readonly existingIds: ReadonlySet<string>;
37 readonly existing?: { readonly id: SecretProviderId; readonly config: SecretProviderConfig };
38}) {
39 const messages = useMessages();
40 const gt = useGT();
41 const updateSettings = useUpdateEnvironmentSettings(environmentId);
42 const [draft, setDraft] = useState<SecretProviderDraft>(
43 existing ? draftFromSecretProvider(existing.config) : { name: "", account: "" },
44 );
45 const [submitted, setSubmitted] = useState(false);
46 const result = secretProviderFromDraft(
47 draft,
48 existingIds,
49 existing ? { mode: "edit", id: existing.id } : { mode: "add" },
50 messages,
51 );
52 const issues = Result.isFailure(result) ? result.failure : {};
53 const nameError = submitted ? issues.name : undefined;
54 const accountError = submitted ? issues.account : undefined;
55
56 const update = (patch: Partial<SecretProviderDraft>) => setDraft({ ...draft, ...patch });
57 const save = () => {
58 setSubmitted(true);
59 if (Result.isFailure(result)) return;
60 updateSettings({ secretProviders: { [result.success.id]: result.success.config } });
61 onClose();
62 };
63
64 return (
65 <Dialog
66 open
67 onOpenChange={(next) => {
68 if (!next) onClose();
69 }}
70 >
71 <DialogPopup className="max-w-md">
72 <DialogHeader>
73 <DialogTitle>
74 {existing ? gt`Edit ${{ label: existing.config.label }}` : gt`Add a 1Password provider`}
75 </DialogTitle>
76 <DialogDescription>
77 <T>
78 Provider variables on <Var>{environmentLabel}</Var> can read values from it by name.
79 Values are read with the op CLI on that server when a provider starts.
80 </T>
81 </DialogDescription>
82 </DialogHeader>
83 <DialogPanel>
84 <form
85 id="secret-provider-form"
86 className="grid gap-4"
87 onSubmit={(event) => {
88 event.preventDefault();
89 save();
90 }}
91 >
92 <div className="grid gap-1.5">
93 <Label htmlFor="secret-provider-name">
94 <T>Name</T>
95 </Label>
96 <Input
97 id="secret-provider-name"
98 placeholder={gt`Personal`}
99 value={draft.name}
100 onChange={(event) => update({ name: event.target.value })}
101 maxLength={64}
102 aria-invalid={nameError !== undefined}
103 aria-describedby={
104 nameError !== undefined ? "secret-provider-name-error" : undefined
105 }
106 autoFocus
107 />
108 {nameError !== undefined ? (
109 <p
110 id="secret-provider-name-error"
111 role="alert"
112 className="text-xs text-destructive"
113 >
114 {nameError}
115 </p>
116 ) : null}
117 </div>
118 <div className="grid gap-1.5">
119 <Label htmlFor="secret-provider-account">
120 <T>Account</T>
121 </Label>
122 <Input
123 id="secret-provider-account"
124 placeholder="my.1password.com"
125 value={draft.account}
126 onChange={(event) => update({ account: event.target.value })}
127 maxLength={253}
128 spellCheck={false}
129 aria-invalid={accountError !== undefined}
130 aria-describedby={
131 accountError !== undefined ? "secret-provider-account-error" : undefined
132 }
133 />
134 {accountError !== undefined ? (
135 <p
136 id="secret-provider-account-error"
137 role="alert"
138 className="text-xs text-destructive"
139 >
140 {accountError}
141 </p>
142 ) : null}
143 <p className="text-xs text-muted-foreground">
144 <T>
145 The account shorthand, sign-in address without https://, or ID that op account
146 list shows.
147 </T>
148 </p>
149 </div>
150 </form>
151 </DialogPanel>
152 <DialogFooter variant="bare">
153 <Button variant="outline" onClick={onClose}>
154 <T>Cancel</T>
155 </Button>
156 <Button
157 type="submit"
158 form="secret-provider-form"
159 disabled={submitted && Result.isFailure(result)}
160 >
161 {existing ? gt`Save changes` : gt`Add provider`}
162 </Button>
163 </DialogFooter>
164 </DialogPopup>
165 </Dialog>
166 );
167}