@@ -3,12 +3,32 @@ import { useHistory } from 'react-router'
33import { TCR_EXISTENCE_TEST } from 'utils/graphql'
44import { ApolloClient , InMemoryCache } from '@apollo/client'
55import { 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
710const 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 = / \/ t c r \/ ( \d + ) \/ 0 x /
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