@@ -38,32 +38,35 @@ import subscribeTo from 'lib/subscribeTo';
3838import * as ColumnPreferences from 'lib/ColumnPreferences' ;
3939import * as ClassPreferences from 'lib/ClassPreferences' ;
4040import { 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' ;
4245import generatePath from 'lib/generatePath' ;
4346import { withRouter } from 'lib/withRouter' ;
4447import { get } from 'lib/AJAX' ;
4548import 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