Skip to content

Commit

Permalink
Merge pull request #14 from make-software/fix-bugs-on-jackpots-page
Browse files Browse the repository at this point in the history
Fixed bugs on Jackpots page
  • Loading branch information
victoriari authored Jun 17, 2024
2 parents 63d4d89 + ec07b77 commit 594d976
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 15 deletions.
4 changes: 2 additions & 2 deletions client/src/app/scenes/jackpots/JackpotTableRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const JackpotTableRow = ({ round }: { round: Round }) => {
winnerPublicKey,
jackpotAmount,
endedAt,
deployHash,
lastPlayDeployHash,
} = round;

const roundPath = `/jackpot/${roundId}`;
Expand All @@ -33,7 +33,7 @@ const JackpotTableRow = ({ round }: { round: Round }) => {
<TableData />
<TableData>
<DeployTimestamp
deployHash={deployHash}
deployHash={lastPlayDeployHash}
timestamp={endedAt}
/>
</TableData>
Expand Down
1 change: 0 additions & 1 deletion client/src/app/scenes/round/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const JackpotRoundTable = ({ id }: { id: string }) => {
handleReset={resetLimit}
/>
)}
itemsLabel={'round'}
/>
);
};
Expand Down
18 changes: 12 additions & 6 deletions client/src/app/services/hooks/use-manage-play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -82,6 +82,8 @@ const useManagePlay = (): ManagePlayData => {
const onCloseWsConnection = () => {
if (!executedDeploy) {
setPlayResultState(errorState);
} else {
setExecutedDeploy(null);
}
};

Expand Down Expand Up @@ -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);
Expand Down
44 changes: 39 additions & 5 deletions client/src/app/services/hooks/use-websockets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useCallback, useEffect, useState } from 'react';
import { isDeploy } from '../../utils/formatters';

interface WebSocketMessage {
data: string;
Expand All @@ -9,7 +10,7 @@ interface UseWebSocketsProps {
onMessage: (message: WebSocketMessage) => void;
onClose: () => void;
}

const DISCONNECT_TIMEOUT = 60000;
const logOpenWsConnection = () => console.log('open ws connection');

export const useWebSockets = ({
Expand All @@ -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();
}, DISCONNECT_TIMEOUT);
}

const addEventHandler = (
type: string,
callback: (value: any) => void
Expand All @@ -32,27 +44,49 @@ 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(
() => addEventHandler('close', onClose),
[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 };
};
2 changes: 1 addition & 1 deletion client/src/app/types/Round.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export interface Round {
roundId: string;
winnerAccountHash: string;
winnerPublicKey: string;
deployHash?: string;
lastPlayDeployHash: string;
}

0 comments on commit 594d976

Please sign in to comment.