Skip to content

Commit 54145bb

Browse files
author
devofficer
committed
fix: usePathValidation incognito issue
1 parent 72efe46 commit 54145bb

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/hooks/use-path-validation.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,32 @@ import { useHistory } from 'react-router'
33
import { TCR_EXISTENCE_TEST } from 'utils/graphql'
44
import { ApolloClient, InMemoryCache } from '@apollo/client'
55
import { HttpLink } from '@apollo/client/link/http'
6+
import { useWeb3Context } from 'web3-react'
7+
import { SAVED_NETWORK_KEY } from 'utils/string'
8+
import { DEFAULT_NETWORK } from 'config/networks'
69

710
const usePathValidation = () => {
811
const history = useHistory()
12+
const { networkId, account } = useWeb3Context()
13+
914
const [pathResolved, setPathResolved] = useState<boolean>(false)
1015
const [invalidTcrAddr, setInvalidTcrAddr] = useState<boolean>(false)
1116

17+
useEffect(() => {
18+
if (networkId === undefined) return
19+
if (account) return // their provider will prompt to change it
20+
const pathname = history.location.pathname
21+
const newPathRegex = /\/tcr\/(\d+)\/0x/
22+
if (!newPathRegex.test(pathname)) return // let it redirect to new path first
23+
const matches = pathname.match(newPathRegex)
24+
const chainId = matches ? matches[1] : DEFAULT_NETWORK
25+
const pathChainId = Number(chainId)
26+
if (networkId !== pathChainId) {
27+
localStorage.setItem(SAVED_NETWORK_KEY, pathChainId.toString())
28+
window.location.reload()
29+
}
30+
}, [history.location.pathname, networkId, account])
31+
1232
useEffect(() => {
1333
const checkPathValidation = async () => {
1434
const pathname = history.location.pathname

0 commit comments

Comments
 (0)