-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove useAsyncMemo (#1022)
* started useAsyncMemo replacement * further components migrated * finished refactor * added env to .env.example * use constant for stale time * requested changes * removed last useAsyncMemo useage * fix: Identity modal bug * small change
- Loading branch information
Showing
62 changed files
with
719 additions
and
499 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
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
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
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 +1,3 @@ | ||
export const DEFAULT_QUERY_STALE_TIME = 120 * 1000 | ||
export const DEFAULT_QUERY_STALE_TIME = 3.6e6 // 1 hour | ||
export const TWENTY_MINUTES_MS = 1.2e6 // 20 minutes | ||
export const FIVE_MINUTES_MS = 3e5 // 5 minutes |
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,35 +1,38 @@ | ||
import useAsyncMemo from 'decentraland-gatsby/dist/hooks/useAsyncMemo' | ||
import { useQuery } from '@tanstack/react-query' | ||
import max from 'lodash/max' | ||
|
||
import { SnapshotGraphql } from '../clients/SnapshotGraphql' | ||
|
||
import { TWENTY_MINUTES_MS } from './constants' | ||
|
||
export type VoteHistory = { lastVoted: number; timesVoted: number } | ||
|
||
export default function useAddressesVotesTotals(addresses: string[]) { | ||
const [addressesVotesTotals, state] = useAsyncMemo( | ||
async () => { | ||
const addressesVotesByDate = await SnapshotGraphql.get().getAddressesVotes(addresses) | ||
const aggregatedVotes: Record<string, VoteHistory> = {} | ||
addressesVotesByDate.map((voteByDate) => { | ||
const voter = voteByDate.voter.toLowerCase() | ||
if (aggregatedVotes[voter]) { | ||
aggregatedVotes[voter].timesVoted += 1 | ||
aggregatedVotes[voter].lastVoted = max([aggregatedVotes[voter].lastVoted, voteByDate.created]) || 0 | ||
} else { | ||
aggregatedVotes[voter] = { | ||
lastVoted: voteByDate.created, | ||
timesVoted: 1, | ||
} | ||
} | ||
}) | ||
return aggregatedVotes | ||
}, | ||
[JSON.stringify(addresses)], | ||
{ initialValue: {} as Record<string, VoteHistory>, callWithTruthyDeps: true } | ||
) | ||
const fetchVotes = async (addresses: string[]) => { | ||
const addressesVotesByDate = await SnapshotGraphql.get().getAddressesVotes(addresses) | ||
const aggregatedVotes: Record<string, VoteHistory> = {} | ||
addressesVotesByDate.map((voteByDate) => { | ||
const voter = voteByDate.voter.toLowerCase() | ||
if (aggregatedVotes[voter]) { | ||
aggregatedVotes[voter].timesVoted += 1 | ||
aggregatedVotes[voter].lastVoted = max([aggregatedVotes[voter].lastVoted, voteByDate.created]) || 0 | ||
} else { | ||
aggregatedVotes[voter] = { | ||
lastVoted: voteByDate.created, | ||
timesVoted: 1, | ||
} | ||
} | ||
}) | ||
return aggregatedVotes | ||
} | ||
|
||
export default function useAddressesVotesTotals(addresses: string[]) { | ||
const { data: addressesVotesTotals, isLoading } = useQuery({ | ||
queryKey: [`votes#${addresses.join('-')}`], | ||
queryFn: () => fetchVotes(addresses), | ||
staleTime: TWENTY_MINUTES_MS, | ||
}) | ||
return { | ||
addressesVotesTotals, | ||
isLoadingAddressesVotesTotals: state.loading, | ||
addressesVotesTotals: addressesVotesTotals ?? {}, | ||
isLoadingAddressesVotesTotals: isLoading, | ||
} | ||
} |
Oops, something went wrong.