This repository has been archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated state to store count of classified claims other than a boolean * Updated the updater to store the count of classified claims * Updated claims on other network banner to check according to claim counts * Fixed issues with claim on multiple networks - Updated state to add new dimension to claim info: account Otherwise when checking a different account the state would be preserved and would lead to bad data - Refactored Updater to fix dependency on obj causing it to being stuck in a re-render loop - Fixed logic that compared different types using `===` instead of `==` - Fixed logic that was causing claim to be displayed when there were no claims * Refactoring variable Co-authored-by: Leandro <[email protected]>
- Loading branch information
1 parent
451206f
commit 9189731
Showing
5 changed files
with
156 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,81 @@ | ||
import { useEffect } from 'react' | ||
import { useEffect, useMemo } from 'react' | ||
import { SupportedChainId } from 'constants/chains' | ||
import { useClaimDispatchers, useClaimState, useUserAvailableClaims } from './hooks' | ||
import { ClassifiedUserClaims, useClaimDispatchers, useClaimState, useClassifiedUserClaims } from './hooks' | ||
import { useActiveWeb3React } from 'hooks' | ||
import { ClaimInfo } from 'state/claim/reducer' | ||
|
||
export default function Updater() { | ||
const { chainId } = useActiveWeb3React() | ||
const { activeClaimAccount } = useClaimState() | ||
const { setHasClaimsOnOtherChains } = useClaimDispatchers() | ||
const { setClaimsCount } = useClaimDispatchers() | ||
|
||
const { claims: mainnetAvailable } = useUserAvailableClaims(activeClaimAccount, SupportedChainId.MAINNET) | ||
const { claims: gnosisAvailable } = useUserAvailableClaims(activeClaimAccount, SupportedChainId.XDAI) | ||
// --- MAINNET --- | ||
// Fetches all classified claims | ||
const mainnetClaims = useClassifiedUserClaims(activeClaimAccount, SupportedChainId.MAINNET) | ||
// De-normalizing to avoid react hook update nightmare | ||
const { | ||
total: mTotal, | ||
expired: mExpired, | ||
claimed: mClaimed, | ||
available: mAvailable, | ||
// Counts the claims, based on which is the current network | ||
} = useMemo(() => _countClaims(mainnetClaims, chainId, SupportedChainId.MAINNET), [chainId, mainnetClaims]) | ||
|
||
// Stores it on redux | ||
useEffect(() => { | ||
setHasClaimsOnOtherChains({ | ||
if (!activeClaimAccount) return | ||
setClaimsCount({ | ||
chain: SupportedChainId.MAINNET, | ||
hasClaims: Boolean(mainnetAvailable && mainnetAvailable.length > 0), | ||
claimInfo: { total: mTotal, expired: mExpired, claimed: mClaimed, available: mAvailable }, | ||
account: activeClaimAccount, | ||
}) | ||
setHasClaimsOnOtherChains({ | ||
}, [activeClaimAccount, mAvailable, mClaimed, mExpired, mTotal, setClaimsCount]) | ||
|
||
// --- GCHAIN --- | ||
const gChainClaims = useClassifiedUserClaims(activeClaimAccount, SupportedChainId.XDAI) | ||
const { | ||
total: gTotal, | ||
expired: gExpired, | ||
claimed: gClaimed, | ||
available: gAvailable, | ||
} = useMemo(() => _countClaims(gChainClaims, chainId, SupportedChainId.XDAI), [chainId, gChainClaims]) | ||
|
||
useEffect(() => { | ||
if (!activeClaimAccount) return | ||
setClaimsCount({ | ||
chain: SupportedChainId.XDAI, | ||
hasClaims: Boolean(gnosisAvailable && gnosisAvailable.length > 0), | ||
claimInfo: { total: gTotal, expired: gExpired, claimed: gClaimed, available: gAvailable }, | ||
account: activeClaimAccount, | ||
}) | ||
}, [setHasClaimsOnOtherChains, mainnetAvailable, gnosisAvailable]) | ||
}, [activeClaimAccount, gAvailable, gClaimed, gExpired, gTotal, setClaimsCount]) | ||
|
||
return null | ||
} | ||
|
||
/** | ||
* Counts claims per network | ||
*/ | ||
function _countClaims( | ||
mainnetClaims: ClassifiedUserClaims, | ||
currentChainId: number | undefined, | ||
chainId: SupportedChainId | ||
) { | ||
const { available, claimed, expired } = mainnetClaims | ||
|
||
const count: ClaimInfo = { | ||
// Total is easy, just add everything up | ||
// This will always be accurate. | ||
// When we only have airdrop repo as reference, everything will be `available` | ||
// Otherwise the sum of all categories | ||
total: claimed.length + available.length + expired.length, | ||
} | ||
// Only if we are in the network we want to check we are able to do smart contract calls | ||
// and fetch contract data | ||
// Otherwise the count would be replaced with outdated info | ||
if (currentChainId === chainId) { | ||
count.available = available.length | ||
count.expired = expired.length | ||
count.claimed = claimed.length | ||
} | ||
return count | ||
} |