2026-05-15 19:18:10 +03:00

128 lines
3.7 KiB
TypeScript

import num from "libs/num";
import { Box, Button, Flex, Text } from "@chakra-ui/react";
// Wallet connection status is now a boolean via useKeplrWallet
import { PROPOSAL_VOTE_POWER } from "constants/constants";
import { handleTinyAmount, NextLink } from "modules/common";
import { useVotingPower } from "modules/governance";
import React, { FC } from "react";
type Props = {
id: string;
address: string;
status: boolean;
totalVotePower: number | null;
proposalContract: any;
};
const VotePower: FC<Props> = ({
id,
address,
status,
totalVotePower,
proposalContract,
}) => {
const isOwner = address === proposalContract?.submitter;
const isVotingOver = proposalContract?.status !== "Active";
const isVotedFor = proposalContract?.for_voters.includes(address);
const isVotedAgainst = proposalContract?.against_voters.includes(address);
const userVotingPower = useVotingPower({ proposal_id: Number(id) });
const userVotingPowerPerc =
userVotingPower && totalVotePower
? (userVotingPower /
num(totalVotePower).div(PROPOSAL_VOTE_POWER).toNumber()) *
100
: `-`;
const showVotingOptions =
!isVotingOver && !isVotedFor && !isVotedAgainst && !!userVotingPower;
return (
<Box h="200px" bg="white.50" mb="3" p="5" borderRadius="xl">
<Text fontSize="xs">My Voting Power</Text>
<Box bg="blackAlpha.400" p="3" mt="3" borderRadius="lg">
<Text>{userVotingPower ? userVotingPower.toLocaleString() : `-`}</Text>
<Text fontSize="sm" mt="2px" color="whiteAlpha.400">
{handleTinyAmount(userVotingPowerPerc)}%
</Text>
</Box>
<Flex mt="4">
{showVotingOptions && (
<>
<NextLink
href={`/governance/proposal/${id}/vote/for`}
passHref
isDisabled={
!status || isOwner
}
>
<Button
width="50%"
mr="1"
variant={isOwner ? "voteinvalid" : "votegreen"}
isDisabled={
!status || isOwner
}
>
Vote For
</Button>
</NextLink>
<NextLink
href={`/governance/proposal/${id}/vote/against`}
passHref
isDisabled={
!status || isOwner
}
>
<Button
width="50%"
ml="1"
variant={isOwner ? "voteinvalid" : "votered"}
isDisabled={
!status || isOwner
}
>
Vote Against
</Button>
</NextLink>
</>
)}
{!showVotingOptions && (
<Flex
fontSize="sm"
w="100%"
h="10"
borderRadius="md"
bg={
isVotedFor
? "proposalColours.greenLight"
: isVotedAgainst
? "proposalColours.redLight"
: "white.100"
}
align="center"
justify="center"
>
{(isVotedFor || isVotedAgainst) && (
<Text
color={
isVotedFor ? "proposalColours.green" : "proposalColours.red"
}
fontWeight="500"
>{`Voted ${
isVotedFor ? "in favor of the" : "against the"
} proposal`}</Text>
)}
{!(isVotedFor || isVotedAgainst) && (
<Text color="whiteAlpha.400" fontWeight="500">
You did not vote
</Text>
)}
</Flex>
)}
</Flex>
</Box>
);
};
export default VotePower;