The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | SecretReference, |
| 3 | type EnvironmentId, |
| 4 | type SecretProviderConfig, |
| 5 | type SecretProviderId, |
| 6 | type SecretProviderKind, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import { builtInSecretProviderDefinitions } from "@t3tools/secret-provider-definitions/registry"; |
| 9 | import { invalidSecretReferenceText } from "@t3tools/client-runtime/secret-request"; |
| 10 | import { useAtomValue } from "@effect/atom-react"; |
| 11 | import * as Option from "effect/Option"; |
| 12 | import * as Schema from "effect/Schema"; |
| 13 | import { useGT, useMessages } from "gt-react"; |
| 14 | import { AlertTriangleIcon, CheckIcon, KeyRoundIcon, RefreshCwIcon, TypeIcon } from "lucide-react"; |
| 15 | import { useState, type ComponentProps, type ComponentType, type ReactNode } from "react"; |
| 16 | |
| 17 | import { isSecretsFeatureEnabled } from "../../env"; |
| 18 | import { useEnvironmentSettings } from "../../hooks/useSettings"; |
| 19 | import { OnePasswordIcon } from "../Icons"; |
| 20 | import { serverEnvironment } from "../../state/server"; |
| 21 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 22 | import { Button } from "../ui/button"; |
| 23 | import { DraftInput } from "../ui/draft-input"; |
| 24 | import { Select, SelectItem, SelectPopup, SelectTrigger, SelectValue } from "../ui/select"; |
| 25 | import { Spinner } from "../ui/spinner"; |
| 26 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 27 | import { SecretInput } from "./SecretInput"; |
| 28 | import { classifySecretProvider } from "./SecretProviderDialog.logic"; |
| 29 | |
| 30 | export interface SecretSource { |
| 31 | readonly id: SecretProviderId; |
| 32 | readonly kind: SecretProviderKind; |
| 33 | readonly label: string; |
| 34 | } |
| 35 | |
| 36 | const NO_SECRET_SOURCES: ReadonlyArray<SecretSource> = []; |
| 37 | const STORED_VALUE = "__stored__"; |
| 38 | const decodeSecretReference = Schema.decodeUnknownOption(SecretReference); |
| 39 | |
| 40 | /** Secret providers a setting can point at; empty when the environment cannot read references. */ |
| 41 | export function useSecretSources(environmentId: EnvironmentId): ReadonlyArray<SecretSource> { |
| 42 | const providers = useEnvironmentSettings(environmentId, (settings) => settings.secretProviders); |
| 43 | const supported = useAtomValue( |
| 44 | serverEnvironment.configValueAtom(environmentId), |
| 45 | (config) => config?.secretReferences === true, |
| 46 | ); |
| 47 | if (!isSecretsFeatureEnabled || !supported) return NO_SECRET_SOURCES; |
| 48 | return (Object.entries(providers) as Array<[SecretProviderId, SecretProviderConfig]>) |
| 49 | .filter(([, config]) => classifySecretProvider(config).kind !== "unsupported") |
| 50 | .map(([id, config]) => ({ id, kind: config.kind, label: config.label })); |
| 51 | } |
| 52 | |
| 53 | function SourceIcon(props: { readonly icon: ComponentType<{ readonly className?: string }> }) { |
| 54 | return <props.icon className="size-3.5 shrink-0" />; |
| 55 | } |
| 56 | |
| 57 | function SourceLabel(props: { |
| 58 | readonly icon: ComponentType<{ readonly className?: string }>; |
| 59 | readonly label: string; |
| 60 | }) { |
| 61 | return ( |
| 62 | <span className="flex min-w-0 items-center gap-1.5"> |
| 63 | <props.icon className="size-3.5 shrink-0" /> |
| 64 | <span className="truncate">{props.label}</span> |
| 65 | </span> |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | /** Picks Value (`null`) or one of `sources`; a `value` no longer among them stays listed. */ |
| 70 | export function SecretSourceSelect(props: { |
| 71 | readonly value: SecretProviderId | null; |
| 72 | readonly sources: ReadonlyArray<Pick<SecretSource, "id" | "label">>; |
| 73 | readonly label: string; |
| 74 | readonly disabled?: boolean; |
| 75 | readonly onValueChange: (provider: SecretProviderId | null) => void; |
| 76 | }) { |
| 77 | const gt = useGT(); |
| 78 | const { value, sources } = props; |
| 79 | const known = value === null || sources.some((source) => source.id === value); |
| 80 | const title = |
| 81 | value === null |
| 82 | ? gt("Value", { $context: "secret source: a value typed in directly" }) |
| 83 | : (sources.find((source) => source.id === value)?.label ?? value); |
| 84 | return ( |
| 85 | <Select |
| 86 | value={value ?? STORED_VALUE} |
| 87 | onValueChange={(next) => |
| 88 | props.onValueChange( |
| 89 | next === STORED_VALUE || next === null ? null : (next as SecretProviderId), |
| 90 | ) |
| 91 | } |
| 92 | {...(props.disabled === undefined ? {} : { disabled: props.disabled })} |
| 93 | > |
| 94 | <SelectTrigger |
| 95 | size="sm" |
| 96 | className="w-auto min-w-0 shrink-0" |
| 97 | aria-label={gt("{label} source", { label: props.label })} |
| 98 | title={title} |
| 99 | > |
| 100 | <SelectValue> |
| 101 | <SourceIcon icon={value === null ? TypeIcon : known ? OnePasswordIcon : KeyRoundIcon} /> |
| 102 | </SelectValue> |
| 103 | </SelectTrigger> |
| 104 | <SelectPopup align="start" alignItemWithTrigger={false}> |
| 105 | <SelectItem value={STORED_VALUE}> |
| 106 | <SourceLabel |
| 107 | icon={TypeIcon} |
| 108 | label={gt("Value", { $context: "secret source: a value typed in directly" })} |
| 109 | /> |
| 110 | </SelectItem> |
| 111 | {sources.map((source) => ( |
| 112 | <SelectItem key={source.id} value={source.id}> |
| 113 | <SourceLabel icon={OnePasswordIcon} label={source.label} /> |
| 114 | </SelectItem> |
| 115 | ))} |
| 116 | {known ? null : ( |
| 117 | <SelectItem value={value}> |
| 118 | <SourceLabel icon={KeyRoundIcon} label={value} /> |
| 119 | </SelectItem> |
| 120 | )} |
| 121 | </SelectPopup> |
| 122 | </Select> |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | type SecretCheck = |
| 127 | | { readonly status: "checking" } |
| 128 | | { readonly status: "resolved" } |
| 129 | | { readonly status: "failed"; readonly detail: string }; |
| 130 | |
| 131 | /** Asks the server to resolve the committed reference, so a typo shows here and not at provider start. */ |
| 132 | function SecretCheckButton(props: { |
| 133 | readonly environmentId: EnvironmentId; |
| 134 | readonly secret: SecretReference; |
| 135 | readonly label: string; |
| 136 | readonly disabled?: boolean | undefined; |
| 137 | }) { |
| 138 | const gt = useGT(); |
| 139 | const { environmentId, secret } = props; |
| 140 | const supported = useAtomValue( |
| 141 | serverEnvironment.configValueAtom(environmentId), |
| 142 | (config) => config?.environment.capabilities.secretSourceCheck === true, |
| 143 | ); |
| 144 | const canCheck = useAtomValue(serverEnvironment.checkSecretSource.permissionAtom(environmentId)); |
| 145 | const checkSecretSource = useAtomCommand(serverEnvironment.checkSecretSource); |
| 146 | const secretKey = `${secret.provider}\u0000${secret.reference}`; |
| 147 | const [check, setCheck] = useState<{ |
| 148 | readonly secretKey: string; |
| 149 | readonly state: SecretCheck; |
| 150 | } | null>(null); |
| 151 | const state = check?.secretKey === secretKey ? check.state : undefined; |
| 152 | |
| 153 | const run = async () => { |
| 154 | setCheck({ secretKey, state: { status: "checking" } }); |
| 155 | const response = await checkSecretSource({ environmentId, input: { source: secret } }); |
| 156 | const next: SecretCheck = |
| 157 | response._tag !== "Success" |
| 158 | ? { status: "failed", detail: gt("Could not reach the server") } |
| 159 | : response.value._tag === "Resolved" |
| 160 | ? { status: "resolved" } |
| 161 | : { status: "failed", detail: response.value.detail }; |
| 162 | setCheck((previous) => |
| 163 | previous?.secretKey === secretKey ? { secretKey, state: next } : previous, |
| 164 | ); |
| 165 | }; |
| 166 | |
| 167 | if (!supported) return null; |
| 168 | const description = !canCheck |
| 169 | ? gt("You cannot check secrets on this environment") |
| 170 | : state === undefined |
| 171 | ? gt("Check that the secret resolves") |
| 172 | : state.status === "checking" |
| 173 | ? gt("Checking the secret") |
| 174 | : state.status === "resolved" |
| 175 | ? gt("Secret resolved") |
| 176 | : state.detail; |
| 177 | return ( |
| 178 | <Tooltip> |
| 179 | <TooltipTrigger |
| 180 | render={ |
| 181 | <span className="inline-flex"> |
| 182 | <Button |
| 183 | type="button" |
| 184 | size="icon-micro" |
| 185 | variant="ghost-muted" |
| 186 | disabled={props.disabled === true || !canCheck || state?.status === "checking"} |
| 187 | onClick={() => void run()} |
| 188 | aria-label={ |
| 189 | state?.status === "failed" |
| 190 | ? gt("{label} failed: {detail}", { label: props.label, detail: state.detail }) |
| 191 | : state?.status === "resolved" |
| 192 | ? gt("{label} resolved", { label: props.label }) |
| 193 | : props.label |
| 194 | } |
| 195 | > |
| 196 | {state?.status === "checking" ? ( |
| 197 | <Spinner /> |
| 198 | ) : state?.status === "resolved" ? ( |
| 199 | <CheckIcon className="text-success-foreground" /> |
| 200 | ) : state?.status === "failed" ? ( |
| 201 | <AlertTriangleIcon className="text-destructive" /> |
| 202 | ) : ( |
| 203 | <RefreshCwIcon /> |
| 204 | )} |
| 205 | </Button> |
| 206 | </span> |
| 207 | } |
| 208 | /> |
| 209 | <TooltipPopup side="top">{description}</TooltipPopup> |
| 210 | </Tooltip> |
| 211 | ); |
| 212 | } |
| 213 | |
| 214 | type SecretSourceInputProps = Omit<ComponentProps<typeof SecretInput>, "value" | "children"> & { |
| 215 | readonly value: string | SecretReference; |
| 216 | /** Offers a check of the committed reference on this environment. */ |
| 217 | readonly environmentId?: EnvironmentId | undefined; |
| 218 | readonly sources: ReadonlyArray<SecretSource>; |
| 219 | readonly onSecretChange: (secret: SecretReference | undefined) => void; |
| 220 | /** Whether the selected source and committed field can be saved or used. */ |
| 221 | readonly onValidityChange?: (valid: boolean) => void; |
| 222 | /** Places the parts in the caller's layout; `source` is null when there is nothing to pick. */ |
| 223 | readonly children?: (parts: { |
| 224 | readonly source: ReactNode; |
| 225 | readonly value: ReactNode; |
| 226 | }) => ReactNode; |
| 227 | }; |
| 228 | |
| 229 | /** A secret entered here, or read from a secret provider when one is configured. */ |
| 230 | export function SecretSourceInput({ |
| 231 | value, |
| 232 | environmentId, |
| 233 | sources, |
| 234 | onSecretChange, |
| 235 | onValidityChange, |
| 236 | children, |
| 237 | ...rest |
| 238 | }: SecretSourceInputProps) { |
| 239 | // A provider picked here applies once a reference for it is committed. |
| 240 | const gt = useGT(); |
| 241 | const messages = useMessages(); |
| 242 | const [pendingProvider, setPendingProvider] = useState<SecretProviderId | null>(null); |
| 243 | const [rejectedReference, setRejectedReference] = useState<string | null>(null); |
| 244 | const secret = typeof value === "string" ? undefined : value; |
| 245 | const input = { ...rest, value: typeof value === "string" ? value : "" }; |
| 246 | const provider = pendingProvider ?? secret?.provider ?? null; |
| 247 | const reference = secret !== undefined && secret.provider === provider ? secret.reference : ""; |
| 248 | const providerSource = sources.find((source) => source.id === provider); |
| 249 | const definition = providerSource && builtInSecretProviderDefinitions.find(providerSource.kind); |
| 250 | const label = input["aria-label"] ?? gt("Secret"); |
| 251 | const source = |
| 252 | sources.length === 0 && provider === null ? null : ( |
| 253 | <SecretSourceSelect |
| 254 | value={provider} |
| 255 | sources={sources} |
| 256 | label={label} |
| 257 | onValueChange={(next) => { |
| 258 | setRejectedReference(null); |
| 259 | if (next === null) { |
| 260 | setPendingProvider(null); |
| 261 | onValidityChange?.(true); |
| 262 | if (secret !== undefined) onSecretChange(undefined); |
| 263 | return; |
| 264 | } |
| 265 | onValidityChange?.(next === secret?.provider); |
| 266 | setPendingProvider(next === secret?.provider ? null : next); |
| 267 | }} |
| 268 | /> |
| 269 | ); |
| 270 | |
| 271 | const valueField = |
| 272 | provider === null ? ( |
| 273 | <SecretInput {...input} /> |
| 274 | ) : ( |
| 275 | <div className="flex min-w-0 flex-1 flex-col gap-1"> |
| 276 | <div className="flex min-w-0 items-center gap-1.5"> |
| 277 | <DraftInput |
| 278 | {...(input.id !== undefined ? { id: input.id } : {})} |
| 279 | size="sm" |
| 280 | font="mono" |
| 281 | className="min-w-0 flex-1" |
| 282 | value={rejectedReference ?? reference} |
| 283 | onCommit={(next) => { |
| 284 | if (next.trim().length === 0) { |
| 285 | setRejectedReference(null); |
| 286 | onValidityChange?.(pendingProvider === null && secret !== undefined); |
| 287 | if (pendingProvider === null && secret !== undefined) onSecretChange(undefined); |
| 288 | return; |
| 289 | } |
| 290 | const decoded = decodeSecretReference({ provider, reference: next }); |
| 291 | if ( |
| 292 | Option.isNone(decoded) || |
| 293 | definition?.isReference(decoded.value.reference) === false |
| 294 | ) { |
| 295 | setRejectedReference(next); |
| 296 | onValidityChange?.(false); |
| 297 | return; |
| 298 | } |
| 299 | setRejectedReference(null); |
| 300 | setPendingProvider(null); |
| 301 | onValidityChange?.(true); |
| 302 | onSecretChange(decoded.value); |
| 303 | }} |
| 304 | autoComplete="off" |
| 305 | placeholder={definition?.referencePlaceholder} |
| 306 | spellCheck={false} |
| 307 | aria-label={gt("{label} reference", { label })} |
| 308 | aria-invalid={rejectedReference !== null} |
| 309 | /> |
| 310 | {environmentId !== undefined && |
| 311 | secret !== undefined && |
| 312 | pendingProvider === null && |
| 313 | rejectedReference === null ? ( |
| 314 | <SecretCheckButton |
| 315 | environmentId={environmentId} |
| 316 | secret={secret} |
| 317 | label={gt("Check {label}", { label })} |
| 318 | disabled={input.disabled} |
| 319 | /> |
| 320 | ) : null} |
| 321 | </div> |
| 322 | {rejectedReference !== null ? ( |
| 323 | <p className="text-2xs text-destructive"> |
| 324 | {definition |
| 325 | ? invalidSecretReferenceText(definition.invalidReferenceMessage, messages) |
| 326 | : gt("Enter a valid secret reference.")} |
| 327 | </p> |
| 328 | ) : null} |
| 329 | </div> |
| 330 | ); |
| 331 | |
| 332 | if (children !== undefined) return children({ source, value: valueField }); |
| 333 | if (source === null) return valueField; |
| 334 | return ( |
| 335 | <div className="flex min-w-0 flex-1 items-start gap-1.5"> |
| 336 | {source} |
| 337 | {valueField} |
| 338 | </div> |
| 339 | ); |
| 340 | } |