Commit 134f30e8 authored by lynn's avatar lynn Committed by GitHub

fix: resolve bug where changing duration when filter string is applied does...

fix: resolve bug where changing duration when filter string is applied does not change data (original duration is still applied) (#4737)

* init middle of change

* init

* fix

* respond to jordan
parent 9b07ac2b
...@@ -132,18 +132,27 @@ function toContractInput(token: PrefetchedTopToken) { ...@@ -132,18 +132,27 @@ function toContractInput(token: PrefetchedTopToken) {
} }
} }
// Map of key: ${chain} + ${address} and value: TopToken object. // Map of key: ${HistoryDuration} and value: another Map, of key:${chain} + ${address} and value: TopToken object.
// Acts as a local cache. // Acts as a local cache.
const tokensWithPriceHistoryCache: Record<string, TopToken> = {}
const checkIfAllTokensCached = (tokens: PrefetchedTopToken[]) => { const tokensWithPriceHistoryCache: Record<HistoryDuration, Record<string, TopToken>> = {
DAY: {},
HOUR: {},
MAX: {},
MONTH: {},
WEEK: {},
YEAR: {},
'%future added value': {},
}
const checkIfAllTokensCached = (duration: HistoryDuration, tokens: PrefetchedTopToken[]) => {
let everyTokenInCache = true let everyTokenInCache = true
const cachedTokens: TopToken[] = [] const cachedTokens: TopToken[] = []
const checkCache = (token: PrefetchedTopToken) => { const checkCache = (token: PrefetchedTopToken) => {
const tokenCacheKey = !!token ? `${token.chain}${token.address}` : '' const tokenCacheKey = !!token ? `${token.chain}${token.address}` : ''
if (tokenCacheKey in tokensWithPriceHistoryCache) { if (tokenCacheKey in tokensWithPriceHistoryCache[duration]) {
cachedTokens.push(tokensWithPriceHistoryCache[tokenCacheKey]) cachedTokens.push(tokensWithPriceHistoryCache[duration][tokenCacheKey])
return true return true
} else { } else {
everyTokenInCache = false everyTokenInCache = false
...@@ -203,8 +212,9 @@ export function useTopTokens(chain: Chain): UseTopTokensReturnValue { ...@@ -203,8 +212,9 @@ export function useTopTokens(chain: Chain): UseTopTokensReturnValue {
.toPromise() .toPromise()
.then((data) => { .then((data) => {
if (data?.tokens) { if (data?.tokens) {
const priceHistoryCacheForCurrentDuration = tokensWithPriceHistoryCache[duration]
data.tokens.map((token) => data.tokens.map((token) =>
!!token ? (tokensWithPriceHistoryCache[`${token.chain}${token.address}`] = token) : null !!token ? (priceHistoryCacheForCurrentDuration[`${token.chain}${token.address}`] = token) : null
) )
appendingTokens ? setTokens([...(tokens ?? []), ...data.tokens]) : setTokens([...data.tokens]) appendingTokens ? setTokens([...(tokens ?? []), ...data.tokens]) : setTokens([...data.tokens])
setLoading(false) setLoading(false)
...@@ -225,7 +235,10 @@ export function useTopTokens(chain: Chain): UseTopTokensReturnValue { ...@@ -225,7 +235,10 @@ export function useTopTokens(chain: Chain): UseTopTokensReturnValue {
// Reset count when filters are changed // Reset count when filters are changed
useLayoutEffect(() => { useLayoutEffect(() => {
const { everyTokenInCache, cachedTokens } = checkIfAllTokensCached(prefetchedSelectedTokensWithoutPriceHistory) const { everyTokenInCache, cachedTokens } = checkIfAllTokensCached(
duration,
prefetchedSelectedTokensWithoutPriceHistory
)
if (everyTokenInCache) { if (everyTokenInCache) {
setTokens(cachedTokens) setTokens(cachedTokens)
setLoading(false) setLoading(false)
...@@ -236,7 +249,7 @@ export function useTopTokens(chain: Chain): UseTopTokensReturnValue { ...@@ -236,7 +249,7 @@ export function useTopTokens(chain: Chain): UseTopTokensReturnValue {
const contracts = prefetchedSelectedTokensWithoutPriceHistory.slice(0, PAGE_SIZE).map(toContractInput) const contracts = prefetchedSelectedTokensWithoutPriceHistory.slice(0, PAGE_SIZE).map(toContractInput)
loadTokensWithPriceHistory({ contracts, appendingTokens: false, page: 0 }) loadTokensWithPriceHistory({ contracts, appendingTokens: false, page: 0 })
} }
}, [loadTokensWithPriceHistory, prefetchedSelectedTokensWithoutPriceHistory]) }, [loadTokensWithPriceHistory, prefetchedSelectedTokensWithoutPriceHistory, duration])
return { return {
loading, loading,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment