-
Notifications
You must be signed in to change notification settings - Fork 18
refactor: use lazy useEnsData for treasury vote ENS resolution #572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,9 +1,8 @@ | ||||||||||||||||||||||||||||||||||||||||||
| import Spinner from "@components/Spinner"; | ||||||||||||||||||||||||||||||||||||||||||
| import { getEnsForVotes } from "@lib/api/ens"; | ||||||||||||||||||||||||||||||||||||||||||
| import { formatAddress, lptFormatter } from "@lib/utils"; | ||||||||||||||||||||||||||||||||||||||||||
| import { lptFormatter } from "@lib/utils"; | ||||||||||||||||||||||||||||||||||||||||||
| import { Flex, Text } from "@livepeer/design-system"; | ||||||||||||||||||||||||||||||||||||||||||
| import { useTreasuryVoteEventsQuery, useTreasuryVotesQuery } from "apollo"; | ||||||||||||||||||||||||||||||||||||||||||
| import React, { useEffect, useMemo, useState } from "react"; | ||||||||||||||||||||||||||||||||||||||||||
| import React, { useMemo, useState } from "react"; | ||||||||||||||||||||||||||||||||||||||||||
| import { useWindowSize } from "react-use"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import TreasuryVotePopover from "./TreasuryVotePopover"; | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -40,69 +39,33 @@ const useVotes = (proposalId: string) => { | |||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const [votes, setVotes] = useState<Vote[]>([]); | ||||||||||||||||||||||||||||||||||||||||||
| const [votesLoading, setVotesLoading] = useState(false); | ||||||||||||||||||||||||||||||||||||||||||
| useEffect(() => { | ||||||||||||||||||||||||||||||||||||||||||
| if ( | ||||||||||||||||||||||||||||||||||||||||||
| !treasuryVotesData?.treasuryVotes || | ||||||||||||||||||||||||||||||||||||||||||
| !treasuryVoteEventsData?.treasuryVoteEvents | ||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||
| setVotes([]); | ||||||||||||||||||||||||||||||||||||||||||
| const votes = useMemo(() => { | ||||||||||||||||||||||||||||||||||||||||||
| const votesList = treasuryVotesData?.treasuryVotes; | ||||||||||||||||||||||||||||||||||||||||||
| const eventsList = treasuryVoteEventsData?.treasuryVoteEvents; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (!votesList || !eventsList) { | ||||||||||||||||||||||||||||||||||||||||||
| return []; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| const decorateVotes = async () => { | ||||||||||||||||||||||||||||||||||||||||||
| setVotesLoading(true); | ||||||||||||||||||||||||||||||||||||||||||
| const uniqueVoters = Array.from( | ||||||||||||||||||||||||||||||||||||||||||
| new Set(treasuryVotesData?.treasuryVotes?.map((v) => v.voter.id) ?? []) | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| const localEnsCache: { [address: string]: string } = {}; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| await Promise.all( | ||||||||||||||||||||||||||||||||||||||||||
| uniqueVoters.map(async (address) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| if (localEnsCache[address]) { | ||||||||||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| const ensAddress = await getEnsForVotes(address); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (ensAddress && ensAddress.name) { | ||||||||||||||||||||||||||||||||||||||||||
| localEnsCache[address] = ensAddress.name; | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| localEnsCache[address] = formatAddress(address); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||||||||||||||||||
| console.warn(`Failed to fetch ENS for ${address}`, e); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| const votes = | ||||||||||||||||||||||||||||||||||||||||||
| treasuryVotesData?.treasuryVotes?.map((vote) => { | ||||||||||||||||||||||||||||||||||||||||||
| const events = (treasuryVoteEventsData?.treasuryVoteEvents ?? []) | ||||||||||||||||||||||||||||||||||||||||||
| .filter((event) => event.voter.id === vote.voter.id) | ||||||||||||||||||||||||||||||||||||||||||
| .sort((a, b) => b.timestamp - a.timestamp); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const latestEvent = events[0]; | ||||||||||||||||||||||||||||||||||||||||||
| const ensName = localEnsCache[vote.voter.id] ?? ""; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||
| ...vote, | ||||||||||||||||||||||||||||||||||||||||||
| reason: latestEvent?.reason || vote.reason || "", | ||||||||||||||||||||||||||||||||||||||||||
| ensName, | ||||||||||||||||||||||||||||||||||||||||||
| transactionHash: latestEvent?.transaction.id ?? "", | ||||||||||||||||||||||||||||||||||||||||||
| timestamp: latestEvent?.timestamp, | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
| }) ?? []; | ||||||||||||||||||||||||||||||||||||||||||
| setVotes(votes as Vote[]); | ||||||||||||||||||||||||||||||||||||||||||
| setVotesLoading(false); | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
| decorateVotes(); | ||||||||||||||||||||||||||||||||||||||||||
| }, [ | ||||||||||||||||||||||||||||||||||||||||||
| treasuryVotesData?.treasuryVotes, | ||||||||||||||||||||||||||||||||||||||||||
| treasuryVoteEventsData?.treasuryVoteEvents, | ||||||||||||||||||||||||||||||||||||||||||
| ]); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return votesList.map((vote) => { | ||||||||||||||||||||||||||||||||||||||||||
| const events = eventsList | ||||||||||||||||||||||||||||||||||||||||||
| .filter((event) => event.voter.id === vote.voter.id) | ||||||||||||||||||||||||||||||||||||||||||
| .sort((a, b) => b.timestamp - a.timestamp); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const latestEvent = events[0]; | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+50
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
The useVotes hook has O(votes × events log events) performance issue from filtering and sorting events for each vote independently |
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||||||||||||
| ...vote, | ||||||||||||||||||||||||||||||||||||||||||
| reason: latestEvent?.reason || vote.reason || "", | ||||||||||||||||||||||||||||||||||||||||||
| transactionHash: latestEvent?.transaction.id ?? "", | ||||||||||||||||||||||||||||||||||||||||||
| timestamp: latestEvent?.timestamp, | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
| }) as Vote[]; | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+50
to
+63
|
||||||||||||||||||||||||||||||||||||||||||
| }, [treasuryVotesData, treasuryVoteEventsData]); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
| }, [treasuryVotesData, treasuryVoteEventsData]); | |
| }, [treasuryVotesData?.treasuryVotes, treasuryVoteEventsData?.treasuryVoteEvents]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "Voter" column accessor changed from a string field to an object, breaking sort functionality in react-table