Skip to content

Commit aabf7db

Browse files
author
devofficer
committed
refactor: checking light curate in hook
1 parent a3c2cf8 commit aabf7db

File tree

6 files changed

+111
-99
lines changed

6 files changed

+111
-99
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@
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",
6769
"@types/styled-components": "^5.1.25",
6870
"eslint-config-prettier": "^6.0.0",
6971
"eslint-config-react-app": "^4.0.1",
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { useEffect, useState } from "react"
2+
import { useWeb3Context } from 'web3-react'
3+
import { ethers } from 'ethers'
4+
import { useParams } from 'react-router'
5+
import _gtcr from '../assets/abis/GeneralizedTCR.json'
6+
7+
const useCheckLightCurate = () => {
8+
const { library, active } = useWeb3Context()
9+
const { tcrAddress } = useParams<{ tcrAddress: string }>()
10+
11+
const [isLightCurate, setLightCurate] = useState<boolean>(false)
12+
13+
useEffect(() => {
14+
const checkLightCurate = async () => {
15+
try {
16+
if (!active) return
17+
const tcr = new ethers.Contract(tcrAddress, _gtcr, library)
18+
19+
// Call a function only available on GTCR Classic. If
20+
// it throws, its not a light curate instance.
21+
await tcr.itemCount()
22+
setLightCurate(false)
23+
// eslint-disable-next-line no-unused-vars
24+
} catch (err) {
25+
console.info(
26+
`Contract call used to verify if this is a Light Curate instance. Ignore exception.`
27+
)
28+
setLightCurate(true)
29+
}
30+
}
31+
checkLightCurate()
32+
}, [active, library, tcrAddress])
33+
34+
return isLightCurate
35+
}
36+
37+
export default useCheckLightCurate

src/pages/item-details-router.js

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

src/pages/item-details-router.tsx

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react'
2+
import { TCRViewProvider } from 'contexts/tcr-view-context'
3+
import { LightTCRViewProvider } from 'contexts/light-tcr-view-context'
4+
import loadable from '@loadable/component'
5+
import { useParams } from 'react-router'
6+
import useTcrNetwork from 'hooks/use-tcr-network'
7+
import { NETWORK_STATUS } from 'config/networks'
8+
import useCheckLightCurate from 'hooks/use-check-light-curate'
9+
import Loading from 'components/loading'
10+
11+
const LightItemDetails = loadable(
12+
() => import(/* webpackPrefetch: true */ './light-item-details/index'),
13+
{
14+
fallback: <Loading />
15+
}
16+
)
17+
18+
const ItemDetails = loadable(
19+
() => import(/* webpackPrefetch: true */ './item-details/index'),
20+
{
21+
fallback: <Loading />
22+
}
23+
)
24+
25+
const ItemDetailsRouter = () => {
26+
const { tcrAddress, itemID } = useParams<{ tcrAddress: string, itemID: string }>()
27+
const { networkStatus } = useTcrNetwork()
28+
const search = window.location.search
29+
const isLightCurate = useCheckLightCurate()
30+
31+
if (isLightCurate === undefined || networkStatus !== NETWORK_STATUS.supported)
32+
return <Loading />
33+
34+
if (isLightCurate)
35+
return (
36+
<LightTCRViewProvider tcrAddress={tcrAddress}>
37+
<LightItemDetails search={search} itemID={itemID} />
38+
</LightTCRViewProvider>
39+
)
40+
41+
return (
42+
<TCRViewProvider tcrAddress={tcrAddress}>
43+
<ItemDetails search={search} itemID={itemID} />
44+
</TCRViewProvider>
45+
)
46+
}
47+
48+
export default ItemDetailsRouter

src/pages/items-router.js renamed to src/pages/items-router.tsx

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import React, { useEffect, useState } from 'react'
1+
import React from 'react'
22
import { TCRViewProvider } from 'contexts/tcr-view-context'
33
import { LightTCRViewProvider } from 'contexts/light-tcr-view-context'
44
import loadable from '@loadable/component'
5-
import _gtcr from '../assets/abis/GeneralizedTCR.json'
6-
import { useWeb3Context } from 'web3-react'
7-
import { ethers } from 'ethers'
85
import { useParams } from 'react-router'
96
import Loading from 'components/loading'
107
import useTcrNetwork from 'hooks/use-tcr-network'
118
import { NETWORK_STATUS } from 'config/networks'
9+
import useCheckLightCurate from 'hooks/use-check-light-curate'
1210

1311
const LightItems = loadable(
1412
() => import(/* webpackPrefetch: true */ './light-items/index'),
@@ -25,31 +23,10 @@ const Items = loadable(
2523
)
2624

2725
const ItemsRouter = () => {
28-
const { tcrAddress } = useParams()
29-
const [isLightCurate, setIsLightCurate] = useState()
30-
const { library, active } = useWeb3Context()
26+
const { tcrAddress } = useParams<{ tcrAddress: string }>()
27+
const isLightCurate = useCheckLightCurate()
3128
const { networkStatus } = useTcrNetwork()
3229

33-
useEffect(() => {
34-
;(async () => {
35-
try {
36-
if (!active) return
37-
const tcr = new ethers.Contract(tcrAddress, _gtcr, library)
38-
39-
// Call a function only available on GTCR Classic. If
40-
// it throws, its not a light curate instance.
41-
await tcr.itemCount()
42-
setIsLightCurate(false)
43-
// eslint-disable-next-line no-unused-vars
44-
} catch (err) {
45-
console.info(
46-
`Contract call used to verify if this is a Light Curate instance. Ignore exception.`
47-
)
48-
setIsLightCurate(true)
49-
}
50-
})()
51-
}, [active, library, tcrAddress])
52-
5330
if (
5431
typeof isLightCurate === 'undefined' ||
5532
networkStatus !== NETWORK_STATUS.supported

yarn.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,11 @@
27272727
dependencies:
27282728
"@types/node" "*"
27292729

2730+
"@types/history@^4.7.11":
2731+
version "4.7.11"
2732+
resolved "https://registry.yarnpkg.com/@types/history/-/history-4.7.11.tgz#56588b17ae8f50c53983a524fc3cc47437969d64"
2733+
integrity sha512-qjDJRrmvBMiTx+jyLxvLfJU7UznFuokDv4f3WRuriHKERccVpFU+8XMQUAbDzoiJCsmexxRExQeMwwCdamSKDA==
2734+
27302735
"@types/hoist-non-react-statics@*":
27312736
version "3.3.1"
27322737
resolved "https://registry.yarnpkg.com/@types/hoist-non-react-statics/-/hoist-non-react-statics-3.3.1.tgz#1124aafe5118cb591977aeb1ceaaed1070eb039f"
@@ -2765,6 +2770,13 @@
27652770
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
27662771
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
27672772

2773+
"@types/loadable__component@^5.13.4":
2774+
version "5.13.4"
2775+
resolved "https://registry.yarnpkg.com/@types/loadable__component/-/loadable__component-5.13.4.tgz#a4646b2406b1283efac1a9d9485824a905b33d4a"
2776+
integrity sha512-YhoCCxyuvP2XeZNbHbi8Wb9EMaUJuA2VGHxJffcQYrJKIKSkymJrhbzsf9y4zpTmr5pExAAEh5hbF628PAZ8Dg==
2777+
dependencies:
2778+
"@types/react" "*"
2779+
27682780
"@types/minimist@^1.2.0":
27692781
version "1.2.2"
27702782
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
@@ -2812,6 +2824,14 @@
28122824
resolved "https://registry.yarnpkg.com/@types/q/-/q-1.5.5.tgz#75a2a8e7d8ab4b230414505d92335d1dcb53a6df"
28132825
integrity sha512-L28j2FcJfSZOnL1WBjDYp2vUHCeIFlyYI/53EwD/rKUBQ7MtUUfbQWiyKJGpcnv4/WgrhWsFKrcPstcAt/J0tQ==
28142826

2827+
"@types/react-router@^5.1.18":
2828+
version "5.1.18"
2829+
resolved "https://registry.yarnpkg.com/@types/react-router/-/react-router-5.1.18.tgz#c8851884b60bc23733500d86c1266e1cfbbd9ef3"
2830+
integrity sha512-YYknwy0D0iOwKQgz9v8nOzt2J6l4gouBmDnWqUUznltOTaon+r8US8ky8HvN0tXvc38U9m6z/t2RsVsnd1zM0g==
2831+
dependencies:
2832+
"@types/history" "^4.7.11"
2833+
"@types/react" "*"
2834+
28152835
"@types/react-slick@^0.23.4":
28162836
version "0.23.5"
28172837
resolved "https://registry.yarnpkg.com/@types/react-slick/-/react-slick-0.23.5.tgz#e55fdc79bf19022ef77a6f22e9d64fd0f7463cc4"

0 commit comments

Comments
 (0)