Skip to content

Commit 822f247

Browse files
committed
unsaved changes old
1 parent 00724fd commit 822f247

File tree

1 file changed

+17
-46
lines changed

1 file changed

+17
-46
lines changed

client/modules/IDE/pages/IDEView.jsx

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import PropTypes from 'prop-types';
2-
import React, { useCallback, useEffect, useRef, useState } from 'react';
3-
import {
4-
useBeforeUnload,
5-
unstable_useBlocker as useBlocker,
6-
useLocation
7-
} from 'react-router-dom';
2+
import React, { useEffect, useRef, useState } from 'react';
3+
import { useLocation, Prompt } from 'react-router-dom';
84
import { useDispatch, useSelector } from 'react-redux';
95
import { useTranslation } from 'react-i18next';
106
import { Helmet } from 'react-helmet';
@@ -58,52 +54,27 @@ function isOverlay(pathname) {
5854

5955
function WarnIfUnsavedChanges() {
6056
const hasUnsavedChanges = useSelector((state) => state.ide.unsavedChanges);
57+
6158
const { t } = useTranslation();
6259

6360
const currentLocation = useLocation();
6461

65-
// blocker handles internal navigation between pages.
66-
const blocker = useBlocker(hasUnsavedChanges);
67-
68-
useEffect(() => {
69-
if (blocker.state === 'blocked') {
70-
const nextLocation = blocker.location;
71-
if (
72-
isAuth(nextLocation.pathname) ||
73-
isAuth(currentLocation.pathname) ||
74-
isOverlay(nextLocation.pathname) ||
75-
isOverlay(currentLocation.pathname)
76-
) {
77-
blocker.proceed();
78-
} else {
79-
const didConfirm = window.confirm(t('Nav.WarningUnsavedChanges'));
80-
if (didConfirm) {
81-
blocker.proceed();
82-
} else {
83-
blocker.reset();
62+
return (
63+
<Prompt
64+
when={hasUnsavedChanges}
65+
message={(nextLocation) => {
66+
if (
67+
isAuth(nextLocation.pathname) ||
68+
isAuth(currentLocation.pathname) ||
69+
isOverlay(nextLocation.pathname) ||
70+
isOverlay(currentLocation.pathname)
71+
) {
72+
return true; // allow navigation
8473
}
85-
}
86-
}
87-
}, [blocker, currentLocation.pathname, t, hasUnsavedChanges]);
88-
89-
// beforeunload handles closing or refreshing the window.
90-
const handleUnload = useCallback(
91-
(e) => {
92-
if (hasUnsavedChanges) {
93-
// See: https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event#browser_compatibility
94-
e.preventDefault();
95-
const confirmationMessage = t('Nav.WarningUnsavedChanges');
96-
e.returnValue = confirmationMessage;
97-
return confirmationMessage;
98-
}
99-
return null;
100-
},
101-
[t, hasUnsavedChanges]
74+
return t('Nav.WarningUnsavedChanges');
75+
}}
76+
/>
10277
);
103-
104-
useBeforeUnload(handleUnload);
105-
106-
return null;
10778
}
10879

10980
export const CmControllerContext = React.createContext({});

0 commit comments

Comments
 (0)