Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat(ui): suggested values for naming inputs, starting with secret sync names (#8702)

Andrew Huangb2a45a369a93
diff --git a/frontend/src/components/secret-syncs/forms/SecretSyncDetailsFields.tsx b/frontend/src/components/secret-syncs/forms/SecretSyncDetailsFields.tsx
index 69d69f65e5..52430946ab 100644
--- a/frontend/src/components/secret-syncs/forms/SecretSyncDetailsFields.tsx
+++ b/frontend/src/components/secret-syncs/forms/SecretSyncDetailsFields.tsx
@@ -1,4 +1,5 @@
import { Controller, useFormContext } from "react-hook-form";
+import slugify from "@sindresorhus/slugify";
import {
Field,
@@ -8,11 +9,48 @@ import {
Input,
TextArea
} from "@app/components/v3";
+import { SECRET_SYNC_MAP } from "@app/helpers/secretSyncs";
+import { TSecretSync } from "@app/hooks/api/secretSyncs";
+import { getSecretSyncDestinationColValues } from "@app/pages/secret-manager/IntegrationsListPage/components/SecretSyncsTab/SecretSyncTable/helpers";
-import { TSecretSyncForm } from "./schemas";
+import { getSecretSyncDestinationConfig, TSecretSyncForm } from "./schemas";
+
+const getSuggestedSyncName = ({
+ destination,
+ destinationConfig,
+ connection,
+ environment
+}: TSecretSyncForm) => {
+ const parsedConfig = getSecretSyncDestinationConfig(destination, destinationConfig);
+ let destinationLabel = "";
+ if (parsedConfig) {
+ try {
+ destinationLabel = getSecretSyncDestinationColValues({
+ destination,
+ destinationConfig: parsedConfig,
+ connection
+ } as TSecretSync).primaryText;
+ } catch {
+ destinationLabel = "";
+ }
+ }
+
+ const environmentSlug = environment?.slug
+ ? slugify(environment.slug, { lowercase: true, decamelize: false })
+ : "";
+ const prefix = slugify(`${SECRET_SYNC_MAP[destination].name} ${destinationLabel}`, {
+ lowercase: true,
+ decamelize: false
+ })
+ .slice(0, environmentSlug ? Math.max(0, 63 - environmentSlug.length) : 64)
+ .replace(/-+$/, "");
+
+ return [prefix, environmentSlug].filter(Boolean).join("-");
+};
export const SecretSyncDetailsFields = () => {
- const { control } = useFormContext<TSecretSyncForm>();
+ const { control, getValues } = useFormContext<TSecretSyncForm>();
+ const suggestedName = getSuggestedSyncName(getValues());
return (
<>
@@ -26,7 +64,7 @@ export const SecretSyncDetailsFields = () => {
id="sync-name"
value={value ?? ""}
onChange={onChange}
- placeholder="my-secret-sync"
+ suggestedValue={suggestedName}
isError={Boolean(error)}
autoFocus
autoComplete="off"
diff --git a/frontend/src/components/v3/generic/Input/Input.tsx b/frontend/src/components/v3/generic/Input/Input.tsx
index eb9c32a8a8..815d978453 100644
--- a/frontend/src/components/v3/generic/Input/Input.tsx
+++ b/frontend/src/components/v3/generic/Input/Input.tsx
@@ -1,7 +1,8 @@
-import { forwardRef } from "react";
+import { forwardRef, useId, useImperativeHandle, useRef } from "react";
import { cva, type VariantProps } from "cva";
import { cn } from "../../utils";
+import { Kbd } from "../Kbd";
const inputVariants = cva(
"h-9 w-full min-w-0 rounded-md border border-border bg-transparent px-2.5 text-sm text-foreground shadow-xs file:inline-flex file:h-7 file:border-0 file:bg-transparent file:text-sm file:font-medium file:text-foreground placeholder:text-muted [&[readonly]]:border-border/50 disabled:pointer-events-none disabled:border-border/50 disabled:text-foreground/50 disabled:placeholder:text-muted/50 selection:bg-foreground selection:text-background",
@@ -21,7 +22,11 @@ const inputVariants = cva(
);
type InputProps = React.ComponentProps<"input"> &
- VariantProps<typeof inputVariants> & { isError?: boolean; "data-1p-ignore"?: boolean };
+ VariantProps<typeof inputVariants> & {
+ isError?: boolean;
+ "data-1p-ignore"?: boolean;
+ suggestedValue?: string;
+ };
const Input = forwardRef<HTMLInputElement, InputProps>(
(
@@ -34,23 +39,86 @@ const Input = forwardRef<HTMLInputElement, InputProps>(
"data-1p-ignore": ignorePasswordManager = !/email|password|username/i.test(
autoComplete ?? ""
),
+ suggestedValue,
+ placeholder,
+ onKeyDown,
+ "aria-describedby": ariaDescribedBy,
...props
},
ref
) => {
- return (
+ const inputRef = useRef<HTMLInputElement>(null);
+ const suggestionHintId = useId();
+ useImperativeHandle(ref, () => inputRef.current as HTMLInputElement);
+
+ const acceptSuggestion = () => {
+ const el = inputRef.current;
+ if (!el || !suggestedValue) return;
+ Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set?.call(
+ el,
+ suggestedValue
+ );
+ el.dispatchEvent(new Event("input", { bubbles: true }));
+ };
+
+ const input = (
<input
- ref={ref}
+ ref={inputRef}
type={type}
data-slot="input"
data-variant={variant}
autoComplete={autoComplete}
data-1p-ignore={ignorePasswordManager || undefined}
- className={cn(inputVariants({ variant, className }))}
+ className={cn(inputVariants({ variant, className }), suggestedValue && "peer pr-14")}
aria-invalid={isError}
+ placeholder={suggestedValue || placeholder}
+ aria-describedby={
+ suggestedValue && !props.readOnly && !props.disabled
+ ? [ariaDescribedBy, suggestionHintId].filter(Boolean).join(" ")
+ : ariaDescribedBy
+ }
+ onKeyDown={(e) => {
+ onKeyDown?.(e);
+ if (
+ !suggestedValue ||
+ e.defaultPrevented ||
+ e.key !== "Tab" ||
+ e.shiftKey ||
+ e.currentTarget.readOnly ||
+ e.currentTarget.value
+ )
+ return;
+ e.preventDefault();
+ acceptSuggestion();
+ }}
{...props}
/>
);
+
+ if (!suggestedValue) return input;
+
+ return (
+ <div data-slot="input-suggestion" className="relative w-full min-w-0">
+ {input}
+ <button
+ type="button"
+ tabIndex={-1}
+ aria-label={`Use suggested value ${suggestedValue}`}
+ title="Use suggested value"
+ className="absolute top-1/2 right-2 hidden -translate-y-1/2 cursor-pointer peer-placeholder-shown:flex peer-read-only:hidden peer-disabled:hidden"
+ onMouseDown={(e) => e.preventDefault()}
+ onClick={() => {
+ acceptSuggestion();
+ inputRef.current?.focus();
+ }}
+ >
+ <Kbd>Tab</Kbd>
+ </button>
+ <span id={suggestionHintId} className="sr-only">
+ When the field is empty, press Tab to use the suggested value.
+ </span>
+ </div>
+ );
}
);