Skip to content

Commit 3556e75

Browse files
author
devofficer
committed
refactor: app router to extract path validation and apollo client
2 parents aabf7db + 8dd923a commit 3556e75

File tree

8 files changed

+229
-191
lines changed

8 files changed

+229
-191
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
"@commitlint/config-conventional": "^8.0.0",
6767
"@types/loadable__component": "^5.13.4",
6868
"@types/react-router": "^5.1.18",
69+
"@types/react-router-dom": "^5.3.3",
6970
"@types/styled-components": "^5.1.25",
7071
"eslint-config-prettier": "^6.0.0",
7172
"eslint-config-react-app": "^4.0.1",

src/bootstrap/app-router.js

Lines changed: 0 additions & 190 deletions
This file was deleted.

src/bootstrap/app-router.tsx

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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(/\/tcr\/(\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;

src/hooks/use-graphql-client.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useMemo } from 'react'
2+
import { ApolloClient, InMemoryCache } from '@apollo/client'
3+
import { HttpLink } from '@apollo/client/link/http'
4+
import getNetworkEnv from 'utils/network-env'
5+
6+
const useApolloClientFactory = (networkId: number | Empty) => {
7+
const client = useMemo(() => {
8+
if (!networkId) return null
9+
10+
const GTCR_SUBGRAPH_URL = getNetworkEnv('REACT_APP_SUBGRAPH_URL', networkId)
11+
12+
if (!GTCR_SUBGRAPH_URL) return null
13+
14+
const httpLink = new HttpLink({
15+
uri: GTCR_SUBGRAPH_URL
16+
})
17+
18+
return new ApolloClient({
19+
link: httpLink,
20+
cache: new InMemoryCache()
21+
})
22+
}, [networkId])
23+
24+
return client
25+
}
26+
27+
export default useApolloClientFactory

0 commit comments

Comments
 (0)