-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fellowship] Show eligible voters on fellowship referendum detail page,
#5236 (#5266) * init EligibleVoters structure, #5236 * init No Data container, #5236 * add List Table, #5239 * only collectives show EligibleVoters, #5236 * voted table data, #5239 * useCollectiveEligibleVoters, #5239 * remove useless key, #5239 * modify scroll container, #5239 * optimize columns style, #5239 * remove useless code, #5236 * Add votes for eligible voters, #5236 (#5290) * Add votes to eligible voters, #5236 * Improve header prompt text, #5236 * Fix eligible voter vote result, #5236 * add rank into allVotes, #5236 * Refactor, #5236 * add memo for allVotes & eligibleVoters, #5236 * revert code, #5236 * revert code, #5236 --------- Co-authored-by: Yongfeng LI <[email protected]>
- Loading branch information
1 parent
79fb8ec
commit 25f9311
Showing
14 changed files
with
390 additions
and
47 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
packages/next-common/hooks/collectives/useRankedCollectiveMinRank.js
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,10 @@ | ||
// used on ranked collective referendum detail page | ||
import { useTrack } from "next-common/context/post/gov2/track"; | ||
import { useRankedCollectivePallet } from "next-common/context/collectives/collectives"; | ||
import { getMinRankOfClass } from "next-common/context/post/fellowship/useMaxVoters"; | ||
|
||
export default function useRankedCollectiveMinRank() { | ||
const { id: trackId } = useTrack(); | ||
const collectivePallet = useRankedCollectivePallet(); | ||
return getMinRankOfClass(trackId, collectivePallet); | ||
} |
26 changes: 7 additions & 19 deletions
26
packages/next-common/hooks/fellowship/useFellowshipMemberRank.js
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,25 +1,13 @@ | ||
import { useContextApi } from "next-common/context/api"; | ||
import { useEffect, useState } from "react"; | ||
import useSubStorage from "next-common/hooks/common/useSubStorage"; | ||
|
||
export function useFellowshipMemberRank( | ||
address, | ||
pallet = "fellowshipCollective", | ||
) { | ||
const [rank, setRank] = useState(null); | ||
const api = useContextApi(); | ||
|
||
useEffect(() => { | ||
if (!api || !api.query[pallet]) { | ||
return; | ||
} | ||
|
||
api.query[pallet].members(address).then((resp) => { | ||
if (!resp.isNone) { | ||
const json = resp.value.toJSON(); | ||
setRank(json.rank); | ||
} | ||
}); | ||
}, [api, address, pallet]); | ||
|
||
return rank; | ||
const { result } = useSubStorage(pallet, "members", [address]); | ||
if (result && result.isSome) { | ||
return result.unwrap().rank.toNumber(); | ||
} else { | ||
return null; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
packages/next-common/utils/hooks/collectives/useCollectiveEligibleVoters.js
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,98 @@ | ||
import { useContextApi } from "next-common/context/api"; | ||
import { useReferendumVotingFinishIndexer } from "next-common/context/post/referenda/useReferendumVotingFinishHeight"; | ||
import { useEffect, useState } from "react"; | ||
import { groupBy, orderBy } from "lodash-es"; | ||
import { normalizeRankedCollectiveEntries } from "next-common/utils/rankedCollective/normalize"; | ||
import { useSelector } from "react-redux"; | ||
import { | ||
fellowshipVotesSelector, | ||
isLoadingFellowshipVotesSelector, | ||
} from "next-common/store/reducers/fellowship/votes"; | ||
import useRankedCollectiveMinRank from "next-common/hooks/collectives/useRankedCollectiveMinRank"; | ||
|
||
async function queryFellowshipCollectiveMembers(api, blockHash) { | ||
let blockApi = api; | ||
if (blockHash) { | ||
blockApi = await api.at(blockHash); | ||
} | ||
return await blockApi.query.fellowshipCollective.members.entries(); | ||
} | ||
|
||
function getMemberVotes(rank, minRank) { | ||
if (rank < minRank) { | ||
throw new Error(`Rank ${rank} is too low, and minimum rank is ${minRank}`); | ||
} | ||
const excess = rank - minRank; | ||
const v = excess + 1; | ||
return Math.floor((v * (v + 1)) / 2); | ||
} | ||
|
||
export default function useCollectiveEligibleVoters() { | ||
const api = useContextApi(); | ||
|
||
const [voters, setVoters] = useState({ | ||
votedMembers: [], | ||
unVotedMembers: [], | ||
}); | ||
const [loading, setLoading] = useState(true); | ||
|
||
const votingFinishIndexer = useReferendumVotingFinishIndexer(); | ||
const minRank = useRankedCollectiveMinRank(); | ||
|
||
const { allAye, allNay } = useSelector(fellowshipVotesSelector); | ||
const isLoadingVotes = useSelector(isLoadingFellowshipVotesSelector); | ||
|
||
useEffect(() => { | ||
if (!api || isLoadingVotes) { | ||
return; | ||
} | ||
|
||
(async () => { | ||
setLoading(true); | ||
try { | ||
const memberEntries = await queryFellowshipCollectiveMembers( | ||
api, | ||
votingFinishIndexer?.blockHash, | ||
); | ||
const normalizedMembers = | ||
normalizeRankedCollectiveEntries(memberEntries); | ||
const voters = normalizedMembers.filter( | ||
(member) => member?.rank >= minRank, | ||
); | ||
const sortedVoters = orderBy(voters, ["rank"], ["desc"]); | ||
const votersWithPower = sortedVoters.map((m) => ({ | ||
...m, | ||
votes: getMemberVotes(m.rank, minRank), | ||
})); | ||
|
||
const allVotes = [...allAye, ...allNay]; | ||
const votedSet = new Set(allVotes.map((i) => i.address)); | ||
const { true: votedMembers, false: unVotedMembers } = groupBy( | ||
votersWithPower, | ||
(member) => votedSet.has(member.address), | ||
); | ||
|
||
setVoters({ | ||
votedMembers: votedMembers.map((m) => { | ||
const vote = allVotes.find((i) => i.address === m.address); | ||
return { | ||
...m, | ||
votes: vote.votes, | ||
isAye: vote.isAye, | ||
}; | ||
}), | ||
unVotedMembers, | ||
}); | ||
} catch (error) { | ||
console.error("Failed to fetch fellowship voters:", error); | ||
} finally { | ||
setLoading(false); | ||
} | ||
})(); | ||
}, [api, votingFinishIndexer, isLoadingVotes, allAye, allNay, minRank]); | ||
|
||
return { | ||
...voters, | ||
isLoading: loading, | ||
}; | ||
} |
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
54 changes: 54 additions & 0 deletions
54
packages/next/components/fellowship/referendum/sidebar/tally/eligibleVoters/columns.jsx
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,54 @@ | ||
import AddressUser from "next-common/components/user/addressUser"; | ||
import FellowshipRank from "next-common/components/fellowship/rank"; | ||
import { isNil } from "lodash-es"; | ||
import { useFellowshipMemberRank } from "next-common/hooks/fellowship/useFellowshipMemberRank"; | ||
import { useRankedCollectivePallet } from "next-common/context/collectives/collectives"; | ||
|
||
export function FellowshipRankInfo({ address }) { | ||
const collectivePallet = useRankedCollectivePallet(); | ||
const rank = useFellowshipMemberRank(address, collectivePallet); | ||
|
||
return <FellowshipRank rank={rank} />; | ||
} | ||
|
||
const rankColumn = { | ||
key: "rank", | ||
className: "w-5", | ||
render: (_, row) => ( | ||
<div className="w-5 h-5"> | ||
<FellowshipRankInfo address={row.address} /> | ||
</div> | ||
), | ||
}; | ||
|
||
const addressColumn = { | ||
key: "address", | ||
className: "text-left min-w-[140px]", | ||
render: (address) => <AddressUser add={address} />, | ||
}; | ||
|
||
const ayeColumn = { | ||
key: "isAye", | ||
className: "text-left w-[140px]", | ||
render: (isAye) => { | ||
if (isNil(isAye)) { | ||
return <span className="text-textDisabled">-</span>; | ||
} | ||
|
||
return isAye ? ( | ||
<span className="text-green500">Aye</span> | ||
) : ( | ||
<span className="text-red500">Nay</span> | ||
); | ||
}, | ||
}; | ||
|
||
const votesColumn = { | ||
key: "votes", | ||
className: "text-right w-[140px] align-right flex-end", | ||
render: (votes) => <span>{votes}</span>, | ||
}; | ||
|
||
const columns = [rankColumn, addressColumn, ayeColumn, votesColumn]; | ||
|
||
export default columns; |
22 changes: 22 additions & 0 deletions
22
packages/next/components/fellowship/referendum/sidebar/tally/eligibleVoters/index.jsx
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,22 @@ | ||
import { memo, useState } from "react"; | ||
import { Button } from "components/gov2/sidebar/tally/styled"; | ||
import dynamicPopup from "next-common/lib/dynamic/popup"; | ||
|
||
const EligibleVotersPopup = dynamicPopup(() => import("./popup")); | ||
|
||
function EligibleVoters() { | ||
const [showEligibleVoters, setShowEligibleVoters] = useState(false); | ||
|
||
return ( | ||
<> | ||
<Button onClick={() => setShowEligibleVoters(true)}> | ||
Eligible Voters | ||
</Button> | ||
{showEligibleVoters && ( | ||
<EligibleVotersPopup onClose={() => setShowEligibleVoters(false)} /> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
export default memo(EligibleVoters); |
Oops, something went wrong.