Skip to content

Commit c27daa5

Browse files
authored
Merge pull request #237 from devofficer/fix/frame-wallet
Fix/frame wallet
2 parents a3c2cf8 + 82b5733 commit c27daa5

16 files changed

+398
-354
lines changed

.eslintrc.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,6 @@ module.exports = {
7070
'import/no-named-default': 2,
7171
'import/no-useless-path-segments': 2,
7272

73-
// React JSX
74-
'react/jsx-filename-extension': [
75-
1,
76-
{
77-
extensions: ['.js']
78-
}
79-
],
80-
'react/jsx-indent': 0,
81-
'react/jsx-curly-brace-presence': [2, 'never'],
82-
8373
// React
8474
'react/prefer-stateless-function': 2,
8575
'react/destructuring-assignment': [2, 'always'],

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
"build": "yarn build:theme && react-scripts --max_old_space_size=4096 build",
99
"build:theme": "lessc --js ./src/bootstrap/theme.less ./src/bootstrap/theme.css",
1010
"release": "standard-version",
11-
"lint": "eslint \"./src/**/*.js\"",
12-
"lint:eslint:fix": "eslint \"./src/**/*.js\" --fix",
13-
"lint:prettier:fix": "prettier --write \"./src/**/*.js\"",
11+
"lint": "eslint \"./src/**/*.js\" \"./src/**/*.ts\" \"./src/**/*.tsx\"",
12+
"lint:eslint:fix": "eslint \"./src/**/*.js\" \"./src/**/*.ts\" \"./src/**/*.tsx\" --fix",
13+
"lint:prettier:fix": "prettier --write \"./src/**/*.js\" \"./src/**/*.ts\" \"./src/**/*.tsx\"",
1414
"format": "yarn run lint:eslint:fix && yarn run lint:prettier:fix"
1515
},
1616
"dependencies": {
@@ -64,6 +64,9 @@
6464
"@babel/helper-define-map": "^7.15.4",
6565
"@commitlint/cli": "^8.0.0",
6666
"@commitlint/config-conventional": "^8.0.0",
67+
"@types/loadable__component": "^5.13.4",
68+
"@types/react-router": "^5.1.18",
69+
"@types/react-router-dom": "^5.3.3",
6770
"@types/styled-components": "^5.1.25",
6871
"eslint-config-prettier": "^6.0.0",
6972
"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: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { useMemo } from 'react'
2+
import { useParams } from 'react-router'
3+
import { useQuery } from '@apollo/client'
4+
import { TCR_EXISTENCE_TEST } from 'utils/graphql'
5+
6+
const useCheckLightCurate = (): {
7+
isLightCurate: boolean
8+
checking: boolean
9+
} => {
10+
const { tcrAddress } = useParams<{ tcrAddress: string }>()
11+
const { loading, data } = useQuery(TCR_EXISTENCE_TEST, {
12+
variables: {
13+
tcrAddress: tcrAddress.toLowerCase()
14+
}
15+
})
16+
const isLightCurate = useMemo<boolean>(() => data?.lregistry ?? false, [data])
17+
18+
return { isLightCurate, checking: loading }
19+
}
20+
21+
export default useCheckLightCurate

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)