@@ -38,17 +38,53 @@ 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 { unstable_usePrompt as usePrompt , useBeforeUnload } from 'react-router-dom' ;
41
+ import {
42
+ UNSAFE_NavigationContext ,
43
+ useBeforeUnload ,
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
50
+ function useBlocker ( blocker , when = true ) {
51
+ const { navigator } = React . useContext ( UNSAFE_NavigationContext ) ;
52
+
53
+ React . useEffect ( ( ) => {
54
+ if ( ! when ) {
55
+ return ;
56
+ }
57
+
58
+ const unblock = navigator . block ( tx => {
59
+ const autoUnblockingTx = {
60
+ ...tx ,
61
+ retry ( ) {
62
+ unblock ( ) ;
63
+ tx . retry ( ) ;
64
+ }
65
+ } ;
66
+ blocker ( autoUnblockingTx ) ;
67
+ } ) ;
68
+
69
+ return unblock ;
70
+ } , [ navigator , blocker , when ] ) ;
71
+ }
72
+
73
+ function usePrompt ( message , when = true ) {
74
+ const blocker = React . useCallback (
75
+ tx => {
76
+ if ( window . confirm ( message ) ) {
77
+ tx . retry ( ) ;
78
+ }
79
+ } ,
80
+ [ message ]
81
+ ) ;
82
+
83
+ useBlocker ( blocker , when ) ;
84
+ }
85
+
47
86
function SelectedRowsNavigationPrompt ( { when } ) {
48
- usePrompt ( {
49
- when,
50
- message : 'There are selected rows. Are you sure you want to leave this page?'
51
- } ) ;
87
+ usePrompt ( 'There are selected rows. Are you sure you want to leave this page?' , when ) ;
52
88
useBeforeUnload (
53
89
React . useCallback (
54
90
event => {
0 commit comments