Addressed PR suggestions

This commit is contained in:
Carlos Monastyrski
2025-07-18 15:32:22 -03:00
parent cf6b9d8905
commit a064fae94e
5 changed files with 142 additions and 57 deletions
+16 -6
View File
@@ -1,24 +1,34 @@
import { useCallback } from "react";
import { useBlocker } from "@tanstack/react-router";
import { BatchContext, useBatchModeActions } from "@app/pages/secret-manager/SecretDashboardPage/SecretMainPage.store";
type TNavigationBlockerReturn = {
isBlocked: boolean;
};
export const useNavigationBlocker = (
shouldBlock: boolean,
message: string = "Are you sure you want to leave? You may have unsaved changes."
): TNavigationBlockerReturn => {
export const useNavigationBlocker = ({
shouldBlock = false,
message = "Are you sure you want to leave? You may have unsaved changes.",
context
}: {
shouldBlock: boolean;
message: string;
context: BatchContext;
}): TNavigationBlockerReturn => {
const { clearAllPendingChanges } = useBatchModeActions();
const blockerFn = useCallback(() => {
if (!shouldBlock) return false;
// eslint-disable-next-line no-alert
const confirmed = window.confirm(message);
if (confirmed) {
clearAllPendingChanges(context);
}
return !confirmed;
}, [shouldBlock, message]);
}, [shouldBlock, message, context]);
useBlocker(blockerFn);
useBlocker(blockerFn, shouldBlock);
return {
isBlocked: shouldBlock