Skip to content

Commit

Permalink
fix: kusama society vote result popup, #1034 (#1056)
Browse files Browse the repository at this point in the history
* fix: kusama society vote result popup, #1034

* Update
  • Loading branch information
hyifeng authored Oct 17, 2024
1 parent 4b9d6e4 commit 31c0041
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 23 deletions.
5 changes: 5 additions & 0 deletions next/components/loadingField.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { ReactComponent as LoadingSvg } from "public/imgs/icons/loading.svg";

export default function LoadingField({ children, isLoading }) {
return isLoading ? <LoadingSvg /> : children;
}
34 changes: 27 additions & 7 deletions next/components/postDetail/postResults.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ import {
hasSocietyVoteStrategyOnly,
} from "frontedUtils/strategy";
import QuorumSocietyVoteResult from "./strategyResult/quorumSocietyVoteResult";
import isNil from "lodash.isnil";
import LoadingField from "../loadingField";

export default function PostResult({ data, voteStatus, space }) {
const votedAmount = data?.votedWeights?.balanceOf || 0;
const societyVotedAmount = data?.votedWeights?.societyVote || 0;
export function InnerPostResult({ data, voteStatus, space }) {
const votedAmount = data?.votedWeights?.balanceOf;
const societyVotedAmount = data?.votedWeights?.societyVote;

const results = data?.weightStrategy?.map((strategy) => {
if (strategy === "balance-of") {
Expand Down Expand Up @@ -97,32 +99,50 @@ export default function PostResult({ data, voteStatus, space }) {
<VoteItem>
<div>Voted</div>
<div>
<ValueDisplay value={votedAmount?.toString()} space={space} />
<LoadingField isLoading={isNil(votedAmount)}>
<ValueDisplay value={votedAmount?.toString()} space={space} />
</LoadingField>
</div>
</VoteItem>
);
} else if (hasSocietyVoteStrategyOnly(data?.weightStrategy)) {
voted = (
<VoteItem>
<div>Voted</div>
<div>{societyVotedAmount} VOTE</div>
<div>
<LoadingField isLoading={isNil(societyVotedAmount)}>
{societyVotedAmount} VOTE
</LoadingField>
</div>
</VoteItem>
);
}

return (
<Panel>
<>
<SideSectionTitle title="Results" img="/imgs/icons/strategy.svg" />
<Divider />
<div>
{voted}
<VoteItem>
<div>Voters</div>
<div>{data?.votesCount}</div>
<div>
<LoadingField isLoading={isNil(data?.votesCount)}>
{data?.votesCount}
</LoadingField>
</div>
</VoteItem>
</div>
{results}
{biasedVoting}
</>
);
}

export default function PostResultPanel({ data, voteStatus, space }) {
return (
<Panel>
<InnerPostResult data={data} voteStatus={voteStatus} space={space} />
</Panel>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
ResultHead,
ResultName,
} from "./styled";
import isNil from "lodash.isnil";
import LoadingField from "@/components/loadingField";

export default function VoteCountOptionList({ optionList, strategy, total }) {
return (
Expand All @@ -25,16 +27,18 @@ export default function VoteCountOptionList({ optionList, strategy, total }) {
<OptionIndex>{vote.choice}</OptionIndex>
</Tooltip>
<FlexAround>
<Tooltip
content={
total === 0
? "0.00%"
: `${((vote.voteBalance / total) * 100).toFixed(2)}%`
}
>
<div>{vote.voteBalance} VOTE</div>
</Tooltip>
{vote.icon && <>&nbsp;{vote.icon}</>}
<LoadingField isLoading={isNil(vote.voteBalance)}>
<Tooltip
content={
total === 0
? "0.00%"
: `${((vote.voteBalance / total) * 100).toFixed(2)}%`
}
>
<div>{vote.voteBalance} VOTE</div>
</Tooltip>
{vote.icon && <>&nbsp;{vote.icon}</>}
</LoadingField>
</FlexAround>
</ProgressItem>
<ProgressBackground>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import VoteCountOptionList from "./common/voteCountOptionList";

export default function OnePersonOneVoteResult({ proposal, voteStatus }) {
let winnerChoice = null;
for (let item of voteStatus) {
for (let item of voteStatus || []) {
if (new BigNumber(item.onePersonOneVote).isZero()) {
continue;
}
Expand All @@ -25,6 +25,17 @@ export default function OnePersonOneVoteResult({ proposal, voteStatus }) {

const optionList = [];
proposal?.choices?.forEach((choice, index) => {
if (!voteStatus) {
optionList.push({
index: index + 1,
choice,
voteBalance: null,
percentage: "0",
icon: null,
});
return;
}

for (let voteStat of voteStatus) {
if (voteStat.choice !== choice) {
continue;
Expand Down
13 changes: 12 additions & 1 deletion next/components/postDetail/strategyResult/societyVoteResult.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default function SocietyVoteResult({ proposal, voteStatus }) {
const total = proposal?.votedWeights?.societyVote || 0;

let winnerChoice = null;
for (let item of voteStatus) {
for (let item of voteStatus || []) {
if (new BigNumber(item.societyVote).isZero()) {
continue;
}
Expand All @@ -32,6 +32,17 @@ export default function SocietyVoteResult({ proposal, voteStatus }) {

const optionList = [];
proposal?.choices?.forEach((choice, index) => {
if (!voteStatus) {
optionList.push({
index: index + 1,
choice,
voteBalance: null,
percentage: "0",
icon: null,
});
return;
}

for (let voteStat of voteStatus) {
if (voteStat.choice !== choice) {
continue;
Expand Down
12 changes: 11 additions & 1 deletion next/components/postResult/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { useRef, useState } from "react";
import { ReactComponent as StatusSvg } from "public/imgs/icons/status.svg";
import { useOffset } from "frontedUtils/hooks";
import Popup from "./popup";
import SocietyPopup from "./societyPopup";
import {
hasOnePersonOneVoteStrategyOnly,
hasSocietyVoteStrategyOnly,
} from "frontedUtils/strategy";

const Wrapper = styled.div`
position: relative;
Expand Down Expand Up @@ -32,6 +37,11 @@ export default function ResultPopup({ data, space }) {
const { top } = useOffset(ref);
const [show, setShow] = useState(false);

const isSocietyProposal =
hasSocietyVoteStrategyOnly(data?.weightStrategy) ||
hasOnePersonOneVoteStrategyOnly(data?.weightStrategy);
const PopupComponent = isSocietyProposal ? SocietyPopup : Popup;

return (
<Wrapper
ref={ref}
Expand All @@ -41,7 +51,7 @@ export default function ResultPopup({ data, space }) {
<IconWrapper className="icon">
<StatusSvg />
</IconWrapper>
{show && <Popup data={data} space={space} isTop={top > 400} />}
{show && <PopupComponent data={data} space={space} isTop={top > 400} />}
</Wrapper>
);
}
6 changes: 3 additions & 3 deletions next/components/postResult/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { votesSelector, fetchVote } from "store/reducers/voteSlice";
import { ReactComponent as LoadingSvg } from "public/imgs/icons/loading.svg";
import BigNumber from "bignumber.js";

const ResultWrapper = styled.div`
export const ResultWrapper = styled.div`
z-index: 999;
position: absolute;
background: var(--fillBgPrimary);
Expand All @@ -27,7 +27,7 @@ const ResultWrapper = styled.div`
`}
`;

const Triangle = styled.div`
export const Triangle = styled.div`
position: absolute;
width: 0;
height: 0;
Expand All @@ -38,7 +38,7 @@ const Triangle = styled.div`
top: 100%;
`;

const TriangleTop = styled(Triangle)`
export const TriangleTop = styled(Triangle)`
border-top: 0;
border-bottom: 8px solid var(--fillBgPrimary);
top: -8px;
Expand Down
50 changes: 50 additions & 0 deletions next/components/postResult/societyPopup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";

import { votesSelector, fetchVote } from "store/reducers/voteSlice";
import { InnerPostResult } from "../postDetail/postResults";
import { ResultWrapper, Triangle, TriangleTop } from "./popup";

export default function SocietyPopup({ data, isTop, space }) {
const votes = useSelector(votesSelector);
const dispatch = useDispatch();

useEffect(() => {
if (data?.cid && !votes[data?.cid]) {
dispatch(fetchVote(data?.cid, data?.space));
}
}, [votes, data, dispatch]);

const vote = votes[data?.cid];

const votesCount = vote?.reduce(
(pre, cur) => pre + Number(cur.votesCount ?? 0),
0,
);
const totalSocietyVote = vote?.reduce(
(pre, cur) => pre + Number(cur.societyVote ?? 0),
0,
);
const totalOnePersonOneVote = vote?.reduce(
(pre, cur) => pre + Number(cur.onePersonOneVote ?? 0),
0,
);

return (
<ResultWrapper isTop={isTop} className="shadow-shadowPopup">
<InnerPostResult
data={{
...data,
votesCount,
votedWeights: {
societyVote: totalSocietyVote,
onePersonOneVote: totalOnePersonOneVote,
},
}}
voteStatus={vote}
space={space}
/>
{isTop ? <Triangle /> : <TriangleTop />}
</ResultWrapper>
);
}

0 comments on commit 31c0041

Please sign in to comment.