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 { 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,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -1 +1,2 @@
|
|||||||
export * from './use-vote-submit';
|
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 { 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();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user