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