|
1 | 1 | 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'; |
8 | 4 | import { useDispatch, useSelector } from 'react-redux';
|
9 | 5 | import { useTranslation } from 'react-i18next';
|
10 | 6 | import { Helmet } from 'react-helmet';
|
@@ -58,52 +54,27 @@ function isOverlay(pathname) {
|
58 | 54 |
|
59 | 55 | function WarnIfUnsavedChanges() {
|
60 | 56 | const hasUnsavedChanges = useSelector((state) => state.ide.unsavedChanges);
|
| 57 | + |
61 | 58 | const { t } = useTranslation();
|
62 | 59 |
|
63 | 60 | const currentLocation = useLocation();
|
64 | 61 |
|
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 |
84 | 73 | }
|
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 | + /> |
102 | 77 | );
|
103 |
| - |
104 |
| - useBeforeUnload(handleUnload); |
105 |
| - |
106 |
| - return null; |
107 | 78 | }
|
108 | 79 |
|
109 | 80 | export const CmControllerContext = React.createContext({});
|
|
0 commit comments