From 2ac3d950c3cd6407c69d7d24afca3225add03eb6 Mon Sep 17 00:00:00 2001 From: victoriari Date: Fri, 14 Jun 2024 15:30:38 +0300 Subject: [PATCH 1/3] Fixed bugs on Jackpots page --- .../app/scenes/jackpots/JackpotTableRow.tsx | 4 +- .../src/app/services/hooks/use-manage-play.ts | 18 +++++--- .../src/app/services/hooks/use-websockets.ts | 44 ++++++++++++++++--- client/src/app/types/Round.ts | 2 +- 4 files changed, 54 insertions(+), 14 deletions(-) diff --git a/client/src/app/scenes/jackpots/JackpotTableRow.tsx b/client/src/app/scenes/jackpots/JackpotTableRow.tsx index e997c8d..3b9b3fd 100644 --- a/client/src/app/scenes/jackpots/JackpotTableRow.tsx +++ b/client/src/app/scenes/jackpots/JackpotTableRow.tsx @@ -14,7 +14,7 @@ const JackpotTableRow = ({ round }: { round: Round }) => { winnerPublicKey, jackpotAmount, endedAt, - deployHash, + lastPlayDeployHash, } = round; const roundPath = `/jackpot/${roundId}`; @@ -33,7 +33,7 @@ const JackpotTableRow = ({ round }: { round: Round }) => { diff --git a/client/src/app/services/hooks/use-manage-play.ts b/client/src/app/services/hooks/use-manage-play.ts index adaee40..a123efd 100644 --- a/client/src/app/services/hooks/use-manage-play.ts +++ b/client/src/app/services/hooks/use-manage-play.ts @@ -73,7 +73,7 @@ const useManagePlay = (): ManagePlayData => { }, [executedDeploy]); const onReceiveWsMessage = (message: { data: string }) => { - if (isDeploy(message.data)) { + if (message.data) { const deploy = JSON.parse(message.data) as DeployMessage; setExecutedDeploy(deploy.data); } @@ -82,6 +82,8 @@ const useManagePlay = (): ManagePlayData => { const onCloseWsConnection = () => { if (!executedDeploy) { setPlayResultState(errorState); + } else { + setExecutedDeploy(null); } }; @@ -112,13 +114,17 @@ const useManagePlay = (): ManagePlayData => { activeAccountWithBalance.public_key ); - const preparedDeploy = await preparePlayDeploy( - parsedActivePublicKey - ); - try { + const preparedDeploy = await preparePlayDeploy( + parsedActivePublicKey + ); + await signAndSendDeploy(preparedDeploy, parsedActivePublicKey); - setPlayResultState({ ...playResultState, loading: true }); + setPlayResultState({ + ...playResultState, + loading: true, + error: false, + }); handleOpenConnection(); } catch (e) { setPlayResultState(errorState); diff --git a/client/src/app/services/hooks/use-websockets.ts b/client/src/app/services/hooks/use-websockets.ts index ec94bac..04d71c7 100644 --- a/client/src/app/services/hooks/use-websockets.ts +++ b/client/src/app/services/hooks/use-websockets.ts @@ -1,4 +1,5 @@ import { useCallback, useEffect, useState } from 'react'; +import { isDeploy } from '../../utils/formatters'; interface WebSocketMessage { data: string; @@ -9,7 +10,7 @@ interface UseWebSocketsProps { onMessage: (message: WebSocketMessage) => void; onClose: () => void; } - +const disconnectTimeout = 60000; const logOpenWsConnection = () => console.log('open ws connection'); export const useWebSockets = ({ @@ -21,6 +22,17 @@ export const useWebSockets = ({ null as unknown as WebSocket ); + let timeoutId; + function resetMessageTimeout() { + clearTimeout(timeoutId); + timeoutId = setTimeout(function () { + console.log( + 'No deploy received for 1 min, closing connection...' + ); + close(); + }, disconnectTimeout); + } + const addEventHandler = ( type: string, callback: (value: any) => void @@ -32,9 +44,22 @@ export const useWebSockets = ({ }; }; - useEffect(() => addEventHandler('open', onOpen), [session, onOpen]); useEffect( - () => addEventHandler('message', onMessage), + () => + addEventHandler('open', () => { + onOpen && onOpen(); + resetMessageTimeout(); + }), + [session, onOpen] + ); + useEffect( + () => + addEventHandler('message', event => { + if (isDeploy(event.data)) { + onMessage(event); + resetMessageTimeout(); + } + }), [session, onMessage] ); useEffect( @@ -42,17 +67,26 @@ export const useWebSockets = ({ [session, onClose] ); + useEffect( + () => + addEventHandler('error', () => { + close(); + }), + [session] + ); + const connect = useCallback((publicKey: string) => { const url = `${config.lottery_api_ws_url}?caller_public_key=${publicKey}`; const ws = new WebSocket(url); setSession(ws); }, []); - const close = useCallback(() => { + const close = () => { if (session?.readyState === session?.OPEN) { session?.close(); + setSession(null as unknown as WebSocket); } - }, [session]); + }; return { connect, close, readyState: session?.readyState }; }; diff --git a/client/src/app/types/Round.ts b/client/src/app/types/Round.ts index d827def..7024c3e 100644 --- a/client/src/app/types/Round.ts +++ b/client/src/app/types/Round.ts @@ -5,5 +5,5 @@ export interface Round { roundId: string; winnerAccountHash: string; winnerPublicKey: string; - deployHash?: string; + lastPlayDeployHash: string; } From 88cf89b4d1111c57d5c4293e420bee0b8182a490 Mon Sep 17 00:00:00 2001 From: victoriari Date: Fri, 14 Jun 2024 17:19:13 +0300 Subject: [PATCH 2/3] Renamed constant --- client/src/app/services/hooks/use-websockets.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/app/services/hooks/use-websockets.ts b/client/src/app/services/hooks/use-websockets.ts index 04d71c7..6cc41fc 100644 --- a/client/src/app/services/hooks/use-websockets.ts +++ b/client/src/app/services/hooks/use-websockets.ts @@ -10,7 +10,7 @@ interface UseWebSocketsProps { onMessage: (message: WebSocketMessage) => void; onClose: () => void; } -const disconnectTimeout = 60000; +const DISCONNECT_TIMEOUT = 60000; const logOpenWsConnection = () => console.log('open ws connection'); export const useWebSockets = ({ @@ -30,7 +30,7 @@ export const useWebSockets = ({ 'No deploy received for 1 min, closing connection...' ); close(); - }, disconnectTimeout); + }, DISCONNECT_TIMEOUT); } const addEventHandler = ( From ec07b77b9e4093413397e7e4657c0a5e710da4e7 Mon Sep 17 00:00:00 2001 From: victoriari Date: Fri, 14 Jun 2024 20:14:35 +0300 Subject: [PATCH 3/3] Fixed total count naming on Jackpot plays page --- client/src/app/scenes/round/index.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/client/src/app/scenes/round/index.tsx b/client/src/app/scenes/round/index.tsx index bf30714..cad4966 100644 --- a/client/src/app/scenes/round/index.tsx +++ b/client/src/app/scenes/round/index.tsx @@ -53,7 +53,6 @@ const JackpotRoundTable = ({ id }: { id: string }) => { handleReset={resetLimit} /> )} - itemsLabel={'round'} /> ); };