Skip to content

Commit

Permalink
Merge pull request #136 from algorandfoundation/feat-ENG-63-pin-appro…
Browse files Browse the repository at this point in the history
…ved-proposals-to-top

feat: ENG-63 sorts passed proposals to top on voting session results page
  • Loading branch information
HashMapsData2Value authored Dec 12, 2023
2 parents 1d60a03 + 124b64b commit 51d4920
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 51 deletions.
3 changes: 2 additions & 1 deletion src/build-a-bull/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ dist-ssr
*.sln
*.sw?

.env
.env*
!.env*.template
54 changes: 32 additions & 22 deletions src/build-a-bull/src/features/vote/VoteResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Alert, Button, Skeleton, Typography } from '@mui/material'
import { saveAs } from 'file-saver'
import Papa from 'papaparse'
import { useMemo, useState } from 'react'
import { VotingRoundMetadata } from '@/shared/IPFSGateway'
import { Question, VotingRoundMetadata } from '@/shared/IPFSGateway'
import { VotingRoundGlobalState } from '@/shared/VotingRoundContract'
import { ProposalCard } from '@/shared/ProposalCard'
import { VotingRoundResult } from '@/shared/types'
Expand Down Expand Up @@ -39,6 +39,16 @@ export const VoteResults = ({
}, 0)
: 0
}

const countVotesTally = (question: Question) => {
return question.options.length > 0 && optionIDsToCounts[question.options[0].id] ? optionIDsToCounts[question.options[0].id] : 0
}

const passedToTopSort = (q1: Question, q2: Question) => {
if (!q1.metadata?.threshold || !q2.metadata?.threshold) return 0
return countVotesTally(q2) / q2.metadata.threshold - countVotesTally(q1) / q1.metadata.threshold
}

const totalVotes = useMemo(() => getTotalVotes(), [votingRoundResults, votingRoundMetadata])
const generateProposalsResultsCsv = async () => {
if (votingRoundMetadata) {
Expand Down Expand Up @@ -105,27 +115,27 @@ export const VoteResults = ({
</>
))}
{!isLoadingVotingRoundResults &&
votingRoundMetadata?.questions.map((question) => (
<div key={question.id}>
{question.metadata && (
<ProposalCard
title={question.prompt}
description={question.description}
category={question.metadata.category}
focus_area={question.metadata.focus_area}
link={question.metadata.link}
threshold={question.metadata.threshold}
ask={question.metadata.ask}
skipTags={true}
totalVotes={totalVotes}
votesTally={
question.options.length > 0 && optionIDsToCounts[question.options[0].id] ? optionIDsToCounts[question.options[0].id] : 0
}
hasClosed={true}
/>
)}
</div>
))}
votingRoundMetadata?.questions
.sort((q1, q2) => passedToTopSort(q1, q2))
.map((question) => (
<div key={question.id}>
{question.metadata && (
<ProposalCard
title={question.prompt}
description={question.description}
category={question.metadata.category}
focus_area={question.metadata.focus_area}
link={question.metadata.link}
threshold={question.metadata.threshold}
ask={question.metadata.ask}
skipTags={true}
totalVotes={totalVotes}
votesTally={countVotesTally(question)}
hasClosed={true}
/>
)}
</div>
))}
</div>
<div className="w-full text-right mt-4">
<Button
Expand Down
3 changes: 2 additions & 1 deletion src/xgov-dapp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ dist-ssr
*.sln
*.sw?

.env
.env*
!.env*.template
62 changes: 35 additions & 27 deletions src/xgov-dapp/src/features/vote/VoteResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,19 @@ export const VoteResults = ({

const optionIDsToCounts = votingRoundResults !== undefined ? generateOptionIDsToCountsMapping(votingRoundResults) : {}

const countVotesTally = (question: Question) => {
return question.options.length > 0 && optionIDsToCounts[question.options[0].id] ? optionIDsToCounts[question.options[0].id] : 0
}

const passedToTopSort = (q1: Question, q2: Question) => {
if (!q1.metadata?.threshold || !q2.metadata?.threshold) return 0
return countVotesTally(q2) / q2.metadata.threshold - countVotesTally(q1) / q1.metadata.threshold
}

const passedToTopSortReserve = (q1: Question, q2: Question, passedReserveList: Set<string>) => {
return passedReserveList.has(q2.id) ? 100 : passedReserveList.has(q1.id) ? -100 : passedToTopSort(q1, q2)
}

// clone the voting round metadata and adjust the threshold to be out of total votes instead of total voting power
// we clone the metadata so that we don't mutate the original metadata
const votingRoundMetadataClone = useMemo<VotingRoundMetadata | undefined>(() => {
Expand Down Expand Up @@ -224,6 +237,7 @@ export const VoteResults = ({
? !isReserveList(q, optionIDsToCounts[q.options[0].id])
: true,
)
.sort((q1, q2) => passedToTopSort(q1, q2))
.map((question) => (
<div key={question.id}>
{question.metadata && (
Expand All @@ -235,11 +249,7 @@ export const VoteResults = ({
link={question.metadata.link}
threshold={question.metadata.threshold}
ask={question.metadata.ask}
votesTally={
question.options.length > 0 && optionIDsToCounts[question.options[0].id]
? optionIDsToCounts[question.options[0].id]
: 0
}
votesTally={countVotesTally(question)}
hasClosed={true}
/>
)}
Expand All @@ -265,28 +275,26 @@ export const VoteResults = ({
</>
))}
{!isLoadingVotingRoundResults &&
reserveList.map((question) => (
<div key={question.id}>
{question.metadata && (
<ProposalCard
title={question.prompt}
description={question.description}
category={question.metadata.category}
focus_area={question.metadata.focus_area}
link={question.metadata.link}
threshold={question.metadata.threshold}
ask={question.metadata.ask}
votesTally={
question.options.length > 0 && optionIDsToCounts[question.options[0].id]
? optionIDsToCounts[question.options[0].id]
: 0
}
hasClosed={true}
forcePass={passedReserveList.has(question.id)}
/>
)}
</div>
))}
reserveList
.sort((q1, q2) => passedToTopSortReserve(q1, q2, passedReserveList))
.map((question) => (
<div key={question.id}>
{question.metadata && (
<ProposalCard
title={question.prompt}
description={question.description}
category={question.metadata.category}
focus_area={question.metadata.focus_area}
link={question.metadata.link}
threshold={question.metadata.threshold}
ask={question.metadata.ask}
votesTally={countVotesTally(question)}
hasClosed={true}
forcePass={passedReserveList.has(question.id)}
/>
)}
</div>
))}
</>
)}
</div>
Expand Down

0 comments on commit 51d4920

Please sign in to comment.