1- import React , { useEffect , useMemo , useState } from 'react'
1+ import React , { useEffect , useMemo } from 'react'
22import {
33 Route ,
44 Switch ,
55 Redirect
66} from 'react-router-dom'
7- import { useHistory } from 'react-router'
8- import { ApolloClient , ApolloProvider , InMemoryCache } from '@apollo/client'
9- import { HttpLink } from '@apollo/client/link/http'
7+ import { ApolloProvider } from '@apollo/client'
108import { useWeb3Context } from 'web3-react'
119import getNetworkEnv from 'utils/network-env'
1210
@@ -17,7 +15,8 @@ import Loading from 'components/loading'
1715import connectors from 'config/connectors'
1816import { DEFAULT_NETWORK } from 'config/networks'
1917import { hexlify } from 'utils/string'
20- import { TCR_EXISTENCE_TEST } from 'utils/graphql'
18+ import usePathValidation from 'hooks/use-path-validation'
19+ import useApolloClientFactory from 'hooks/use-apollo-client-factory'
2120
2221const ItemsRouter = loadable (
2322 ( ) => import ( /* webpackPrefetch: true */ 'pages/items-router' ) ,
@@ -41,93 +40,11 @@ const ClassicFactory = loadable(
4140
4241
4342const AppRouter = ( ) => {
44- const history = useHistory ( )
4543 const { networkId, error } = useWeb3Context ( ) ;
4644 const isUnsupported = useMemo ( ( ) => error ?. code === 'UNSUPPORTED_NETWORK' , [ error ] )
47- const [ pathResolved , setPathResolved ] = useState ( false ) ;
48- const [ invalidTcrAddr , setInvalidTcrAddr ] = useState ( false ) ;
49-
5045 const tcrAddress = getNetworkEnv ( 'REACT_APP_DEFAULT_TCR_ADDRESSES' , networkId ) ;
51-
52- const client = useMemo ( ( ) => {
53- if ( ! networkId ) {
54- return null
55- }
56-
57- const GTCR_SUBGRAPH_URL = getNetworkEnv (
58- 'REACT_APP_SUBGRAPH_URL' ,
59- networkId
60- )
61-
62- if ( ! GTCR_SUBGRAPH_URL ) {
63- return null ;
64- }
65-
66- const httpLink = new HttpLink ( {
67- uri : GTCR_SUBGRAPH_URL
68- } )
69- return new ApolloClient ( {
70- link : httpLink ,
71- cache : new InMemoryCache ( )
72- } )
73- } , [ networkId ] )
74-
75- useEffect ( ( ) => {
76- const checkPathValidation = async ( ) => {
77- const pathname = history . location . pathname ;
78- const search = history . location . search ;
79- const isOldPath = / \/ t c r \/ 0 x / . test ( pathname ) ;
80-
81- if ( isOldPath ) {
82- const searchParams = new URLSearchParams ( search ) ;
83- let chainId = null ;
84- const tcrAddress = pathname . match ( / t c r \/ ( 0 x [ 0 - 9 a - z A - Z ] + ) / ) [ 1 ] . toLowerCase ( ) ;
85-
86- if ( searchParams . has ( 'chainId' ) ) {
87- chainId = searchParams . get ( 'chainId' ) ;
88- } else {
89- const DEFAULT_TCR_ADDRESSES = JSON . parse ( process . env . REACT_APP_DEFAULT_TCR_ADDRESSES ) ;
90- const ADDRs = Object . values ( DEFAULT_TCR_ADDRESSES ) . map ( addr => addr . toLowerCase ( ) ) ;
91- const CHAIN_IDs = Object . keys ( DEFAULT_TCR_ADDRESSES ) ;
92- const tcrIndex = ADDRs . findIndex ( addr => addr === tcrAddress ) ;
93-
94- if ( tcrIndex >= 0 ) {
95- chainId = Number ( CHAIN_IDs [ tcrIndex ] ) ;
96- } else {
97- const SUBGRAPH_URLS = JSON . parse ( process . env . REACT_APP_SUBGRAPH_URL ) ;
98- const queryResults = await Promise . all ( Object . values ( SUBGRAPH_URLS ) . map ( subgraph => {
99- const client = new ApolloClient ( {
100- link : new HttpLink ( { uri : subgraph } ) ,
101- cache : new InMemoryCache ( )
102- } ) ;
103- return client . query ( {
104- query : TCR_EXISTENCE_TEST ,
105- variables : {
106- tcrAddress,
107- } ,
108- } ) ;
109- } ) ) ;
110- const validIndex = queryResults . findIndex (
111- ( { data : { lregistry, registry } } ) => lregistry !== null || registry !== null
112- ) ;
113-
114- if ( validIndex >= 0 ) {
115- chainId = Object . keys ( SUBGRAPH_URLS ) [ validIndex ] ;
116- }
117- }
118- }
119-
120- if ( chainId ) {
121- const newPathname = pathname . replace ( '/tcr/' , `/tcr/${ chainId } /` ) ;
122- history . push ( { pathname : newPathname , search } ) ;
123- } else {
124- setInvalidTcrAddr ( true ) ;
125- }
126- }
127- setPathResolved ( true ) ;
128- } ;
129- checkPathValidation ( ) ;
130- } , [ history , setPathResolved ] ) ;
46+ const [ pathResolved , invalidTcrAddr ] = usePathValidation ( )
47+ const client = useApolloClientFactory ( networkId )
13148
13249 useEffect ( ( ) => {
13350 if ( isUnsupported && window . ethereum ) {
0 commit comments