fix(governance): ensure cached finalized vote is only used when pubkey is c… (#4040)

This commit is contained in:
Sam Keen 2023-06-19 18:07:30 +01:00 committed by GitHub
parent d1fd9184ce
commit 5a48ba4a33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 15 deletions

View File

@ -1,11 +1,10 @@
import { captureMessage } from '@sentry/minimal'; import { captureMessage } from '@sentry/minimal';
import { useVegaWallet } from '@vegaprotocol/wallet'; import { useVegaWallet } from '@vegaprotocol/wallet';
import { VoteValue } from '@vegaprotocol/types'; import { VoteValue } from '@vegaprotocol/types';
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import { useUserVoteQuery } from './__generated__/Vote'; import { useUserVoteQuery } from './__generated__/Vote';
import type { FinalizedVote } from '@vegaprotocol/proposals';
import { removePaginationWrapper } from '@vegaprotocol/utils'; import { removePaginationWrapper } from '@vegaprotocol/utils';
import type { FinalizedVote } from '@vegaprotocol/proposals';
export enum VoteState { export enum VoteState {
NotCast = 'NotCast', NotCast = 'NotCast',
@ -45,21 +44,23 @@ export function useUserVote(
); );
useEffect(() => { useEffect(() => {
if (finalizedVote?.vote.value) { if (finalizedVote?.vote.value && finalizedVote.pubKey === pubKey) {
setUserVote(finalizedVote); setUserVote(finalizedVote);
} else if (data?.party?.votesConnection?.edges) { } else if (data?.party?.votesConnection?.edges && pubKey) {
// This sets the vote (if any) when the user first loads the page const vote = removePaginationWrapper(
setUserVote( data?.party?.votesConnection?.edges
removePaginationWrapper(data?.party?.votesConnection?.edges).find( ).find(({ proposalId: pId }) => proposalId === pId);
({ proposalId: pId }) => proposalId === pId
) if (vote) {
); setUserVote({ ...vote, pubKey });
}
} }
}, [ }, [
finalizedVote?.vote.value, finalizedVote?.vote.value,
data?.party?.votesConnection?.edges, data?.party?.votesConnection?.edges,
finalizedVote, finalizedVote,
proposalId, proposalId,
pubKey,
]); ]);
// If user vote changes update the vote state // If user vote changes update the vote state
@ -94,6 +95,8 @@ export function useUserVote(
return { return {
voteState, voteState,
userVote, userVote,
voteDatetime: userVote ? new Date(userVote.vote.datetime) : null, voteDatetime: userVote?.vote?.datetime
? new Date(userVote.vote.datetime)
: null,
}; };
} }

View File

@ -1 +1,2 @@
export * from './use-vote-submit'; export * from './use-vote-submit';
export * from './__generated__/VoteSubsciption';

View File

@ -5,15 +5,16 @@ import { useVoteEvent } from './use-vote-event';
import type { VoteValue } from '@vegaprotocol/types'; import type { VoteValue } from '@vegaprotocol/types';
import type { VoteEventFieldsFragment } from './__generated__/VoteSubsciption'; import type { VoteEventFieldsFragment } from './__generated__/VoteSubsciption';
export type FinalizedVote = VoteEventFieldsFragment; export type FinalizedVote = VoteEventFieldsFragment & { pubKey: string };
export const useVoteSubmit = () => { export const useVoteSubmit = () => {
const { pubKey } = useVegaWallet(); const { pubKey } = useVegaWallet();
const { send, transaction, setComplete, Dialog } = useVegaTransaction(); const { send, transaction, setComplete, Dialog } = useVegaTransaction();
const waitForVoteEvent = useVoteEvent(transaction); const waitForVoteEvent = useVoteEvent(transaction);
const [finalizedVote, setFinalizedVote] = const [finalizedVote, setFinalizedVote] = useState<FinalizedVote | null>(
useState<VoteEventFieldsFragment | null>(null); null
);
const submit = useCallback( const submit = useCallback(
async (voteValue: VoteValue, proposalId: string | null) => { async (voteValue: VoteValue, proposalId: string | null) => {
@ -33,7 +34,7 @@ export const useVoteSubmit = () => {
if (res) { if (res) {
waitForVoteEvent(proposalId, pubKey, (v) => { waitForVoteEvent(proposalId, pubKey, (v) => {
setFinalizedVote(v); setFinalizedVote({ ...v, pubKey });
setComplete(); setComplete();
}); });
} }