diff --git a/apps/governance/src/routes/proposals/components/vote-details/use-user-vote.tsx b/apps/governance/src/routes/proposals/components/vote-details/use-user-vote.tsx index 826794ac7..92c3859eb 100644 --- a/apps/governance/src/routes/proposals/components/vote-details/use-user-vote.tsx +++ b/apps/governance/src/routes/proposals/components/vote-details/use-user-vote.tsx @@ -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, }; } diff --git a/libs/proposals/src/lib/voting-hooks/index.ts b/libs/proposals/src/lib/voting-hooks/index.ts index e5ce57ed9..3dc273f6c 100644 --- a/libs/proposals/src/lib/voting-hooks/index.ts +++ b/libs/proposals/src/lib/voting-hooks/index.ts @@ -1 +1,2 @@ export * from './use-vote-submit'; +export * from './__generated__/VoteSubsciption'; diff --git a/libs/proposals/src/lib/voting-hooks/use-vote-submit.ts b/libs/proposals/src/lib/voting-hooks/use-vote-submit.ts index 08ccfa89f..55485eae4 100644 --- a/libs/proposals/src/lib/voting-hooks/use-vote-submit.ts +++ b/libs/proposals/src/lib/voting-hooks/use-vote-submit.ts @@ -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(null); + const [finalizedVote, setFinalizedVote] = useState( + 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(); }); }