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