Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.9 KB
Raw Blame
import { useMutation } from '@tanstack/react-query';
import { t } from 'i18next';
import { TriangleAlert } from 'lucide-react';
import { useState } from 'react';
import { toast } from 'sonner';

import {
  TypeToConfirm,
  useTypeToConfirm,
} from '@/components/custom/type-to-confirm';
import { Alert, AlertDescription } from '@/components/ui/alert';
import { Button } from '@/components/ui/button';
import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogFooter,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog';
import { AdminControl, adminControl } from '@/lib/admin-control';

interface ConfirmationDeleteDialogProps {
  title: string;
  message: React.ReactNode | string;
  mutationFn: () => Promise<void>;
  entityName: string;
  children?: React.ReactNode;
  open?: boolean;
  isDanger?: boolean;
  buttonText?: string;
  onOpenChange?: (open: boolean) => void;
  showToast?: boolean;
  onError?: (error: Error) => void;
  warning?: React.ReactNode | string;
  confirmDisabled?: boolean;
  confirmText?: string;
  controlId?: AdminControl;
  contentClassName?: string;
  keepOpenOnOutsideClick?: boolean;
  confirmVariant?: 'destructive' | 'default';
}

export const ConfirmationDeleteDialog = ({
  title,
  message,
  mutationFn,
  showToast,
  isDanger,
  entityName,
  buttonText,
  children,
  open,
  onError,
  onOpenChange,
  warning,
  confirmDisabled,
  confirmText,
  controlId,
  contentClassName,
  keepOpenOnOutsideClick = false,
  confirmVariant = 'destructive',
}: ConfirmationDeleteDialogProps) => {
  const [isControlled] = useState(
    open !== undefined && onOpenChange !== undefined,
  );
  const [isUncontrolledOpen, setIsUncontrolledOpen] = useState(false);
  const isOpen = isControlled ? open : isUncontrolledOpen;
  const typed = useTypeToConfirm({ open: !!isOpen, text: confirmText });

  const { mutate, isPending } = useMutation({
    mutationFn,
    onSuccess: () => {
      handleClose();
      if (showToast) {
        toast.success(t('Removed {entityName}', { entityName }));
      }
    },
    onError,
  });

  const handleClose = () => {
    if (isControlled) {
      onOpenChange?.(false);
    } else {
      setIsUncontrolledOpen(false);
    }
  };

  return (
    <Dialog
      open={isOpen}
      onOpenChange={isControlled ? onOpenChange : setIsUncontrolledOpen}
    >
      {children && <DialogTrigger asChild>{children}</DialogTrigger>}

      <DialogContent
        className={contentClassName}
        onClick={(e) => e.stopPropagation()}
        onInteractOutside={(event) => {
          if (keepOpenOnOutsideClick) {
            event.preventDefault();
          }
        }}
      >
        <DialogHeader className="min-w-0">
          <DialogTitle>{title}</DialogTitle>
          <DialogDescription className="pt-2 break-words">
            {message}
          </DialogDescription>
        </DialogHeader>
        {warning && (
          <Alert variant="warning">
            <TriangleAlert className="h-4 w-4" />
            <AlertDescription>{warning}</AlertDescription>
          </Alert>
        )}
        {confirmText && !confirmDisabled && (
          <TypeToConfirm
            id="delete-confirm-text"
            text={confirmText}
            value={typed.value}
            onChange={typed.setValue}
          />
        )}
        <DialogFooter className="mt-3">
          <Button
            variant="outline"
            disabled={isPending}
            onClick={() => handleClose()}
          >
            {t('Cancel')}
          </Button>
          <Button
            variant={confirmVariant}
            loading={isPending}
            disabled={confirmDisabled || !typed.confirmed}
            onClick={() => mutate()}
            {...adminControl(controlId)}
          >
            {isDanger && <TriangleAlert className="size-4 mr-2" />}
            {buttonText || t('Remove')}
          </Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
  );
};