Skip to content

Commit

Permalink
Remove getVoteWeight from useSnapshotProposal
Browse files Browse the repository at this point in the history
  • Loading branch information
mudrila committed Nov 22, 2023
1 parent afb3e4c commit 8e0aa2e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Grid, GridItem, Text, Flex } from '@chakra-ui/react';
import { useTranslation } from 'react-i18next';
import useSnapshotProposal from '../../../hooks/DAO/loaders/snapshot/useSnapshotProposal';
import useDisplayName from '../../../hooks/utils/useDisplayName';
import { useFractal } from '../../../providers/App/AppProvider';
import {
Expand All @@ -17,7 +16,6 @@ interface ISnapshotProposalVoteItem {

export default function SnapshotProposalVoteItem({ proposal, vote }: ISnapshotProposalVoteItem) {
const { t } = useTranslation();
const { getVoteWeight } = useSnapshotProposal(proposal);
const { displayName } = useDisplayName(vote.voter);
const {
readOnly: { user },
Expand Down Expand Up @@ -62,7 +60,7 @@ export default function SnapshotProposalVoteItem({ proposal, vote }: ISnapshotPr
</GridItem>
<GridItem colSpan={1}>
<Text textStyle="text-base-sans-regular">
{getVoteWeight(vote)} {proposal.strategies[0].params.symbol}
{vote.votingWeight} {proposal.strategies[0].params.symbol}
</Text>
</GridItem>
</Grid>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { useState, useEffect } from 'react';
import useSnapshotProposal from '../../../../hooks/DAO/loaders/snapshot/useSnapshotProposal';
import { ExtendedSnapshotProposal } from '../../../../types';

export default function useTotalVotes({ proposal }: { proposal?: ExtendedSnapshotProposal }) {
const [totalVotesCasted, setTotalVotesCasted] = useState(0);
const { getVoteWeight } = useSnapshotProposal(proposal);

useEffect(() => {
if (proposal) {
let newTotalVotesCasted = 0;
proposal.votes.forEach(vote => (newTotalVotesCasted += getVoteWeight(vote)));
proposal.votes.forEach(vote => (newTotalVotesCasted += vote.votingWeight));
setTotalVotesCasted(newTotalVotesCasted);
}
}, [proposal, totalVotesCasted, getVoteWeight]);
}, [proposal, totalVotesCasted]);

return { totalVotesCasted };
}
17 changes: 7 additions & 10 deletions src/hooks/DAO/loaders/snapshot/useSnapshotProposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
[snapshotProposal]
);

const getVoteWeight = useCallback((vote: SnapshotVote) => vote.votingWeight, []);

const loadProposal = useCallback(async () => {
if (snapshotProposal?.snapshotProposalId) {
const proposalQueryResult = await client
Expand Down Expand Up @@ -135,12 +133,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
const existingChoiceType = votesBreakdown[voteChoice];
if (existingChoiceType) {
votesBreakdown[voteChoice] = {
total: existingChoiceType.total + getVoteWeight(vote),
total: existingChoiceType.total + vote.votingWeight,
votes: [...existingChoiceType.votes, vote],
};
} else {
votesBreakdown[voteChoice] = {
total: getVoteWeight(vote),
total: vote.votingWeight,
votes: [vote],
};
}
Expand All @@ -152,12 +150,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
const existingChoiceType = votesBreakdown[voteChoice];
if (existingChoiceType) {
votesBreakdown[voteChoice] = {
total: existingChoiceType.total + getVoteWeight(vote),
total: existingChoiceType.total + vote.votingWeight,
votes: [...existingChoiceType.votes, vote],
};
} else {
votesBreakdown[voteChoice] = {
total: getVoteWeight(vote),
total: vote.votingWeight,
votes: [vote],
};
}
Expand All @@ -170,12 +168,12 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u

if (existingChoiceType) {
votesBreakdown[choiceKey] = {
total: existingChoiceType.total + getVoteWeight(vote),
total: existingChoiceType.total + vote.votingWeight,
votes: [...existingChoiceType.votes, vote],
};
} else {
votesBreakdown[choiceKey] = {
total: getVoteWeight(vote),
total: vote.votingWeight,
votes: [vote],
};
}
Expand All @@ -190,7 +188,7 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
votes: votesQueryResult,
} as ExtendedSnapshotProposal);
}
}, [snapshotProposal?.snapshotProposalId, proposal, snapshotProposal?.state, getVoteWeight]);
}, [snapshotProposal?.snapshotProposalId, proposal, snapshotProposal?.state]);

const loadVotingWeight = useCallback(async () => {
if (snapshotProposal?.snapshotProposalId) {
Expand Down Expand Up @@ -230,7 +228,6 @@ export default function useSnapshotProposal(proposal: FractalProposal | null | u
return {
loadVotingWeight,
loadProposal,
getVoteWeight,
snapshotProposal,
isSnapshotProposal,
extendedSnapshotProposal,
Expand Down

0 comments on commit 8e0aa2e

Please sign in to comment.