1+ import React , { useEffect , useMemo } from 'react'
2+ import {
3+ Route ,
4+ Switch ,
5+ Redirect
6+ } from 'react-router-dom'
7+ import { ApolloProvider } from '@apollo/client'
8+ import { useWeb3Context , Connectors } from 'web3-react'
9+ import getNetworkEnv from 'utils/network-env'
10+
11+ import loadable from '@loadable/component'
12+ import ErrorPage from 'pages/error-page'
13+ import NoWeb3Detected from 'pages/no-web3'
14+ import Loading from 'components/loading'
15+ import connectors from 'config/connectors'
16+ import { DEFAULT_NETWORK } from 'config/networks'
17+ import { hexlify } from 'utils/string'
18+ import usePathValidation from 'hooks/use-path-validation'
19+ import useGraphQLClient from 'hooks/use-graphql-client'
20+ import { Web3ContextCurate } from 'types/web3-cotext'
21+
22+ const { Connector } = Connectors ;
23+
24+ const ItemsRouter = loadable (
25+ ( ) => import ( /* webpackPrefetch: true */ 'pages/items-router' ) ,
26+ { fallback : < Loading /> }
27+ )
28+
29+ const ItemDetailsRouter = loadable (
30+ ( ) => import ( /* webpackPrefetch: true */ 'pages/item-details-router' ) ,
31+ { fallback : < Loading /> }
32+ )
33+
34+ const Factory = loadable (
35+ ( ) => import ( /* webpackPrefetch: true */ 'pages/factory/index' ) ,
36+ { fallback : < Loading /> }
37+ )
38+
39+ const ClassicFactory = loadable (
40+ ( ) => import ( /* webpackPrefetch: true */ 'pages/factory-classic/index' ) ,
41+ { fallback : < Loading /> }
42+ )
43+
44+
45+ const AppRouter = ( ) => {
46+ const { networkId, error } : Web3ContextCurate = useWeb3Context ( ) ;
47+ const isUnsupported = useMemo ( ( ) => error ?. code === Connector . errorCodes . UNSUPPORTED_NETWORK , [ error ] )
48+ const tcrAddress = getNetworkEnv ( 'REACT_APP_DEFAULT_TCR_ADDRESSES' , networkId as number ) ;
49+ const [ pathResolved , invalidTcrAddr ] = usePathValidation ( )
50+ const client = useGraphQLClient ( networkId )
51+
52+ useEffect ( ( ) => {
53+ if ( isUnsupported && window . ethereum ) {
54+ const chainIdTokens = window . location . pathname . match ( / \/ t c r \/ ( \d + ) \/ / ) ;
55+ const chainId = hexlify ( chainIdTokens && chainIdTokens ?. length > 1 ? chainIdTokens [ 1 ] : DEFAULT_NETWORK ) ;
56+
57+ window . ethereum . request ( {
58+ method : 'wallet_switchEthereumChain' ,
59+ params : [ { chainId } ]
60+ } ) ;
61+ }
62+ } , [ isUnsupported ] ) ;
63+
64+ if ( Object . entries ( connectors ) . length === 0 )
65+ return < NoWeb3Detected />
66+
67+ if ( isUnsupported && error ) {
68+ return (
69+ < ErrorPage
70+ code = { ' ' }
71+ title = { error . code as string }
72+ message = { error . message }
73+ tip = {
74+ < >
75+ < p > Switching network to supported one</ p >
76+ < Loading />
77+ </ >
78+ }
79+ />
80+ )
81+ } else if ( ! networkId || ! pathResolved ) {
82+ return < Loading />
83+ } else if ( invalidTcrAddr || ! client ) {
84+ return < ErrorPage />
85+ }
86+
87+ return (
88+ < ApolloProvider client = { client } >
89+ < Switch >
90+ < Route path = "/tcr/:chainId/:tcrAddress/:itemID" component = { ItemDetailsRouter } />
91+ < Route path = "/tcr/:chainId/:tcrAddress" component = { ItemsRouter } />
92+ < Route path = "/factory" exact component = { Factory } />
93+ < Route path = "/factory-classic" exact component = { ClassicFactory } />
94+ < Redirect from = "/" exact to = { `/tcr/${ networkId } /${ tcrAddress } ` } />
95+ < Route path = "*" exact component = { ErrorPage } />
96+ </ Switch >
97+ </ ApolloProvider >
98+ )
99+ }
100+
101+ export default AppRouter ;
0 commit comments