Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
import { ReactNode, useEffect, useRef, useState } from "react";
import { Button } from "../../generic/Button";
import { CardDescription, CardHeader, CardTitle } from "../../generic/Card";
import { FieldError } from "../../generic/Field";
import { VerificationCodeInput } from "./VerificationCodeInput";
type VerificationCodeHeaderProps = {
action?: ReactNode;
description?: ReactNode;
recipient?: string;
title: ReactNode;
};
export const VerificationCodeHeader = ({
action,
description,
recipient,
title
}: VerificationCodeHeaderProps) => (
<CardHeader className="mb-6 gap-2">
{recipient ? (
<>
<CardDescription className="ml-0.5 text-base">{title}</CardDescription>
<div className="flex min-w-0 items-baseline justify-between gap-3">
<div
aria-label={recipient}
className="ml-0.5 min-w-0 flex-1 truncate font-sans text-2xl font-normal text-foreground"
title={recipient}
>
{recipient}
</div>
{action && <div className="shrink-0">{action}</div>}
</div>
{description && <CardDescription className="ml-0.5">{description}</CardDescription>}
</>
) : (
<>
<CardTitle className="font-alliance text-2xl font-normal">{title}</CardTitle>
{description && <CardDescription className="text-base">{description}</CardDescription>}
</>
)}
</CardHeader>
);
type VerificationCodeFormProps = {
children?: ReactNode;
error?: ReactNode;
fields?: number;
isDisabled?: boolean;
isPending?: boolean;
name: string;
onChange: (value: string, reason: "input" | "reset") => void;
onSubmit: () => void | Promise<void>;
submitVariant?: React.ComponentProps<typeof Button>["variant"];
value: string;
};
export const VerificationCodeForm = ({
children,
error,
fields = 6,
isDisabled,
isPending,
name,
onChange,
onSubmit,
submitVariant = "project",
value
}: VerificationCodeFormProps) => {
const isComplete = value.trim().length === fields;
const hasError = Boolean(error);
const [hasChangedSinceError, setHasChangedSinceError] = useState(false);
const [hasAttemptedVerification, setHasAttemptedVerification] = useState(false);
const [hasFailedVerification, setHasFailedVerification] = useState(false);
const onSubmitRef = useRef(onSubmit);
const onChangeRef = useRef(onChange);
useEffect(() => {
onSubmitRef.current = onSubmit;
onChangeRef.current = onChange;
}, [onChange, onSubmit]);
useEffect(() => {
if (hasError && !isPending) {
setHasChangedSinceError(false);
setHasFailedVerification(true);
onChangeRef.current("", "reset");
}
}, [hasError, isPending]);
useEffect(() => {
if (hasAttemptedVerification || !isComplete || isDisabled || isPending) return;
setHasAttemptedVerification(true);
onSubmitRef.current();
}, [hasAttemptedVerification, isComplete, isDisabled, isPending]);
const handleChange = (nextValue: string) => {
if (hasError) setHasChangedSinceError(true);
onChange(nextValue, "input");
};
return (
<form
className="flex flex-col gap-5"
onSubmit={(event) => {
event.preventDefault();
if (!isComplete || isDisabled || isPending) return;
setHasAttemptedVerification(true);
onSubmit();
}}
>
<div className="flex flex-col gap-2">
<VerificationCodeInput
key={`${name}-${fields}`}
name={name}
fields={fields}
onChange={handleChange}
value={value}
isError={hasError && !isPending && !hasChangedSinceError}
/>
{error && <FieldError className="mt-0">{error}</FieldError>}
</div>
<Button
type="submit"
variant={submitVariant}
size="lg"
isFullWidth
isPending={isPending}
isDisabled={!isComplete || isDisabled || isPending}
aria-label={isPending ? "Verifying code" : undefined}
>
{hasFailedVerification ? "Try again" : "Continue"}
</Button>
{children}
</form>
);
};