-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
<!-- Before opening a pull request, please read the [contributing guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md) first --> <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on enhancing the voting system by introducing new vote types, updating interfaces, and improving the UI for casting and displaying votes, particularly for single-choice and weighted voting proposals. ### Detailed summary - Added `SingleVoteState` and `WeightedVoteState` interfaces. - Introduced `ProposalTypeName` enum for vote types. - Updated `Proposal` interface to include `type`, `scores`, and `scores_total`. - Modified `CastVoteModal` to handle different vote types. - Created `SingleVote` and `WeightedVote` components for respective voting methods. - Improved `VoteRow` to display vote percentages for weighted votes. - Enhanced UI components for better styling and responsiveness. - Updated localization strings for new voting-related texts. - Refactored voting logic to accommodate new features and improve clarity. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
- Loading branch information
1 parent
680d3f1
commit 1b8a6d4
Showing
22 changed files
with
789 additions
and
196 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
44 changes: 44 additions & 0 deletions
44
apps/web/src/views/Voting/Proposal/ResultType/SingleVoteResults.tsx
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 |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useTranslation } from '@pancakeswap/localization' | ||
import { Box, Flex, Progress, Text } from '@pancakeswap/uikit' | ||
import { formatNumber } from '@pancakeswap/utils/formatBalance' | ||
import { Vote } from 'state/types' | ||
import TextEllipsis from '../../components/TextEllipsis' | ||
import { calculateVoteResults, getTotalFromVotes } from '../../helpers' | ||
|
||
interface SingleVoteResultsProps { | ||
choices: string[] | ||
votes: Vote[] | ||
} | ||
|
||
export const SingleVoteResults: React.FC<SingleVoteResultsProps> = ({ votes, choices }) => { | ||
const { t } = useTranslation() | ||
const results = calculateVoteResults(votes) | ||
const totalVotes = getTotalFromVotes(votes) | ||
|
||
return ( | ||
<> | ||
{choices.map((choice, index) => { | ||
const choiceVotes = results[choice] || [] | ||
const totalChoiceVote = getTotalFromVotes(choiceVotes) | ||
const progress = totalVotes === 0 ? 0 : (totalChoiceVote / totalVotes) * 100 | ||
|
||
return ( | ||
<Box key={choice} mt={index > 0 ? '24px' : '0px'}> | ||
<Flex alignItems="center" mb="8px"> | ||
<TextEllipsis mb="4px" title={choice}> | ||
{choice} | ||
</TextEllipsis> | ||
</Flex> | ||
<Box mb="4px"> | ||
<Progress primaryStep={progress} scale="sm" /> | ||
</Box> | ||
<Flex alignItems="center" justifyContent="space-between"> | ||
<Text color="textSubtle">{t('%total% Votes', { total: formatNumber(totalChoiceVote, 0, 2) })}</Text> | ||
<Text>{progress.toLocaleString(undefined, { minimumFractionDigits: 2, maximumFractionDigits: 2 })}%</Text> | ||
</Flex> | ||
</Box> | ||
) | ||
})} | ||
</> | ||
) | ||
} |
72 changes: 72 additions & 0 deletions
72
apps/web/src/views/Voting/Proposal/ResultType/WeightedVoteResults.tsx
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { useTranslation } from '@pancakeswap/localization' | ||
import { Box, Flex, Progress, Text } from '@pancakeswap/uikit' | ||
import { formatNumber } from '@pancakeswap/utils/formatBalance' | ||
import { useMemo } from 'react' | ||
import { WeightedVoteState } from 'views/Voting/Proposal/VoteType/types' | ||
import TextEllipsis from '../../components/TextEllipsis' | ||
|
||
interface WeightedVoteResultsProps { | ||
choices: string[] | ||
sortData?: boolean | ||
choicesVotes: WeightedVoteState[] | ||
} | ||
|
||
export const WeightedVoteResults: React.FC<WeightedVoteResultsProps> = ({ choices, sortData, choicesVotes }) => { | ||
const { t } = useTranslation() | ||
|
||
const totalSum = useMemo( | ||
() => choicesVotes.reduce((sum, item) => sum + Object.values(item).reduce((a, b) => a + b, 0), 0), | ||
[choicesVotes], | ||
) | ||
|
||
const percentageResults = useMemo( | ||
() => | ||
choicesVotes.reduce((acc, item) => { | ||
Object.entries(item).forEach(([key, value]) => { | ||
// eslint-disable-next-line no-param-reassign | ||
acc[key] = (acc[key] || 0) + value | ||
}) | ||
return acc | ||
}, {}), | ||
[choicesVotes], | ||
) | ||
|
||
const sortedChoices = useMemo(() => { | ||
const list = choices.map((choice, index) => { | ||
const totalChoiceVote = percentageResults[index + 1] ?? 0 | ||
const progress = (totalChoiceVote / totalSum) * 100 | ||
return { choice, totalChoiceVote, progress } | ||
}) | ||
|
||
return sortData ? list.sort((a, b) => b.progress - a.progress) : list | ||
}, [choices, percentageResults, sortData, totalSum]) | ||
|
||
return ( | ||
<> | ||
{sortedChoices.map(({ choice, totalChoiceVote, progress }, index) => ( | ||
<Box key={choice} mt={index > 0 ? '24px' : '0px'}> | ||
<Flex alignItems="center" mb="8px"> | ||
<TextEllipsis mb="4px" title={choice}> | ||
{choice} | ||
</TextEllipsis> | ||
</Flex> | ||
<Box mb="4px"> | ||
<Progress primaryStep={progress} scale="sm" /> | ||
</Box> | ||
<Flex alignItems="center" justifyContent="space-between"> | ||
<Text color="textSubtle">{t('%total% Votes', { total: formatNumber(totalChoiceVote, 0, 2) })}</Text> | ||
<Text> | ||
{totalChoiceVote === 0 && totalSum === 0 | ||
? '0.00%' | ||
: `${progress.toLocaleString(undefined, { | ||
minimumFractionDigits: 2, | ||
maximumFractionDigits: 2, | ||
})} | ||
%`} | ||
</Text> | ||
</Flex> | ||
</Box> | ||
))} | ||
</> | ||
) | ||
} |
Oops, something went wrong.