The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId, SecretProviderConfig, SecretProviderId } from "@t3tools/contracts"; |
| 2 | import * as Result from "effect/Result"; |
| 3 | import { useState } from "react"; |
| 4 | import { T, useGT, useMessages, Var } from "gt-react"; |
| 5 | |
| 6 | import { useUpdateEnvironmentSettings } from "../../hooks/useSettings"; |
| 7 | import { Button } from "../ui/button"; |
| 8 | import { |
| 9 | Dialog, |
| 10 | DialogDescription, |
| 11 | DialogFooter, |
| 12 | DialogHeader, |
| 13 | DialogPanel, |
| 14 | DialogPopup, |
| 15 | DialogTitle, |
| 16 | } from "../ui/dialog"; |
| 17 | import { Input } from "../ui/input"; |
| 18 | import { Label } from "../ui/label"; |
| 19 | import { |
| 20 | draftFromSecretProvider, |
| 21 | secretProviderFromDraft, |
| 22 | type SecretProviderDraft, |
| 23 | } from "./SecretProviderDialog.logic"; |
| 24 | |
| 25 | /** Mounted per open, so each open starts from a fresh draft. */ |
| 26 | export 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 |
| 75 | ? gt("Edit {label}", { label: existing.config.label }) |
| 76 | : gt("Add a 1Password provider")} |
| 77 | </DialogTitle> |
| 78 | <DialogDescription> |
| 79 | <T> |
| 80 | Provider variables on <Var>{environmentLabel}</Var> can read values from it by name. |
| 81 | Values are read with the op CLI on that server when a provider starts. |
| 82 | </T> |
| 83 | </DialogDescription> |
| 84 | </DialogHeader> |
| 85 | <DialogPanel> |
| 86 | <form |
| 87 | id="secret-provider-form" |
| 88 | className="grid gap-4" |
| 89 | onSubmit={(event) => { |
| 90 | event.preventDefault(); |
| 91 | save(); |
| 92 | }} |
| 93 | > |
| 94 | <div className="grid gap-1.5"> |
| 95 | <Label htmlFor="secret-provider-name"> |
| 96 | <T>Name</T> |
| 97 | </Label> |
| 98 | <Input |
| 99 | id="secret-provider-name" |
| 100 | placeholder={gt("Personal")} |
| 101 | value={draft.name} |
| 102 | onChange={(event) => update({ name: event.target.value })} |
| 103 | maxLength={64} |
| 104 | aria-invalid={nameError !== undefined} |
| 105 | aria-describedby={ |
| 106 | nameError !== undefined ? "secret-provider-name-error" : undefined |
| 107 | } |
| 108 | autoFocus |
| 109 | /> |
| 110 | {nameError !== undefined ? ( |
| 111 | <p |
| 112 | id="secret-provider-name-error" |
| 113 | role="alert" |
| 114 | className="text-xs text-destructive" |
| 115 | > |
| 116 | {nameError} |
| 117 | </p> |
| 118 | ) : null} |
| 119 | </div> |
| 120 | <div className="grid gap-1.5"> |
| 121 | <Label htmlFor="secret-provider-account"> |
| 122 | <T>Account</T> |
| 123 | </Label> |
| 124 | <Input |
| 125 | id="secret-provider-account" |
| 126 | placeholder="my.1password.com" |
| 127 | value={draft.account} |
| 128 | onChange={(event) => update({ account: event.target.value })} |
| 129 | maxLength={253} |
| 130 | spellCheck={false} |
| 131 | aria-invalid={accountError !== undefined} |
| 132 | aria-describedby={ |
| 133 | accountError !== undefined ? "secret-provider-account-error" : undefined |
| 134 | } |
| 135 | /> |
| 136 | {accountError !== undefined ? ( |
| 137 | <p |
| 138 | id="secret-provider-account-error" |
| 139 | role="alert" |
| 140 | className="text-xs text-destructive" |
| 141 | > |
| 142 | {accountError} |
| 143 | </p> |
| 144 | ) : null} |
| 145 | <p className="text-xs text-muted-foreground"> |
| 146 | <T> |
| 147 | The account shorthand, sign-in address without https://, or ID that op account |
| 148 | list shows. |
| 149 | </T> |
| 150 | </p> |
| 151 | </div> |
| 152 | </form> |
| 153 | </DialogPanel> |
| 154 | <DialogFooter variant="bare"> |
| 155 | <Button variant="outline" onClick={onClose}> |
| 156 | <T>Cancel</T> |
| 157 | </Button> |
| 158 | <Button |
| 159 | type="submit" |
| 160 | form="secret-provider-form" |
| 161 | disabled={submitted && Result.isFailure(result)} |
| 162 | > |
| 163 | {existing ? gt("Save changes") : gt("Add provider")} |
| 164 | </Button> |
| 165 | </DialogFooter> |
| 166 | </DialogPopup> |
| 167 | </Dialog> |
| 168 | ); |
| 169 | } |