Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

976 B
Raw Blame
import { useDisclosure } from "@mantine/hooks";
import { useCallback } from "react";

type UseActionFormUnsavedLeaveConfirmationProps = {
  shouldShowLeaveConfirmation: () => boolean;
  onClose: () => void;
};

export function useActionFormUnsavedLeaveConfirmation({
  shouldShowLeaveConfirmation,
  onClose,
}: UseActionFormUnsavedLeaveConfirmationProps) {
  const [showLeaveConfirmation, leaveConfirmationHandlers] =
    useDisclosure(false);

  const handleClose = useCallback(() => {
    if (shouldShowLeaveConfirmation()) {
      leaveConfirmationHandlers.open();
      return;
    }

    onClose();
  }, [leaveConfirmationHandlers, onClose, shouldShowLeaveConfirmation]);

  const handleLeaveConfirmation = useCallback(() => {
    leaveConfirmationHandlers.close();
    onClose();
  }, [leaveConfirmationHandlers, onClose]);

  return {
    showLeaveConfirmation,
    handleContinue: leaveConfirmationHandlers.close,
    handleClose,
    handleLeaveConfirmation,
  };
}