fix(governance): ensure cached finalized vote is only used when pubkey is c… (#4040)
This commit is contained in:
parent
d1fd9184ce
commit
5a48ba4a33
@ -1,11 +1,10 @@
|
||||
import { captureMessage } from '@sentry/minimal';
|
||||
|
||||
import { useVegaWallet } from '@vegaprotocol/wallet';
|
||||
import { VoteValue } from '@vegaprotocol/types';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useUserVoteQuery } from './__generated__/Vote';
|
||||
import type { FinalizedVote } from '@vegaprotocol/proposals';
|
||||
import { removePaginationWrapper } from '@vegaprotocol/utils';
|
||||
import type { FinalizedVote } from '@vegaprotocol/proposals';
|
||||
|
||||
export enum VoteState {
|
||||
NotCast = 'NotCast',
|
||||
@ -45,21 +44,23 @@ export function useUserVote(
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (finalizedVote?.vote.value) {
|
||||
if (finalizedVote?.vote.value && finalizedVote.pubKey === pubKey) {
|
||||
setUserVote(finalizedVote);
|
||||
} else if (data?.party?.votesConnection?.edges) {
|
||||
// This sets the vote (if any) when the user first loads the page
|
||||
setUserVote(
|
||||
removePaginationWrapper(data?.party?.votesConnection?.edges).find(
|
||||
({ proposalId: pId }) => proposalId === pId
|
||||
)
|
||||
);
|
||||
} else if (data?.party?.votesConnection?.edges && pubKey) {
|
||||
const vote = removePaginationWrapper(
|
||||
data?.party?.votesConnection?.edges
|
||||
).find(({ proposalId: pId }) => proposalId === pId);
|
||||
|
||||
if (vote) {
|
||||
setUserVote({ ...vote, pubKey });
|
||||
}
|
||||
}
|
||||
}, [
|
||||
finalizedVote?.vote.value,
|
||||
data?.party?.votesConnection?.edges,
|
||||
finalizedVote,
|
||||
proposalId,
|
||||
pubKey,
|
||||
]);
|
||||
|
||||
// If user vote changes update the vote state
|
||||
@ -94,6 +95,8 @@ export function useUserVote(
|
||||
return {
|
||||
voteState,
|
||||
userVote,
|
||||
voteDatetime: userVote ? new Date(userVote.vote.datetime) : null,
|
||||
voteDatetime: userVote?.vote?.datetime
|
||||
? new Date(userVote.vote.datetime)
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
@ -1 +1,2 @@
|
||||
export * from './use-vote-submit';
|
||||
export * from './__generated__/VoteSubsciption';
|
||||
|
@ -5,15 +5,16 @@ import { useVoteEvent } from './use-vote-event';
|
||||
import type { VoteValue } from '@vegaprotocol/types';
|
||||
import type { VoteEventFieldsFragment } from './__generated__/VoteSubsciption';
|
||||
|
||||
export type FinalizedVote = VoteEventFieldsFragment;
|
||||
export type FinalizedVote = VoteEventFieldsFragment & { pubKey: string };
|
||||
|
||||
export const useVoteSubmit = () => {
|
||||
const { pubKey } = useVegaWallet();
|
||||
const { send, transaction, setComplete, Dialog } = useVegaTransaction();
|
||||
const waitForVoteEvent = useVoteEvent(transaction);
|
||||
|
||||
const [finalizedVote, setFinalizedVote] =
|
||||
useState<VoteEventFieldsFragment | null>(null);
|
||||
const [finalizedVote, setFinalizedVote] = useState<FinalizedVote | null>(
|
||||
null
|
||||
);
|
||||
|
||||
const submit = useCallback(
|
||||
async (voteValue: VoteValue, proposalId: string | null) => {
|
||||
@ -33,7 +34,7 @@ export const useVoteSubmit = () => {
|
||||
|
||||
if (res) {
|
||||
waitForVoteEvent(proposalId, pubKey, (v) => {
|
||||
setFinalizedVote(v);
|
||||
setFinalizedVote({ ...v, pubKey });
|
||||
setComplete();
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user