Skip to content

Commit 982523c

Browse files
committed
fix: show confirmation when leaving with selected rows
1 parent 78d82fc commit 982523c

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

src/dashboard/Data/Browser/Browser.react.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,32 +38,35 @@ import subscribeTo from 'lib/subscribeTo';
3838
import * as ColumnPreferences from 'lib/ColumnPreferences';
3939
import * as ClassPreferences from 'lib/ClassPreferences';
4040
import { Helmet } from 'react-helmet';
41-
import { useBeforeUnload, useLocation, useNavigate } from 'react-router-dom';
41+
import {
42+
useBeforeUnload,
43+
UNSAFE_NavigationContext,
44+
} from 'react-router-dom';
4245
import generatePath from 'lib/generatePath';
4346
import { withRouter } from 'lib/withRouter';
4447
import { get } from 'lib/AJAX';
4548
import BrowserFooter from './BrowserFooter.react';
4649

47-
function SelectedRowsNavigationPrompt({ when }) {
48-
const location = useLocation();
49-
const navigate = useNavigate();
50-
const previousLocation = React.useRef(location);
51-
const message = 'There are selected rows. Are you sure you want to leave this page?';
52-
50+
function usePrompt(message, when) {
51+
const { navigator } = React.useContext(UNSAFE_NavigationContext);
5352
React.useEffect(() => {
5453
if (!when) {
55-
previousLocation.current = location;
5654
return;
5755
}
58-
if (location !== previousLocation.current) {
59-
if (!window.confirm(message)) {
60-
navigate(-1);
61-
} else {
62-
previousLocation.current = location;
56+
const unblock = navigator.block(tx => {
57+
if (window.confirm(message)) {
58+
unblock();
59+
tx.retry();
6360
}
64-
}
65-
}, [location, when, navigate]);
61+
});
62+
return unblock;
63+
}, [navigator, message, when]);
64+
}
6665

66+
function SelectedRowsNavigationPrompt({ when }) {
67+
const message =
68+
'There are selected rows. Are you sure you want to leave this page?';
69+
usePrompt(message, when);
6770
useBeforeUnload(
6871
React.useCallback(
6972
event => {

0 commit comments

Comments
 (0)