Skip to content

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

2.5 KB
Raw Blame
import { ReactNode, useState } from "react";

import {
  Alert,
  AlertDescription,
  AlertDialog,
  AlertDialogAction,
  AlertDialogCancel,
  AlertDialogConfirmationField,
  AlertDialogContent,
  AlertDialogDescription,
  AlertDialogFooter,
  AlertDialogHeader,
  AlertDialogTitle
} from "@app/components/v3";

type Props = {
  open: boolean;
  onOpenChange: (open: boolean) => void;
  title: string;
  description: ReactNode;
  confirmationText: string;
  actionLabel: string;
  onConfirm: () => Promise<void>;
  isDisabled?: boolean;
  descriptionAsAlert?: boolean;
  descriptionAlertVariant?: "warning" | "danger";
};

export const IdentityActionConfirmationDialog = ({
  open,
  onOpenChange,
  title,
  description,
  confirmationText,
  actionLabel,
  onConfirm,
  isDisabled,
  descriptionAsAlert = false,
  descriptionAlertVariant = "warning"
}: Props) => {
  const [isPending, setIsPending] = useState(false);

  const handleConfirm = async () => {
    if (isPending || isDisabled) return;

    setIsPending(true);
    try {
      await onConfirm();
    } catch {
      // Mutation errors are surfaced by the shared query mutation handler. Keep the dialog open so
      // the user can retry without re-entering the confirmation text.
    } finally {
      setIsPending(false);
    }
  };

  return (
    <AlertDialog
      isPending={isPending}
      open={open}
      confirmationValue={confirmationText}
      onOpenChange={onOpenChange}
    >
      <AlertDialogContent>
        <AlertDialogHeader>
          <AlertDialogTitle>{title}</AlertDialogTitle>
          {descriptionAsAlert ? (
            <AlertDialogDescription asChild>
              <Alert variant={descriptionAlertVariant} appearance="borderless">
                <AlertDescription>{description}</AlertDescription>
              </Alert>
            </AlertDialogDescription>
          ) : (
            <AlertDialogDescription>{description}</AlertDialogDescription>
          )}
        </AlertDialogHeader>
        <AlertDialogConfirmationField onConfirm={() => handleConfirm().catch(() => undefined)} />
        <AlertDialogFooter>
          <AlertDialogCancel>Cancel</AlertDialogCancel>
          <AlertDialogAction
            type="button"
            variant="danger"
            isDisabled={isDisabled}
            onClick={(event) => {
              event.preventDefault();
              handleConfirm().catch(() => undefined);
            }}
          >
            {actionLabel}
          </AlertDialogAction>
        </AlertDialogFooter>
      </AlertDialogContent>
    </AlertDialog>
  );
};