@@ -38,32 +38,35 @@ import subscribeTo from 'lib/subscribeTo';
38
38
import * as ColumnPreferences from 'lib/ColumnPreferences' ;
39
39
import * as ClassPreferences from 'lib/ClassPreferences' ;
40
40
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' ;
42
45
import generatePath from 'lib/generatePath' ;
43
46
import { withRouter } from 'lib/withRouter' ;
44
47
import { get } from 'lib/AJAX' ;
45
48
import BrowserFooter from './BrowserFooter.react' ;
46
49
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 ) ;
53
52
React . useEffect ( ( ) => {
54
53
if ( ! when ) {
55
- previousLocation . current = location ;
56
54
return ;
57
55
}
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 ( ) ;
63
60
}
64
- }
65
- } , [ location , when , navigate ] ) ;
61
+ } ) ;
62
+ return unblock ;
63
+ } , [ navigator , message , when ] ) ;
64
+ }
66
65
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 ) ;
67
70
useBeforeUnload (
68
71
React . useCallback (
69
72
event => {
0 commit comments