Skip to content

Commit 1852915

Browse files
authored
Merge pull request #208 from oasisprotocol/lw/block-soft-nav
move: Block react-router navigation while withdrawing/depositing
2 parents 4e0992c + 281a039 commit 1852915

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

.changelog/208.bugfix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
move: Block react-router navigation while withdrawing/depositing

move/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"bignumber.js": "^9.1.2",
2828
"fathom-client": "^3.7.2",
2929
"react": "^18.2.0",
30+
"react-router-dom": "^6.22.2",
3031
"tweetnacl": "^1.0.3",
3132
"viem": "^2.23.1",
3233
"wagmi": "^2.14.11"
Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { useState } from 'react'
1+
import { useEffect, useState } from 'react'
2+
import { useBlocker } from 'react-router-dom'
23

34
let globalIsBlocking = false // If useBlockNavigatingAway gets destroyed, load state from this.
45

@@ -10,17 +11,36 @@ const beforeUnloadHandler = (event: BeforeUnloadEvent) => {
1011

1112
export function useBlockNavigatingAway() {
1213
const [reactiveIsBlocking, setReactiveIsBlocking] = useState(globalIsBlocking)
14+
const reactRouterBlocker = useBlocker(() => reactiveIsBlocking) // Blocks soft navigation away
15+
// TODO can't have two blockers
16+
17+
function blockNavigatingAway() {
18+
window.addEventListener('beforeunload', beforeUnloadHandler) // Blocks hard navigation away
19+
setReactiveIsBlocking(true)
20+
globalIsBlocking = true
21+
}
22+
23+
function allowNavigatingAway() {
24+
window.removeEventListener('beforeunload', beforeUnloadHandler)
25+
setReactiveIsBlocking(false)
26+
globalIsBlocking = false
27+
}
28+
29+
useEffect(() => {
30+
if (reactRouterBlocker.state === 'blocked') {
31+
if (window.confirm('You are navigating away. Progress you made may not be saved.')) {
32+
reactRouterBlocker.proceed()
33+
allowNavigatingAway()
34+
} else {
35+
reactRouterBlocker.reset()
36+
}
37+
}
38+
// eslint-disable-next-line react-hooks/exhaustive-deps
39+
}, [reactRouterBlocker.state])
40+
1341
return {
1442
isBlockingNavigatingAway: reactiveIsBlocking,
15-
blockNavigatingAway: () => {
16-
window.addEventListener('beforeunload', beforeUnloadHandler)
17-
setReactiveIsBlocking(true)
18-
globalIsBlocking = true
19-
},
20-
allowNavigatingAway: () => {
21-
window.removeEventListener('beforeunload', beforeUnloadHandler)
22-
setReactiveIsBlocking(false)
23-
globalIsBlocking = false
24-
},
43+
blockNavigatingAway,
44+
allowNavigatingAway,
2545
}
2646
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

stake/src/hoc/withConnectedWallet.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const withConnectedWallet = (WrappedComponent: FC) => {
1414

1515
useEffect(() => {
1616
if (isSupportedNetwork && isConnected) {
17-
navigate('/stake/dashboard')
17+
navigate('/stake/dashboard', { replace: true })
1818
}
1919
}, [isSupportedNetwork, isConnected, navigate])
2020

0 commit comments

Comments
 (0)