Only notify with actual changes (#1173)
This commit is contained in:
@@ -1,13 +1,14 @@
|
|||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
|
|
||||||
function useShouldNotifyWhenClosingTab() {
|
function useShouldNotifyWhenClosingTab(shouldNotify: boolean) {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function f(event: BeforeUnloadEvent) {
|
function f(event: BeforeUnloadEvent) {
|
||||||
// this function is here to have a stable reference only
|
// this function is here to have a stable reference only
|
||||||
|
if (!shouldNotify) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
// Recommended
|
// Recommended
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
|
|
||||||
// Included for legacy support, e.g. Chrome/Edge < 119
|
// Included for legacy support, e.g. Chrome/Edge < 119
|
||||||
// refer to https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
|
// refer to https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event
|
||||||
event.returnValue = true;
|
event.returnValue = true;
|
||||||
@@ -18,7 +19,7 @@ function useShouldNotifyWhenClosingTab() {
|
|||||||
return () => {
|
return () => {
|
||||||
window.removeEventListener("beforeunload", f);
|
window.removeEventListener("beforeunload", f);
|
||||||
};
|
};
|
||||||
}, []);
|
}, [shouldNotify]);
|
||||||
}
|
}
|
||||||
|
|
||||||
export { useShouldNotifyWhenClosingTab };
|
export { useShouldNotifyWhenClosingTab };
|
||||||
|
|||||||
@@ -191,7 +191,7 @@ function FlowRenderer({
|
|||||||
const [title, setTitle] = useState(initialTitle);
|
const [title, setTitle] = useState(initialTitle);
|
||||||
const nodesInitialized = useNodesInitialized();
|
const nodesInitialized = useNodesInitialized();
|
||||||
const { hasChanges, setHasChanges } = useWorkflowHasChangesStore();
|
const { hasChanges, setHasChanges } = useWorkflowHasChangesStore();
|
||||||
useShouldNotifyWhenClosingTab();
|
useShouldNotifyWhenClosingTab(hasChanges);
|
||||||
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
|
const blocker = useBlocker(({ currentLocation, nextLocation }) => {
|
||||||
return hasChanges && nextLocation.pathname !== currentLocation.pathname;
|
return hasChanges && nextLocation.pathname !== currentLocation.pathname;
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user