Skip to content

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

1.7 KB
Raw Blame
import { ReactNode } from "react";
import { Control, Controller } from "react-hook-form";

import { Field, FieldDescription, FieldLabel, Input } from "@app/components/v3";

import { PolicyRowMessage } from "./PolicyRowMessage";

type Props = {
  control: Control<any>;
  label: string;
  description?: ReactNode;
  className?: string;
  /** The policy's ceiling on validity, shown until something has to be corrected. */
  hint?: string[];
  /** How the entered duration breaks the policy. */
  policyError?: string;
  /** Policy findings stay hidden until the requester tries to leave the step. */
  revealPolicyError?: boolean;
  isReadOnly?: boolean;
};

/**
 * The validity field and the policy's ceiling on it. The schema's own error takes precedence, so a
 * missing value reads as missing rather than as a policy violation.
 */
export const ValidityField = ({
  control,
  label,
  description,
  className,
  hint,
  policyError,
  revealPolicyError,
  isReadOnly
}: Props) => (
  <Controller
    control={control}
    name="ttl"
    render={({ field, fieldState: { error } }) => {
      const shownError = error?.message ?? (revealPolicyError ? policyError : undefined);

      return (
        <Field className={className}>
          <FieldLabel>
            {label} <span className="text-danger">*</span>
          </FieldLabel>
          <Input
            {...field}
            readOnly={isReadOnly}
            placeholder="30d, 1y, 8760h"
            isError={Boolean(shownError)}
          />
          {description && <FieldDescription>{description}</FieldDescription>}
          {shownError && <PolicyRowMessage isError lines={[shownError]} />}
          {!shownError && hint && <PolicyRowMessage lines={hint} />}
        </Field>
      );
    }}
  />
);