* feat(ui): enforce unique secret ids client-side Keep secret ID uniqueness enforcement in the web UI only and preserve the existing upsert behavior in the core secrets API. Prefill source and secrets-page creation flows with the first available unique ID, prevent duplicate manual IDs in the shared React flows, and show each saved secret's ID beside its name in the secrets list with subdued monospace styling and a themed hover tooltip for the full value. Add an optional hideArrow escape hatch to the shared tooltip component so the secrets list can keep that tooltip flush with the dark UI. * Lift new-secret form into a SecretForm compound provider The dialog and the inline header-auth flow had two divergent copies of the same form: name + auto-derived id + value + scope + submit. The PR's dup-detection only landed in one of them, and state was strewn across five useStates per surface with manual reset() bookkeeping. - New plugins/secret-form.tsx with state/actions/meta context and Provider + NameField/IdField/ValueField/ProviderField/ErrorBanner/ SubmitButton parts. Submit lifecycle is a discriminated status union instead of saving + error booleans. - AddSecretDialog and InlineCreateSecret now compose the parts; layout is each surface's responsibility, logic isn't. - Remount-on-open in the dialog (key prop) replaces the manual reset. - existingSecretIds memoised in SecretsPage so the array reference is stable when the secrets list is unchanged. - Drop the SecretRow secret-id tooltip + the bespoke `hideArrow` tooltip prop; native title attribute carries the same info without a custom Radix subtree. - Drop the `useUniqueSecretIdInput` hook; pure helpers live in secret-id.tsx and the form provider owns the override state directly. Co-authored-by: Rhys Sullivan <rhys@rhyssullivan.com> * Move + New secret to top of SecretPicker dropdown Co-authored-by: Rhys Sullivan <rhys@rhyssullivan.com> ---------
Unified · Split
| @@ -1,10 +1,11 @@ | |||
| 1 | import { useState, Suspense } from "react"; | 1 | import { useMemo, useState, Suspense } from "react"; |
| 2 | import { useAtomValue, useAtomSet } from "@effect/atom-react"; | 2 | import { useAtomValue, useAtomSet } from "@effect/atom-react"; |
| 3 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; | 3 | import * as AsyncResult from "effect/unstable/reactivity/AsyncResult"; |
| 4 | import { secretsAtom, setSecret, removeSecret } from "../api/atoms"; | 4 | import { secretsAtom, removeSecret } from "../api/atoms"; |
| 5 | import { secretWriteKeys } from "../api/reactivity-keys"; | 5 | import { secretWriteKeys } from "../api/reactivity-keys"; |
| 6 | import { useSecretProviderPlugins } from "@executor-js/sdk/client"; | 6 | import { useSecretProviderPlugins } from "@executor-js/sdk/client"; |
| 7 | import { SecretId } from "@executor-js/sdk"; | 7 | import { SecretId } from "@executor-js/sdk"; |
| 8 | import { SecretForm } from "../plugins/secret-form"; | ||
| 8 | import { useScope } from "../hooks/use-scope"; | 9 | import { useScope } from "../hooks/use-scope"; |
| 9 | import { | 10 | import { |
| 10 | Dialog, | 11 | Dialog, |
| @@ -16,21 +17,12 @@ import { · 5 unmodified lines | |||
| 16 | DialogClose, | 17 | DialogClose, |
| 17 | } from "../components/dialog"; | 18 | } from "../components/dialog"; |
| 18 | import { Button } from "../components/button"; | 19 | import { Button } from "../components/button"; |
| 19 | import { Input } from "../components/input"; | ||
| 20 | import { Label } from "../components/label"; | ||
| 21 | import { | 20 | import { |
| 22 | DropdownMenu, | 21 | DropdownMenu, |
| 23 | DropdownMenuContent, | 22 | DropdownMenuContent, |
| 24 | DropdownMenuItem, | 23 | DropdownMenuItem, |
| 25 | DropdownMenuTrigger, | 24 | DropdownMenuTrigger, |
| 26 | } from "../components/dropdown-menu"; | 25 | } from "../components/dropdown-menu"; |
| 27 | import { | ||
| 28 | Select, | ||
| 29 | SelectContent, | ||
| 30 | SelectItem, | ||
| 31 | SelectTrigger, | ||
| 32 | SelectValue, | ||
| 33 | } from "../components/select"; | ||
| 34 | import { | 26 | import { |
| 35 | CardStack, | 27 | CardStack, |
| 36 | CardStackContent, | 28 | CardStackContent, |
| @@ -56,6 +48,11 @@ const defaultStorageOptions: readonly SecretStorageOption[] = [ · 19 unmodified lines | |||
| 56 | 48 | ||
| 57 | // --------------------------------------------------------------------------- | 49 | // --------------------------------------------------------------------------- |
| 58 | // Add secret dialog | 50 | // Add secret dialog |
| 51 | // | ||
| 52 | // Form state, derived id, dup detection, and submit lifecycle live in | ||
| 53 | // `<SecretForm.Provider>` and are shared with the inline create flow in | ||
| 54 | // secret-header-auth.tsx. Dialog content remounts on each open via `key` so | ||
| 55 | // state always starts fresh — no manual reset. | ||
| 59 | // --------------------------------------------------------------------------- | 56 | // --------------------------------------------------------------------------- |
| 60 | 57 | ||
| 61 | function AddSecretDialog(props: { | 58 | function AddSecretDialog(props: { |
| @@ -63,57 +60,36 @@ function AddSecretDialog(props: { · 1 unmodified line | |||
| 63 | onOpenChange: (v: boolean) => void; | 60 | onOpenChange: (v: boolean) => void; |
| 64 | description: string; | 61 | description: string; |
| 65 | storageOptions: readonly SecretStorageOption[]; | 62 | storageOptions: readonly SecretStorageOption[]; |
| 63 | existingSecretIds: readonly string[]; | ||
| 66 | }) { | 64 | }) { |
| 67 | const initialProvider = props.storageOptions[0]?.value ?? "auto"; | 65 | return ( |
| 68 | const [id, setId] = useState(""); | 66 | <Dialog open={props.open} onOpenChange={props.onOpenChange}> |
| 69 | const [name, setName] = useState(""); | 67 | {props.open && ( |
| 70 | const [value, setValue] = useState(""); | 68 | <AddSecretDialogContent |
| 71 | const [provider, setProvider] = useState(initialProvider); | 69 | key="open" |
| 72 | const [saving, setSaving] = useState(false); | 70 | description={props.description} |
| 73 | const [error, setError] = useState<string | null>(null); | 71 | storageOptions={props.storageOptions} |
| 74 | 72 | existingSecretIds={props.existingSecretIds} | |
| 75 | const scopeId = useScope(); | 73 | onClose={() => props.onOpenChange(false)} |
| 76 | const doSet = useAtomSet(setSecret, { mode: "promise" }); | 74 | /> |
| 77 | 75 | )} | |
| 78 | const reset = () => { | 76 | </Dialog> |
| 79 | setId(""); | 77 | ); |
| 80 | setName(""); | 78 | } |
| 81 | setValue(""); | ||
| 82 | setProvider(initialProvider); | ||
| 83 | setError(null); | ||
| 84 | setSaving(false); | ||
| 85 | }; | ||
| 86 | 79 | ||
| 87 | const handleSave = async () => { | 80 | function AddSecretDialogContent(props: { |
| 88 | if (!id.trim() || !name.trim() || !value.trim()) return; | 81 | description: string; |
| 89 | setSaving(true); | 82 | storageOptions: readonly SecretStorageOption[]; |
| 90 | setError(null); | 83 | existingSecretIds: readonly string[]; |
| 91 | try { | 84 | onClose: () => void; |
| 92 | await doSet({ | 85 | }) { |
| 93 | params: { scopeId }, | 86 | const initialProvider = props.storageOptions[0]?.value ?? "auto"; |
| 94 | payload: { | ||
| 95 | id: SecretId.make(id.trim()), | ||
| 96 | name: name.trim(), | ||
| 97 | value: value.trim(), | ||
| 98 | provider: provider === "auto" ? undefined : provider, | ||
| 99 | }, | ||
| 100 | reactivityKeys: secretWriteKeys, | ||
| 101 | }); | ||
| 102 | reset(); | ||
| 103 | props.onOpenChange(false); | ||
| 104 | } catch (e) { | ||
| 105 | setError(e instanceof Error ? e.message : "Failed to save secret"); | ||
| 106 | setSaving(false); | ||
| 107 | } | ||
| 108 | }; | ||
| 109 | 87 | ||
| 110 | return ( | 88 | return ( |
| 111 | <Dialog | 89 | <SecretForm.Provider |
| 112 | open={props.open} | 90 | existingSecretIds={props.existingSecretIds} |
| 113 | onOpenChange={(v) => { | 91 | initialProvider={initialProvider} |
| 114 | if (!v) reset(); | 92 | onCreated={props.onClose} |
| 115 | props.onOpenChange(v); | ||
| 116 | }} | ||
| 117 | > | 93 | > |
| 118 | <DialogContent className="sm:max-w-[440px]"> | 94 | <DialogContent className="sm:max-w-[440px]"> |
| 119 | <DialogHeader> | 95 | <DialogHeader> |
| @@ -125,85 +101,12 @@ function AddSecretDialog(props: { · 5 unmodified lines | |||
| 125 | 101 | ||
| 126 | <div className="grid gap-5 py-3"> | 102 | <div className="grid gap-5 py-3"> |
| 127 | <div className="grid grid-cols-2 gap-3"> | 103 | <div className="grid grid-cols-2 gap-3"> |
| 128 | <div className="grid gap-1.5"> | 104 | <SecretForm.NameField /> |
| 129 | <Label | 105 | <SecretForm.IdField /> |
| 130 | htmlFor="secret-id" | ||
| 131 | className="text-sm font-medium uppercase tracking-wider text-muted-foreground" | ||
| 132 | > | ||
| 133 | ID | ||
| 134 | </Label> | ||
| 135 | <Input | ||
| 136 | id="secret-id" | ||
| 137 | placeholder="github-token" | ||
| 138 | value={id} | ||
| 139 | onChange={(e) => setId((e.target as HTMLInputElement).value)} | ||
| 140 | className="font-mono text-xs h-9" | ||
| 141 | /> | ||
| 142 | </div> | ||
| 143 | <div className="grid gap-1.5"> | ||
| 144 | <Label | ||
| 145 | htmlFor="secret-name" | ||
| 146 | className="text-sm font-medium uppercase tracking-wider text-muted-foreground" | ||
| 147 | > | ||
| 148 | Name | ||
| 149 | </Label> | ||
| 150 | <Input | ||
| 151 | id="secret-name" | ||
| 152 | placeholder="GitHub PAT" | ||
| 153 | value={name} | ||
| 154 | onChange={(e) => setName((e.target as HTMLInputElement).value)} | ||
| 155 | className="text-sm h-9" | ||
| 156 | /> | ||
| 157 | </div> | ||
| 158 | </div> | 106 | </div> |
| 159 | 107 | <SecretForm.ValueField /> | |
| 160 | <div className="grid gap-1.5"> | 108 | <SecretForm.ProviderField options={props.storageOptions} /> |
| 161 | <Label | 109 | <SecretForm.ErrorBanner /> |
| 162 | htmlFor="secret-value" | ||
| 163 | className="text-sm font-medium uppercase tracking-wider text-muted-foreground" | ||
| 164 | > | ||
| 165 | Value | ||
| 166 | </Label> | ||
| 167 | <Input | ||
| 168 | id="secret-value" | ||
| 169 | type="password" | ||
| 170 | placeholder="ghp_xxxxxxxxxxxxxxxxxxxx" | ||
| 171 | value={value} | ||
| 172 | onChange={(e) => setValue((e.target as HTMLInputElement).value)} | ||
| 173 | className="font-mono text-xs h-9" | ||
| 174 | /> | ||
| 175 | </div> | ||
| 176 | |||
| 177 | <div className="grid gap-3"> | ||
| 178 | {props.storageOptions.length > 1 && ( | ||
| 179 | <div className="grid gap-1.5"> | ||
| 180 | <Label | ||
| 181 | htmlFor="secret-provider" | ||
| 182 | className="text-sm font-medium uppercase tracking-wider text-muted-foreground" | ||
| 183 | > | ||
| 184 | Storage | ||
| 185 | </Label> | ||
| 186 | <Select value={provider} onValueChange={setProvider}> | ||
| 187 | <SelectTrigger id="secret-provider" className="h-9 text-sm"> | ||
| 188 | <SelectValue /> | ||
| 189 | </SelectTrigger> | ||
| 190 | <SelectContent> | ||
| 191 | {props.storageOptions.map((option) => ( | ||
| 192 | <SelectItem key={option.value} value={option.value}> | ||
| 193 | {option.label} | ||
| 194 | </SelectItem> | ||
| 195 | ))} | ||
| 196 | </SelectContent> | ||
| 197 | </Select> | ||
| 198 | </div> | ||
| 199 | )} | ||
| 200 | </div> | ||
| 201 | |||
| 202 | {error && ( | ||
| 203 | <div className="rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2"> | ||
| 204 | <p className="text-sm text-destructive">{error}</p> | ||
| 205 | </div> | ||
| 206 | )} | ||
| 207 | </div> | 110 | </div> |
| 208 | 111 | ||
| 209 | <DialogFooter> | 112 | <DialogFooter> |
| @@ -212,16 +115,10 @@ function AddSecretDialog(props: { · 2 unmodified lines | |||
| 212 | Cancel | 115 | Cancel |
| 213 | </Button> | 116 | </Button> |
| 214 | </DialogClose> | 117 | </DialogClose> |
| 215 | <Button | 118 | <SecretForm.SubmitButton size="sm">Save secret</SecretForm.SubmitButton> |
| 216 | size="sm" | ||
| 217 | onClick={handleSave} | ||
| 218 | disabled={!id.trim() || !name.trim() || !value.trim() || saving} | ||
| 219 | > | ||
| 220 | {saving ? "Saving…" : "Save secret"} | ||
| 221 | </Button> | ||
| 222 | </DialogFooter> | 119 | </DialogFooter> |
| 223 | </DialogContent> | 120 | </DialogContent> |
| 224 | </Dialog> | 121 | </SecretForm.Provider> |
| 225 | ); | 122 | ); |
| 226 | } | 123 | } |
| 227 | 124 | ||
| @@ -239,8 +136,16 @@ function SecretRow(props: { · 11 unmodified lines | |||
| 239 | return ( | 136 | return ( |
| 240 | <CardStackEntry> | 137 | <CardStackEntry> |
| 241 | <CardStackEntryContent> | 138 | <CardStackEntryContent> |
| 242 | <CardStackEntryTitle className="flex items-center gap-2"> | 139 | <CardStackEntryTitle className="flex min-w-0 items-center gap-2"> |
| 243 | <span className="truncate">{secret.name}</span> | 140 | <span className="min-w-0 shrink truncate" title={secret.name}> |
| 141 | {secret.name} | ||
| 142 | </span> | ||
| 143 | <span | ||
| 144 | className="max-w-40 shrink truncate font-mono text-xs text-muted-foreground" | ||
| 145 | title={secret.id} | ||
| 146 | > | ||
| 147 | {secret.id} | ||
| 148 | </span> | ||
| 244 | </CardStackEntryTitle> | 149 | </CardStackEntryTitle> |
| 245 | </CardStackEntryContent> | 150 | </CardStackEntryContent> |
| 246 | <CardStackEntryActions> | 151 | <CardStackEntryActions> |
| @@ -291,6 +196,15 @@ export function SecretsPage(props: { · 44 unmodified lines | |||
| 291 | const [addOpen, setAddOpen] = useState(false); | 196 | const [addOpen, setAddOpen] = useState(false); |
| 292 | const scopeId = useScope(); | 197 | const scopeId = useScope(); |
| 293 | const secrets = useAtomValue(secretsAtom(scopeId)); | 198 | const secrets = useAtomValue(secretsAtom(scopeId)); |
| 199 | const existingSecretIds = useMemo( | ||
| 200 | () => | ||
| 201 | AsyncResult.match(secrets, { | ||
| 202 | onInitial: () => [] as string[], | ||
| 203 | onFailure: () => [] as string[], | ||
| 204 | onSuccess: ({ value }) => value.map((secret) => secret.id), | ||
| 205 | }), | ||
| 206 | [secrets], | ||
| 207 | ); | ||
| 294 | const doRemove = useAtomSet(removeSecret, { mode: "promise" }); | 208 | const doRemove = useAtomSet(removeSecret, { mode: "promise" }); |
| 295 | 209 | ||
| 296 | const handleRemove = async (secretId: string) => { | 210 | const handleRemove = async (secretId: string) => { |
| @@ -413,6 +327,7 @@ export function SecretsPage(props: { · 116 unmodified lines | |||
| 413 | onOpenChange={setAddOpen} | 327 | onOpenChange={setAddOpen} |
| 414 | description={addSecretDescription} | 328 | description={addSecretDescription} |
| 415 | storageOptions={storageOptions} | 329 | storageOptions={storageOptions} |
| 330 | existingSecretIds={existingSecretIds} | ||
| 416 | /> | 331 | /> |
| 417 | </div> | 332 | </div> |
| 418 | </div> | 333 | </div> |
| @@ -0,0 +1,366 @@ | |||
| 1 | import { | ||
| 2 | createContext, | ||
| 3 | use, | ||
| 4 | useId, | ||
| 5 | useMemo, | ||
| 6 | useState, | ||
| 7 | type CSSProperties, | ||
| 8 | type ReactNode, | ||
| 9 | } from "react"; | ||
| 10 | import { useAtomSet } from "@effect/atom-react"; | ||
| 11 | |||
| 12 | import { setSecret } from "../api/atoms"; | ||
| 13 | import { secretWriteKeys } from "../api/reactivity-keys"; | ||
| 14 | import { useScope } from "../api/scope-context"; | ||
| 15 | import { SecretId, type ScopeId } from "@executor-js/sdk"; | ||
| 16 | import { Button, type buttonVariants } from "../components/button"; | ||
| 17 | import { Field, FieldError, FieldLabel } from "../components/field"; | ||
| 18 | import { Input } from "../components/input"; | ||
| 19 | import { | ||
| 20 | Select, | ||
| 21 | SelectContent, | ||
| 22 | SelectItem, | ||
| 23 | SelectTrigger, | ||
| 24 | SelectValue, | ||
| 25 | } from "../components/select"; | ||
| 26 | import type { VariantProps } from "class-variance-authority"; | ||
| 27 | |||
| 28 | import { | ||
| 29 | getUniqueSecretId, | ||
| 30 | isSecretIdTaken, | ||
| 31 | } from "./secret-id"; | ||
| 32 | |||
| 33 | // --------------------------------------------------------------------------- | ||
| 34 | // Context | ||
| 35 | // | ||
| 36 | // One generic interface — `state`, `actions`, `meta` — that both the modal | ||
| 37 | // and inline create flows share. Surfaces compose the `SecretForm.*` parts | ||
| 38 | // they want; provider owns state, derived values, and submit lifecycle. | ||
| 39 | // --------------------------------------------------------------------------- | ||
| 40 | |||
| 41 | type SubmitStatus = | ||
| 42 | | { readonly kind: "idle" } | ||
| 43 | | { readonly kind: "submitting" } | ||
| 44 | | { readonly kind: "error"; readonly message: string }; | ||
| 45 | |||
| 46 | interface SecretFormState { | ||
| 47 | readonly name: string; | ||
| 48 | readonly value: string; | ||
| 49 | readonly idOverride: string | null; | ||
| 50 | readonly provider: string; | ||
| 51 | readonly revealed: boolean; | ||
| 52 | readonly status: SubmitStatus; | ||
| 53 | } | ||
| 54 | |||
| 55 | interface SecretFormActions { | ||
| 56 | readonly setName: (v: string) => void; | ||
| 57 | readonly setValue: (v: string) => void; | ||
| 58 | readonly setIdOverride: (v: string) => void; | ||
| 59 | readonly setProvider: (v: string) => void; | ||
| 60 | readonly toggleReveal: () => void; | ||
| 61 | readonly submit: () => Promise<void>; | ||
| 62 | } | ||
| 63 | |||
| 64 | interface SecretFormMeta { | ||
| 65 | readonly id: string; | ||
| 66 | readonly duplicateError: string | null; | ||
| 67 | readonly canSubmit: boolean; | ||
| 68 | } | ||
| 69 | |||
| 70 | interface SecretFormContextValue { | ||
| 71 | readonly state: SecretFormState; | ||
| 72 | readonly actions: SecretFormActions; | ||
| 73 | readonly meta: SecretFormMeta; | ||
| 74 | } | ||
| 75 | |||
| 76 | const SecretFormContext = createContext<SecretFormContextValue | null>(null); | ||
| 77 | |||
| 78 | function useSecretForm(): SecretFormContextValue { | ||
| 79 | const ctx = use(SecretFormContext); | ||
| 80 | if (!ctx) { | ||
| 81 | throw new Error("SecretForm parts must be rendered inside <SecretForm.Provider>"); | ||
| 82 | } | ||
| 83 | return ctx; | ||
| 84 | } | ||
| 85 | |||
| 86 | // --------------------------------------------------------------------------- | ||
| 87 | // Provider | ||
| 88 | // --------------------------------------------------------------------------- | ||
| 89 | |||
| 90 | interface SecretFormProviderProps { | ||
| 91 | readonly existingSecretIds: readonly string[]; | ||
| 92 | readonly suggestedName?: string; | ||
| 93 | readonly fallbackId?: string; | ||
| 94 | readonly initialProvider?: string; | ||
| 95 | readonly scopeId?: ScopeId; | ||
| 96 | readonly onCreated: (secretId: string) => void; | ||
| 97 | readonly children: ReactNode; | ||
| 98 | } | ||
| 99 | |||
| 100 | function SecretFormProvider(props: SecretFormProviderProps) { | ||
| 101 | const { | ||
| 102 | existingSecretIds, | ||
| 103 | suggestedName = "", | ||
| 104 | fallbackId = "secret", | ||
| 105 | initialProvider = "auto", | ||
| 106 | scopeId: scopeIdProp, | ||
| 107 | onCreated, | ||
| 108 | children, | ||
| 109 | } = props; | ||
| 110 | |||
| 111 | const defaultScope = useScope(); | ||
| 112 | const scopeId = scopeIdProp ?? defaultScope; | ||
| 113 | const doSet = useAtomSet(setSecret, { mode: "promise" }); | ||
| 114 | |||
| 115 | const [state, setState] = useState<SecretFormState>(() => ({ | ||
| 116 | name: "", | ||
| 117 | value: "", | ||
| 118 | idOverride: null, | ||
| 119 | provider: initialProvider, | ||
| 120 | revealed: false, | ||
| 121 | status: { kind: "idle" }, | ||
| 122 | })); | ||
| 123 | |||
| 124 | const baseName = state.name || suggestedName; | ||
| 125 | const autoId = useMemo( | ||
| 126 | () => getUniqueSecretId(baseName, existingSecretIds, fallbackId), | ||
| 127 | [baseName, existingSecretIds, fallbackId], | ||
| 128 | ); | ||
| 129 | const id = state.idOverride ?? autoId; | ||
| 130 | const duplicateError = | ||
| 131 | state.idOverride !== null && isSecretIdTaken(state.idOverride, existingSecretIds) | ||
| 132 | ? "Secret ID already exists" | ||
| 133 | : null; | ||
| 134 | |||
| 135 | const displayName = state.name.trim() || suggestedName.trim(); | ||
| 136 | const canSubmit = | ||
| 137 | id.trim() !== "" && | ||
| 138 | state.value.trim() !== "" && | ||
| 139 | duplicateError === null && | ||
| 140 | state.status.kind !== "submitting"; | ||
| 141 | |||
| 142 | const submit = async () => { | ||
| 143 | if (!canSubmit) return; | ||
| 144 | setState((s) => ({ ...s, status: { kind: "submitting" } })); | ||
| 145 | try { | ||
| 146 | await doSet({ | ||
| 147 | params: { scopeId }, | ||
| 148 | payload: { | ||
| 149 | id: SecretId.make(id.trim()), | ||
| 150 | name: displayName || id.trim(), | ||
| 151 | value: state.value.trim(), | ||
| 152 | provider: state.provider === "auto" ? undefined : state.provider, | ||
| 153 | }, | ||
| 154 | reactivityKeys: secretWriteKeys, | ||
| 155 | }); | ||
| 156 | onCreated(id.trim()); | ||
| 157 | } catch (e) { | ||
| 158 | setState((s) => ({ | ||
| 159 | ...s, | ||
| 160 | status: { | ||
| 161 | kind: "error", | ||
| 162 | message: e instanceof Error ? e.message : "Failed to save secret", | ||
| 163 | }, | ||
| 164 | })); | ||
| 165 | } | ||
| 166 | }; | ||
| 167 | |||
| 168 | const value: SecretFormContextValue = { | ||
| 169 | state, | ||
| 170 | actions: { | ||
| 171 | setName: (v) => setState((s) => ({ ...s, name: v })), | ||
| 172 | setValue: (v) => setState((s) => ({ ...s, value: v })), | ||
| 173 | setIdOverride: (v) => setState((s) => ({ ...s, idOverride: v })), | ||
| 174 | setProvider: (v) => setState((s) => ({ ...s, provider: v })), | ||
| 175 | toggleReveal: () => setState((s) => ({ ...s, revealed: !s.revealed })), | ||
| 176 | submit, | ||
| 177 | }, | ||
| 178 | meta: { id, duplicateError, canSubmit }, | ||
| 179 | }; | ||
| 180 | |||
| 181 | return <SecretFormContext value={value}>{children}</SecretFormContext>; | ||
| 182 | } | ||
| 183 | |||
| 184 | // --------------------------------------------------------------------------- | ||
| 185 | // Parts | ||
| 186 | // --------------------------------------------------------------------------- | ||
| 187 | |||
| 188 | function NameField(props: { label?: string; placeholder?: string }) { | ||
| 189 | const { state, actions } = useSecretForm(); | ||
| 190 | const inputId = useId(); | ||
| 191 | return ( | ||
| 192 | <Field> | ||
| 193 | <FieldLabel htmlFor={inputId}>{props.label ?? "Name"}</FieldLabel> | ||
| 194 | <Input | ||
| 195 | id={inputId} | ||
| 196 | value={state.name} | ||
| 197 | onChange={(e) => actions.setName((e.target as HTMLInputElement).value)} | ||
| 198 | placeholder={props.placeholder ?? "GitHub PAT"} | ||
| 199 | /> | ||
| 200 | </Field> | ||
| 201 | ); | ||
| 202 | } | ||
| 203 | |||
| 204 | function IdField(props: { placeholder?: string }) { | ||
| 205 | const { actions, meta } = useSecretForm(); | ||
| 206 | const inputId = useId(); | ||
| 207 | return ( | ||
| 208 | <Field> | ||
| 209 | <FieldLabel htmlFor={inputId}>ID</FieldLabel> | ||
| 210 | <Input | ||
| 211 | id={inputId} | ||
| 212 | value={meta.id} | ||
| 213 | onChange={(e) => actions.setIdOverride((e.target as HTMLInputElement).value)} | ||
| 214 | placeholder={props.placeholder ?? "github-token"} | ||
| 215 | className="font-mono" | ||
| 216 | /> | ||
| 217 | {meta.duplicateError && <FieldError>{meta.duplicateError}</FieldError>} | ||
| 218 | </Field> | ||
| 219 | ); | ||
| 220 | } | ||
| 221 | |||
| 222 | function ValueField(props: { revealable?: boolean; placeholder?: string }) { | ||
| 223 | const { state, actions } = useSecretForm(); | ||
| 224 | const inputId = useId(); | ||
| 225 | const revealable = props.revealable ?? false; | ||
| 226 | const errored = state.status.kind === "error"; | ||
| 227 | |||
| 228 | return ( | ||
| 229 | <Field> | ||
| 230 | <FieldLabel htmlFor={inputId}>Value</FieldLabel> | ||
| 231 | <div className="relative"> | ||
| 232 | <Input | ||
| 233 | id={inputId} | ||
| 234 | type={revealable ? "text" : "password"} | ||
| 235 | value={state.value} | ||
| 236 | onChange={(e) => actions.setValue((e.target as HTMLInputElement).value)} | ||
| 237 | placeholder={props.placeholder ?? "ghp_xxxxxxxxxxxxxxxxxxxx"} | ||
| 238 | className={revealable ? "pr-9 font-mono" : "font-mono"} | ||
| 239 | style={ | ||
| 240 | revealable && !state.revealed | ||
| 241 | ? ({ WebkitTextSecurity: "disc" } as CSSProperties) | ||
| 242 | : undefined | ||
| 243 | } | ||
| 244 | /> | ||
| 245 | {revealable && ( | ||
| 246 | <Button | ||
| 247 | type="button" | ||
| 248 | variant="ghost" | ||
| 249 | size="icon-xs" | ||
| 250 | className="absolute right-1 top-1/2 size-7 -translate-y-1/2 text-muted-foreground hover:text-foreground" | ||
| 251 | onClick={actions.toggleReveal} | ||
| 252 | aria-label={state.revealed ? "Hide secret value" : "Reveal secret value"} | ||
| 253 | > | ||
| 254 | <SecretVisibilityIcon revealed={state.revealed} /> | ||
| 255 | </Button> | ||
| 256 | )} | ||
| 257 | </div> | ||
| 258 | {errored && <FieldError>{state.status.kind === "error" ? state.status.message : ""}</FieldError>} | ||
| 259 | </Field> | ||
| 260 | ); | ||
| 261 | } | ||
| 262 | |||
| 263 | function ProviderField(props: { options: readonly { label: string; value: string }[] }) { | ||
| 264 | const { state, actions } = useSecretForm(); | ||
| 265 | const inputId = useId(); | ||
| 266 | if (props.options.length <= 1) return null; | ||
| 267 | return ( | ||
| 268 | <Field> | ||
| 269 | <FieldLabel htmlFor={inputId}>Storage</FieldLabel> | ||
| 270 | <Select value={state.provider} onValueChange={actions.setProvider}> | ||
| 271 | <SelectTrigger id={inputId} className="h-9 text-sm"> | ||
| 272 | <SelectValue /> | ||
| 273 | </SelectTrigger> | ||
| 274 | <SelectContent> | ||
| 275 | {props.options.map((option) => ( | ||
| 276 | <SelectItem key={option.value} value={option.value}> | ||
| 277 | {option.label} | ||
| 278 | </SelectItem> | ||
| 279 | ))} | ||
| 280 | </SelectContent> | ||
| 281 | </Select> | ||
| 282 | </Field> | ||
| 283 | ); | ||
| 284 | } | ||
| 285 | |||
| 286 | function ErrorBanner() { | ||
| 287 | const { state } = useSecretForm(); | ||
| 288 | if (state.status.kind !== "error") return null; | ||
| 289 | return ( | ||
| 290 | <div className="rounded-lg border border-destructive/30 bg-destructive/5 px-3 py-2"> | ||
| 291 | <p className="text-sm text-destructive">{state.status.message}</p> | ||
| 292 | </div> | ||
| 293 | ); | ||
| 294 | } | ||
| 295 | |||
| 296 | type ButtonProps = React.ComponentProps<"button"> & VariantProps<typeof buttonVariants>; | ||
| 297 | |||
| 298 | function SubmitButton(props: ButtonProps & { children?: ReactNode }) { | ||
| 299 | const { state, meta, actions } = useSecretForm(); | ||
| 300 | const { children, disabled, onClick, ...rest } = props; | ||
| 301 | const submitting = state.status.kind === "submitting"; | ||
| 302 | return ( | ||
| 303 | <Button | ||
| 304 | {...rest} | ||
| 305 | onClick={(e) => { | ||
| 306 | onClick?.(e); | ||
| 307 | if (!e.defaultPrevented) void actions.submit(); | ||
| 308 | }} | ||
| 309 | disabled={disabled || !meta.canSubmit} | ||
| 310 | > | ||
| 311 | {submitting ? "Saving…" : children} | ||
| 312 | </Button> | ||
| 313 | ); | ||
| 314 | } | ||
| 315 | |||
| 316 | // --------------------------------------------------------------------------- | ||
| 317 | // Reveal-eye icon (used by ValueField when `revealable`) | ||
| 318 | // --------------------------------------------------------------------------- | ||
| 319 | |||
| 320 | function SecretVisibilityIcon(props: { revealed: boolean }) { | ||
| 321 | return props.revealed ? ( | ||
| 322 | <svg | ||
| 323 | viewBox="0 0 16 16" | ||
| 324 | className="size-3.5" | ||
| 325 | fill="none" | ||
| 326 | stroke="currentColor" | ||
| 327 | strokeWidth="1.3" | ||
| 328 | strokeLinecap="round" | ||
| 329 | strokeLinejoin="round" | ||
| 330 | > | ||
| 331 | <path d="M2 2l12 12" /> | ||
| 332 | <path d="M6.5 6.5a2 2 0 0 0 3 3" /> | ||
| 333 | <path d="M3.5 5.5C2.3 6.7 1.5 8 1.5 8s2.5 4.5 6.5 4.5c1 0 1.9-.3 2.7-.7" /> | ||
| 334 | <path d="M10.7 10.7c2-1.4 3.3-3.2 3.8-3.7 0 0-2.5-5-6.5-5-.7 0-1.4.1-2 .4" /> | ||
| 335 | </svg> | ||
| 336 | ) : ( | ||
| 337 | <svg | ||
| 338 | viewBox="0 0 16 16" | ||
| 339 | className="size-3.5" | ||
| 340 | fill="none" | ||
| 341 | stroke="currentColor" | ||
| 342 | strokeWidth="1.3" | ||
| 343 | strokeLinecap="round" | ||
| 344 | strokeLinejoin="round" | ||
| 345 | > | ||
| 346 | <path d="M1.5 8s2.5-4.5 6.5-4.5S14.5 8 14.5 8s-2.5 4.5-6.5 4.5S1.5 8 1.5 8z" /> | ||
| 347 | <circle cx="8" cy="8" r="2" /> | ||
| 348 | </svg> | ||
| 349 | ); | ||
| 350 | } | ||
| 351 | |||
| 352 | // --------------------------------------------------------------------------- | ||
| 353 | // Compound export | ||
| 354 | // --------------------------------------------------------------------------- | ||
| 355 | |||
| 356 | export const SecretForm = { | ||
| 357 | Provider: SecretFormProvider, | ||
| 358 | NameField, | ||
| 359 | IdField, | ||
| 360 | ValueField, | ||
| 361 | ProviderField, | ||
| 362 | ErrorBanner, | ||
| 363 | SubmitButton, | ||
| 364 | }; | ||
| 365 | |||
| 366 | export type { SecretFormProviderProps }; | ||
| @@ -1,13 +1,10 @@ | |||
| 1 | import { useId, useState, type CSSProperties } from "react"; | 1 | import { useId, useState } from "react"; |
| 2 | import { useAtomSet } from "@effect/atom-react"; | ||
| 3 | 2 | ||
| 4 | import { setSecret } from "../api/atoms"; | 3 | import { type ScopeId } from "@executor-js/sdk"; |
| 5 | import { secretWriteKeys } from "../api/reactivity-keys"; | ||
| 6 | import { useScope } from "../api/scope-context"; | ||
| 7 | import { SecretId, type ScopeId } from "@executor-js/sdk"; | ||
| 8 | import { Button } from "../components/button"; | 4 | import { Button } from "../components/button"; |
| 9 | import { Field, FieldError, FieldGroup, FieldLabel } from "../components/field"; | 5 | import { Field, FieldGroup, FieldLabel } from "../components/field"; |
| 10 | import { Input } from "../components/input"; | 6 | import { Input } from "../components/input"; |
| 7 | import { SecretForm } from "./secret-form"; | ||
| 11 | import { SecretPicker, type SecretPickerSecret } from "./secret-picker"; | 8 | import { SecretPicker, type SecretPickerSecret } from "./secret-picker"; |
| 12 | 9 | ||
| 13 | export interface HeaderAuthPreset { | 10 | export interface HeaderAuthPreset { |
| @@ -27,155 +24,40 @@ export const defaultHeaderAuthPresets: readonly HeaderAuthPreset[] = [ · 13 unmodified lines | |||
| 27 | { key: "custom", label: "Custom", name: "" }, | 24 | { key: "custom", label: "Custom", name: "" }, |
| 28 | ]; | 25 | ]; |
| 29 | 26 | ||
| 30 | function SecretVisibilityIcon(props: { revealed: boolean }) { | ||
| 31 | return props.revealed ? ( | ||
| 32 | <svg | ||
| 33 | viewBox="0 0 16 16" | ||
| 34 | className="size-3.5" | ||
| 35 | fill="none" | ||
| 36 | stroke="currentColor" | ||
| 37 | strokeWidth="1.3" | ||
| 38 | strokeLinecap="round" | ||
| 39 | strokeLinejoin="round" | ||
| 40 | > | ||
| 41 | <path d="M2 2l12 12" /> | ||
| 42 | <path d="M6.5 6.5a2 2 0 0 0 3 3" /> | ||
| 43 | <path d="M3.5 5.5C2.3 6.7 1.5 8 1.5 8s2.5 4.5 6.5 4.5c1 0 1.9-.3 2.7-.7" /> | ||
| 44 | <path d="M10.7 10.7c2-1.4 3.3-3.2 3.8-3.7 0 0-2.5-5-6.5-5-.7 0-1.4.1-2 .4" /> | ||
| 45 | </svg> | ||
| 46 | ) : ( | ||
| 47 | <svg | ||
| 48 | viewBox="0 0 16 16" | ||
| 49 | className="size-3.5" | ||
| 50 | fill="none" | ||
| 51 | stroke="currentColor" | ||
| 52 | strokeWidth="1.3" | ||
| 53 | strokeLinecap="round" | ||
| 54 | strokeLinejoin="round" | ||
| 55 | > | ||
| 56 | <path d="M1.5 8s2.5-4.5 6.5-4.5S14.5 8 14.5 8s-2.5 4.5-6.5 4.5S1.5 8 1.5 8z" /> | ||
| 57 | <circle cx="8" cy="8" r="2" /> | ||
| 58 | </svg> | ||
| 59 | ); | ||
| 60 | } | ||
| 61 | |||
| 62 | function slugifyForSecretId(input: string): string { | ||
| 63 | return input | ||
| 64 | .trim() | ||
| 65 | .toLowerCase() | ||
| 66 | .replace(/[^a-z0-9]+/g, "-") | ||
| 67 | .replace(/^-+|-+$/g, ""); | ||
| 68 | } | ||
| 69 | |||
| 70 | export function InlineCreateSecret(props: { | 27 | export function InlineCreateSecret(props: { |
| 71 | suggestedId: string; | ||
| 72 | suggestedName: string; | 28 | suggestedName: string; |
| 29 | existingSecretIds: readonly string[]; | ||
| 73 | onCreated: (secretId: string) => void; | 30 | onCreated: (secretId: string) => void; |
| 74 | onCancel: () => void; | 31 | onCancel: () => void; |
| 32 | fallbackId?: string; | ||
| 75 | targetScope?: ScopeId; | 33 | targetScope?: ScopeId; |
| 76 | writeScope?: ScopeId; | 34 | writeScope?: ScopeId; |
| 77 | }) { | 35 | }) { |
| 78 | const [nameOverride, setNameOverride] = useState<string | null>(null); | ||
| 79 | const [idOverride, setIdOverride] = useState<string | null>(null); | ||
| 80 | const [secretValue, setSecretValue] = useState(""); | ||
| 81 | const [secretRevealed, setSecretRevealed] = useState(false); | ||
| 82 | const [saving, setSaving] = useState(false); | ||
| 83 | const [error, setError] = useState<string | null>(null); | ||
| 84 | const defaultScope = useScope(); | ||
| 85 | const scopeId = props.targetScope ?? props.writeScope ?? defaultScope; | ||
| 86 | const doSet = useAtomSet(setSecret, { mode: "promise" }); | ||
| 87 | const secretIdInputId = useId(); | ||
| 88 | const secretNameInputId = useId(); | ||
| 89 | const secretValueInputId = useId(); | ||
| 90 | |||
| 91 | const secretName = nameOverride ?? props.suggestedName; | ||
| 92 | const secretId = idOverride ?? (slugifyForSecretId(secretName) || "custom-header"); | ||
| 93 | |||
| 94 | const handleSave = async () => { | ||
| 95 | if (!secretId.trim() || !secretValue.trim()) return; | ||
| 96 | setSaving(true); | ||
| 97 | setError(null); | ||
| 98 | try { | ||
| 99 | await doSet({ | ||
| 100 | params: { scopeId }, | ||
| 101 | payload: { | ||
| 102 | id: SecretId.make(secretId.trim()), | ||
| 103 | name: secretName.trim() || secretId.trim(), | ||
| 104 | value: secretValue.trim(), | ||
| 105 | }, | ||
| 106 | reactivityKeys: secretWriteKeys, | ||
| 107 | }); | ||
| 108 | props.onCreated(secretId.trim()); | ||
| 109 | } catch (e) { | ||
| 110 | setError(e instanceof Error ? e.message : "Failed to save secret"); | ||
| 111 | setSaving(false); | ||
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | return ( | 36 | return ( |
| 116 | <div className="bg-primary/[0.03] px-4 py-3 space-y-3"> | 37 | <SecretForm.Provider |
| 117 | <p className="text-[11px] font-semibold text-primary tracking-wide uppercase">New secret</p> | 38 | existingSecretIds={props.existingSecretIds} |
| 118 | <FieldGroup className="gap-3"> | 39 | suggestedName={props.suggestedName} |
| 119 | <div className="grid grid-cols-2 gap-3"> | 40 | fallbackId={props.fallbackId ?? "custom-header"} |
| 120 | <Field> | 41 | scopeId={props.targetScope ?? props.writeScope} |
| 121 | <FieldLabel htmlFor={secretNameInputId}>Label</FieldLabel> | 42 | onCreated={props.onCreated} |
| 122 | <Input | 43 | > |
| 123 | id={secretNameInputId} | 44 | <div className="bg-primary/[0.03] px-4 py-3 space-y-3"> |
| 124 | value={secretName} | 45 | <p className="text-[11px] font-semibold text-primary tracking-wide uppercase">New secret</p> |
| 125 | onChange={(e) => setNameOverride((e.target as HTMLInputElement).value)} | 46 | <FieldGroup className="gap-3"> |
| 126 | placeholder="API Token" | 47 | <div className="grid grid-cols-2 gap-3"> |
| 127 | /> | 48 | <SecretForm.NameField label="Label" placeholder="API Token" /> |
| 128 | </Field> | 49 | <SecretForm.IdField placeholder="my-api-token" /> |
| 129 | <Field> | ||
| 130 | <FieldLabel htmlFor={secretIdInputId}>ID</FieldLabel> | ||
| 131 | <Input | ||
| 132 | id={secretIdInputId} | ||
| 133 | value={secretId} | ||
| 134 | onChange={(e) => setIdOverride((e.target as HTMLInputElement).value)} | ||
| 135 | placeholder="my-api-token" | ||
| 136 | className="font-mono" | ||
| 137 | /> | ||
| 138 | </Field> | ||
| 139 | </div> | ||
| 140 | <Field> | ||
| 141 | <FieldLabel htmlFor={secretValueInputId}>Value</FieldLabel> | ||
| 142 | <div className="relative"> | ||
| 143 | <Input | ||
| 144 | id={secretValueInputId} | ||
| 145 | type="text" | ||
| 146 | value={secretValue} | ||
| 147 | onChange={(e) => setSecretValue((e.target as HTMLInputElement).value)} | ||
| 148 | placeholder="paste your token or key…" | ||
| 149 | className="pr-9 font-mono" | ||
| 150 | style={secretRevealed ? undefined : ({ WebkitTextSecurity: "disc" } as CSSProperties)} | ||
| 151 | /> | ||
| 152 | <Button | ||
| 153 | type="button" | ||
| 154 | variant="ghost" | ||
| 155 | size="icon-xs" | ||
| 156 | className="absolute right-1 top-1/2 size-7 -translate-y-1/2 text-muted-foreground hover:text-foreground" | ||
| 157 | onClick={() => setSecretRevealed((revealed) => !revealed)} | ||
| 158 | aria-label={secretRevealed ? "Hide secret value" : "Reveal secret value"} | ||
| 159 | > | ||
| 160 | <SecretVisibilityIcon revealed={secretRevealed} /> | ||
| 161 | </Button> | ||
| 162 | </div> | 50 | </div> |
| 163 | {error && <FieldError>{error}</FieldError>} | 51 | <SecretForm.ValueField revealable placeholder="paste your token or key…" /> |
| 164 | </Field> | 52 | </FieldGroup> |
| 165 | </FieldGroup> | 53 | <div className="flex justify-end gap-1.5 pt-0.5"> |
| 166 | <div className="flex justify-end gap-1.5 pt-0.5"> | 54 | <Button variant="outline" size="xs" onClick={props.onCancel}> |
| 167 | <Button variant="outline" size="xs" onClick={props.onCancel}> | 55 | Cancel |
| 168 | Cancel | 56 | </Button> |
| 169 | </Button> | 57 | <SecretForm.SubmitButton size="xs">Create and use</SecretForm.SubmitButton> |
| 170 | <Button | 58 | </div> |
| 171 | size="xs" | ||
| 172 | onClick={handleSave} | ||
| 173 | disabled={!secretId.trim() || !secretValue.trim() || saving} | ||
| 174 | > | ||
| 175 | {saving ? "Saving…" : "Create and use"} | ||
| 176 | </Button> | ||
| 177 | </div> | 59 | </div> |
| 178 | </div> | 60 | </SecretForm.Provider> |
| 179 | ); | 61 | ); |
| 180 | } | 62 | } |
| 181 | 63 | ||
| @@ -288,13 +170,12 @@ export function SecretHeaderAuthRow(props: { · 106 unmodified lines | |||
| 288 | const isCustom = presetKey === "custom" || presetKey === undefined; | 170 | const isCustom = presetKey === "custom" || presetKey === undefined; |
| 289 | const headerLabel = name.trim() || "Custom Header"; | 171 | const headerLabel = name.trim() || "Custom Header"; |
| 290 | const suggestedName = [sourceName?.trim(), headerLabel].filter(Boolean).join(" "); | 172 | const suggestedName = [sourceName?.trim(), headerLabel].filter(Boolean).join(" "); |
| 291 | const suggestedId = slugifyForSecretId(suggestedName) || "custom-header"; | ||
| 292 | 173 | ||
| 293 | if (creating) { | 174 | if (creating) { |
| 294 | return ( | 175 | return ( |
| 295 | <InlineCreateSecret | 176 | <InlineCreateSecret |
| 296 | suggestedId={suggestedId} | ||
| 297 | suggestedName={suggestedName} | 177 | suggestedName={suggestedName} |
| 178 | existingSecretIds={existingSecrets.map((secret) => secret.id)} | ||
| 298 | onCreated={(id) => { | 179 | onCreated={(id) => { |
| 299 | onSelectSecret(id); | 180 | onSelectSecret(id); |
| 300 | setCreating(false); | 181 | setCreating(false); |
| @@ -406,13 +287,13 @@ export function CreatableSecretPicker(props: { · 105 unmodified lines | |||
| 406 | const [creating, setCreating] = useState(false); | 287 | const [creating, setCreating] = useState(false); |
| 407 | 288 | ||
| 408 | const suggestedName = [sourceName?.trim(), secretLabel].filter(Boolean).join(" "); | 289 | const suggestedName = [sourceName?.trim(), secretLabel].filter(Boolean).join(" "); |
| 409 | const suggestedId = suggestedIdProp?.trim() || slugifyForSecretId(suggestedName) || "secret"; | ||
| 410 | 290 | ||
| 411 | if (creating) { | 291 | if (creating) { |
| 412 | return ( | 292 | return ( |
| 413 | <InlineCreateSecret | 293 | <InlineCreateSecret |
| 414 | suggestedId={suggestedId} | ||
| 415 | suggestedName={suggestedName} | 294 | suggestedName={suggestedName} |
| 295 | existingSecretIds={secrets.map((secret) => secret.id)} | ||
| 296 | fallbackId={suggestedIdProp?.trim() || "secret"} | ||
| 416 | onCreated={(id) => { | 297 | onCreated={(id) => { |
| 417 | onSelect(id); | 298 | onSelect(id); |
| 418 | setCreating(false); | 299 | setCreating(false); |
| @@ -0,0 +1,34 @@ | |||
| 1 | import { describe, expect, it } from "@effect/vitest"; | ||
| 2 | |||
| 3 | import { | ||
| 4 | getUniqueSecretId, | ||
| 5 | isSecretIdTaken, | ||
| 6 | slugifyForSecretId, | ||
| 7 | } from "./secret-id"; | ||
| 8 | |||
| 9 | describe("secret id helpers", () => { | ||
| 10 | it("slugifies display names into secret ids", () => { | ||
| 11 | expect(slugifyForSecretId("GitHub PAT")).toBe("github-pat"); | ||
| 12 | expect(slugifyForSecretId(" Client Secret ")).toBe("client-secret"); | ||
| 13 | }); | ||
| 14 | |||
| 15 | it("returns the base id when it is unused", () => { | ||
| 16 | expect(getUniqueSecretId("GitHub PAT", ["openai-api-key"])).toBe("github-pat"); | ||
| 17 | }); | ||
| 18 | |||
| 19 | it("appends a numeric suffix when the base id already exists", () => { | ||
| 20 | expect(getUniqueSecretId("GitHub PAT", ["github-pat"])).toBe("github-pat-2"); | ||
| 21 | expect(getUniqueSecretId("GitHub PAT", ["github-pat", "github-pat-2"])).toBe( | ||
| 22 | "github-pat-3", | ||
| 23 | ); | ||
| 24 | }); | ||
| 25 | |||
| 26 | it("matches existing ids exactly after trimming", () => { | ||
| 27 | expect(isSecretIdTaken("github-pat", [" github-pat "])).toBe(true); | ||
| 28 | expect(isSecretIdTaken("github-pat", ["github-pat-2"])).toBe(false); | ||
| 29 | }); | ||
| 30 | |||
| 31 | it("allows an empty fallback for flows that should start blank", () => { | ||
| 32 | expect(getUniqueSecretId("", [], "")).toBe(""); | ||
| 33 | }); | ||
| 34 | }); | ||
| @@ -0,0 +1,44 @@ | |||
| 1 | // Pure helpers shared by `SecretForm` (compound form for new-secret flows) | ||
| 2 | // and the reuse tests. UI state lives in `secret-form.tsx`. | ||
| 3 | |||
| 4 | export function slugifyForSecretId(input: string): string { | ||
| 5 | return input | ||
| 6 | .trim() | ||
| 7 | .toLowerCase() | ||
| 8 | .replace(/[^a-z0-9]+/g, "-") | ||
| 9 | .replace(/^-+|-+$/g, ""); | ||
| 10 | } | ||
| 11 | |||
| 12 | const normalizeSecretId = (secretId: string): string => secretId.trim(); | ||
| 13 | |||
| 14 | export function isSecretIdTaken( | ||
| 15 | secretId: string, | ||
| 16 | existingSecretIds: Iterable<string>, | ||
| 17 | ): boolean { | ||
| 18 | const normalizedId = normalizeSecretId(secretId); | ||
| 19 | if (!normalizedId) return false; | ||
| 20 | |||
| 21 | for (const existingSecretId of existingSecretIds) { | ||
| 22 | if (normalizeSecretId(existingSecretId) === normalizedId) { | ||
| 23 | return true; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | return false; | ||
| 28 | } | ||
| 29 | |||
| 30 | export function getUniqueSecretId( | ||
| 31 | baseName: string, | ||
| 32 | existingSecretIds: Iterable<string>, | ||
| 33 | fallbackId = "secret", | ||
| 34 | ): string { | ||
| 35 | const baseId = slugifyForSecretId(baseName) || fallbackId; | ||
| 36 | if (!baseId) return ""; | ||
| 37 | if (!isSecretIdTaken(baseId, existingSecretIds)) return baseId; | ||
| 38 | |||
| 39 | let suffix = 2; | ||
| 40 | while (isSecretIdTaken(`${baseId}-${suffix}`, existingSecretIds)) { | ||
| 41 | suffix += 1; | ||
| 42 | } | ||
| 43 | return `${baseId}-${suffix}`; | ||
| 44 | } | ||
| @@ -35,7 +35,7 @@ export function SecretPicker(props: { · 34 unmodified lines | |||
| 35 | readonly onSelect: (secretId: string) => void; | 35 | readonly onSelect: (secretId: string) => void; |
| 36 | readonly secrets: readonly SecretPickerSecret[]; | 36 | readonly secrets: readonly SecretPickerSecret[]; |
| 37 | readonly placeholder?: string; | 37 | readonly placeholder?: string; |
| 38 | /** When provided, renders a "+ New secret" row at the bottom of the dropdown. */ | 38 | /** When provided, renders a "+ New secret" row at the top of the dropdown. */ |
| 39 | readonly onCreateNew?: () => void; | 39 | readonly onCreateNew?: () => void; |
| 40 | }) { | 40 | }) { |
| 41 | const { value, onSelect, secrets, placeholder = "Search secrets…", onCreateNew } = props; | 41 | const { value, onSelect, secrets, placeholder = "Search secrets…", onCreateNew } = props; |
| @@ -98,6 +98,25 @@ export function SecretPicker(props: { · 56 unmodified lines | |||
| 98 | <Command shouldFilter={false}> | 98 | <Command shouldFilter={false}> |
| 99 | <CommandList> | 99 | <CommandList> |
| 100 | <CommandEmpty>No secrets found</CommandEmpty> | 100 | <CommandEmpty>No secrets found</CommandEmpty> |
| 101 | {onCreateNew && ( | ||
| 102 | <> | ||
| 103 | <CommandGroup> | ||
| 104 | <CommandItem | ||
| 105 | value="__create_new__" | ||
| 106 | onSelect={() => { | ||
| 107 | onCreateNew(); | ||
| 108 | setOpen(false); | ||
| 109 | setQuery(""); | ||
| 110 | }} | ||
| 111 | className="text-muted-foreground data-[selected=true]:text-foreground" | ||
| 112 | > | ||
| 113 | <PlusIcon aria-hidden className="size-3.5" /> | ||
| 114 | <span>New secret</span> | ||
| 115 | </CommandItem> | ||
| 116 | </CommandGroup> | ||
| 117 | {secrets.length > 0 && <CommandSeparator />} | ||
| 118 | </> | ||
| 119 | )} | ||
| 101 | {groups.map(([label, items]) => { | 120 | {groups.map(([label, items]) => { |
| 102 | const lowerQuery = query.toLowerCase(); | 121 | const lowerQuery = query.toLowerCase(); |
| 103 | const filtered = lowerQuery | 122 | const filtered = lowerQuery |
| @@ -126,25 +145,6 @@ export function SecretPicker(props: { · 22 unmodified lines | |||
| 126 | </CommandGroup> | 145 | </CommandGroup> |
| 127 | ); | 146 | ); |
| 128 | })} | 147 | })} |
| 129 | {onCreateNew && ( | ||
| 130 | <> | ||
| 131 | {secrets.length > 0 && <CommandSeparator />} | ||
| 132 | <CommandGroup> | ||
| 133 | <CommandItem | ||
| 134 | value="__create_new__" | ||
| 135 | onSelect={() => { | ||
| 136 | onCreateNew(); | ||
| 137 | setOpen(false); | ||
| 138 | setQuery(""); | ||
| 139 | }} | ||
| 140 | className="text-muted-foreground data-[selected=true]:text-foreground" | ||
| 141 | > | ||
| 142 | <PlusIcon aria-hidden className="size-3.5" /> | ||
| 143 | <span>New secret</span> | ||
| 144 | </CommandItem> | ||
| 145 | </CommandGroup> | ||
| 146 | </> | ||
| 147 | )} | ||
| 148 | </CommandList> | 148 | </CommandList> |
| 149 | </Command> | 149 | </Command> |
| 150 | </PopoverContent> | 150 | </PopoverContent> |
| @@ -0,0 +1,7 @@ | |||
| 1 | import { defineConfig } from "vitest/config"; | ||
| 2 | |||
| 3 | export default defineConfig({ | ||
| 4 | test: { | ||
| 5 | include: ["src/**/*.test.ts", "src/**/*.test.tsx"], | ||
| 6 | }, | ||
| 7 | }); | ||