fix: vote not seen being incorrectly reported (#1508)

This commit is contained in:
Dexter Edwards 2022-09-28 14:55:25 +01:00 committed by GitHub
parent 6ddceaf6bd
commit f688b28b29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,8 @@
import { captureException, captureMessage } from '@sentry/minimal'; import { captureException, captureMessage } from '@sentry/minimal';
import * as React from 'react';
import { useVegaWallet } from '@vegaprotocol/wallet'; import { useVegaWallet } from '@vegaprotocol/wallet';
import { VoteValue } from '@vegaprotocol/types'; import { VoteValue } from '@vegaprotocol/types';
import { useEffect, useMemo, useState } from 'react';
export type Vote = { export type Vote = {
value: VoteValue; value: VoteValue;
@ -43,15 +43,17 @@ export function useUserVote(
noVotes: Votes | null noVotes: Votes | null
) { ) {
const { keypair, sendTx } = useVegaWallet(); const { keypair, sendTx } = useVegaWallet();
const yes = React.useMemo(() => yesVotes || [], [yesVotes]); // eslint-disable-next-line @typescript-eslint/no-explicit-any
const no = React.useMemo(() => noVotes || [], [noVotes]); const [timeout, setTimeoutValue] = useState<any>(null);
const yes = useMemo(() => yesVotes || [], [yesVotes]);
const no = useMemo(() => noVotes || [], [noVotes]);
const [voteState, setVoteState] = React.useState<VoteState | null>( const [voteState, setVoteState] = useState<VoteState | null>(
VoteState.NotCast VoteState.NotCast
); );
// Find the users vote everytime yes or no votes change // Find the users vote everytime yes or no votes change
const userVote = React.useMemo(() => { const userVote = useMemo(() => {
if (keypair) { if (keypair) {
return getUserVote(keypair.pub, yes, no); return getUserVote(keypair.pub, yes, no);
} }
@ -59,7 +61,7 @@ export function useUserVote(
}, [keypair, yes, no]); }, [keypair, yes, no]);
// If user vote changes update the vote state // If user vote changes update the vote state
React.useEffect(() => { useEffect(() => {
if (!userVote) { if (!userVote) {
setVoteState(VoteState.NotCast); setVoteState(VoteState.NotCast);
} else { } else {
@ -71,21 +73,19 @@ export function useUserVote(
// Starts a timeout of 30s to set a failed message if // Starts a timeout of 30s to set a failed message if
// the vote is not seen by the time the callback is invoked // the vote is not seen by the time the callback is invoked
React.useEffect(() => { useEffect(() => {
// eslint-disable-next-line if (voteState === VoteState.Pending && !timeout) {
let timeout: any; const timeout = setTimeout(() => {
if (voteState === VoteState.Pending) {
setTimeout(() => {
setVoteState(VoteState.Failed); setVoteState(VoteState.Failed);
captureMessage('Vote not seen after 30s'); captureMessage('Vote not seen after 30s');
}, 1000 * 30); }, 1000 * 30);
} else { setTimeoutValue(timeout);
} else if (voteState !== VoteState.Pending) {
clearTimeout(timeout); clearTimeout(timeout);
} }
return () => clearTimeout(timeout); return () => clearTimeout(timeout);
}, [voteState]); }, [timeout, voteState]);
/** /**
* Casts a vote using the users connected wallet * Casts a vote using the users connected wallet