Skip to content

Commit

Permalink
chore: check notVoted before showing the modal
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick-1979 committed Oct 30, 2024
1 parent f2b61d1 commit e8636c5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import { faHandshakeAngle } from '@fortawesome/free-solid-svg-icons';
import ThumbUpIcon from '@mui/icons-material/ThumbUp';
import WatchLaterIcon from '@mui/icons-material/WatchLater';
import { Box, Grid, Typography } from '@mui/material';
import React, { useCallback, useContext, useEffect, useState } from 'react';
import React, { useCallback, useContext, useEffect, useMemo, useState } from 'react';

import { BN, BN_ZERO } from '@polkadot/util';

import { AccountsAssetsContext, TwoButtons } from '../../components';
import { getStorage, setStorage } from '../../components/Loading';
import { useTranslation } from '../../hooks';
import { useMyVote, useTranslation } from '../../hooks';
import { tieAccount } from '../../messaging';
import { POLKADOT_GENESIS_HASH } from '../../util/constants';
import { openOrFocusTab } from '../accountDetails/components/CommonTasks';
import SimpleModalTitle from '../partials/SimpleModalTitle';
import { DraggableModal } from './components/DraggableModal';

const PROPOSAL_NO = 1264;
const TRACK_ID = 33;
const SHOW_INTERVAL = 10 * 1000; // ms
const STORAGE_LABEL = `polkaGateVoteReminderLastShown_${PROPOSAL_NO}`;

Expand All @@ -31,6 +32,8 @@ export default function SupportUs (): React.ReactElement {
const [open, setOpen] = useState<boolean>(true);
const [maxPowerAddress, setAddress] = useState<string>();
const [timeToShow, setTimeToShow] = useState<boolean>();
const vote = useMyVote(maxPowerAddress, PROPOSAL_NO, TRACK_ID);
const notVoted = useMemo(() => vote === null || (vote && !('standard' in vote || 'splitAbstain' in vote || ('delegating' in vote && vote?.delegating?.voted))), [vote]);

useEffect(() => {
getStorage(STORAGE_LABEL).then((maybeDate) => {
Expand All @@ -56,8 +59,10 @@ export default function SupportUs (): React.ReactElement {

const votingBalance = maybeAsset[0].votingBalance ? new BN(maybeAsset[0].votingBalance) : BN_ZERO;

max = votingBalance.gt(max) ? votingBalance : max;
addressWithMaxVotingPower = address;
if (votingBalance.gt(max)) {
max = votingBalance;
addressWithMaxVotingPower = address;
}
});

addressWithMaxVotingPower && tieAccount(addressWithMaxVotingPower, POLKADOT_GENESIS_HASH).finally(() => {
Expand All @@ -80,7 +85,7 @@ export default function SupportUs (): React.ReactElement {

return (
<>
{maxPowerAddress && timeToShow &&
{maxPowerAddress && timeToShow && notVoted &&
<DraggableModal onClose={handleClose} open={open}>
<>
<SimpleModalTitle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
// SPDX-License-Identifier: Apache-2.0
// @ts-nocheck

//@ts-nocheck

import type { ApiPromise } from '@polkadot/api';
import type { AccountId32 } from '@polkadot/types/interfaces/runtime';
import type { PalletConvictionVotingVoteVoting } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
import type { Track } from '../../utils/types';

import { ApiPromise } from '@polkadot/api';
import { BN } from '@polkadot/util';

import { Track } from '../../utils/types';
import { toCamelCase } from '../../utils/util';

export const CONVICTION = {
Expand Down Expand Up @@ -71,8 +71,8 @@ interface Voting {
delegating: any; // needs to be fixed
}

export async function getAddressVote(address: string, api: ApiPromise, referendumIndex: number, trackId: number): Promise<Vote | null> {
const voting = await api.query.convictionVoting.votingFor(address, trackId) as unknown as PalletConvictionVotingVoteVoting;
export async function getAddressVote (address: string, api: ApiPromise, referendumIndex: number, trackId: number): Promise<Vote | null> {
const voting = await api.query['convictionVoting']['votingFor'](address, trackId) as unknown as PalletConvictionVotingVoteVoting;

if (voting.isEmpty) {
return null;
Expand All @@ -96,7 +96,7 @@ export async function getAddressVote(address: string, api: ApiPromise, referendu
if (voting.isDelegating) {
// Then, look into the votes of the delegating target address.
const { conviction, target } = voting.asDelegating;
const proxyVoting = await api.query.convictionVoting.votingFor(target, trackId) as unknown as PalletConvictionVotingVoteVoting;
const proxyVoting = await api.query['convictionVoting']['votingFor'](target, trackId) as unknown as PalletConvictionVotingVoteVoting;
const targetVote = proxyVoting.isCasting ? proxyVoting.asCasting.votes.find(([index]) => index.toNumber() === referendumIndex)?.[1] : undefined;

if (!targetVote?.isStandard && !targetVote?.isSplitAbstain) {
Expand Down Expand Up @@ -139,8 +139,8 @@ export async function getAddressVote(address: string, api: ApiPromise, referendu
return null;
}

export async function getAllVotes(address: string, api: ApiPromise, tracks: Track[]): Promise<number[] | null> {
const queries = tracks.map((t) => api.query.convictionVoting.votingFor(address, t[0]));
export async function getAllVotes (address: string, api: ApiPromise, tracks: Track[]): Promise<number[] | null> {
const queries = tracks.map((t) => api.query['convictionVoting']['votingFor'](address, t[0]));
const voting = await Promise.all(queries);
const castedRefIndexes = voting?.map((v) => {
const jsonV = v.toJSON() as unknown as Voting;
Expand Down
16 changes: 9 additions & 7 deletions packages/extension-polkagate/src/hooks/useMyVote.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
// Copyright 2019-2024 @polkadot/extension-polkagate authors & contributors
// SPDX-License-Identifier: Apache-2.0
// @ts-nocheck

import type React from 'react';
import type { Vote } from '../fullscreen/governance/post/myVote/util';

import { useCallback, useEffect, useState } from 'react';

import { getAddressVote, Vote } from '../fullscreen/governance/post/myVote/util';
import { useApi, useFormatted } from '.';
import { getAddressVote } from '../fullscreen/governance/post/myVote/util';
import { useInfo } from '.';

export default function useMyVote(
export default function useMyVote (
address: string | undefined,
refIndex: number | string | undefined,
trackId: number | string | undefined,
refresh?: boolean,
setRefresh?: React.Dispatch<React.SetStateAction<boolean>>
): Vote | null | undefined {
const api = useApi(address);
const formatted = useFormatted(address);
const { api, formatted } = useInfo(address);

const [vote, setVote] = useState<Vote | null | undefined>();

const fetchVote = useCallback(async () => {
Expand All @@ -36,7 +38,7 @@ export default function useMyVote(
}, [fetchVote]);

useEffect(() => {
refresh && fetchVote();
refresh && fetchVote().catch(console.error);
}, [fetchVote, refresh]);

return vote;
Expand Down
5 changes: 4 additions & 1 deletion packages/extension/public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1412,5 +1412,8 @@
"No proxy": "",
"Switch Theme": "",
"Account Icon": "",
"Support PolkaGate!": ""
"Support PolkaGate!": "",
"We’re seeking retroactive funding to sustain and expand PolkaGate’s impact! Your vote can empower us to continue delivering valuable improvements and innovations. Voting won’t spend your tokens—they’ll just be temporarily locked based on your chosen conviction level.": "",
"Vote to Support Us": "",
"Maybe Later": ""
}

0 comments on commit e8636c5

Please sign in to comment.