From 7d7adc0ea0630f011567a13104738725c026ebc0 Mon Sep 17 00:00:00 2001 From: Justas Bartka <> Date: Wed, 19 Apr 2023 01:07:02 -0400 Subject: [PATCH] fixed timout issue while in game --- services/app/src/pages/game/Game.jsx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/services/app/src/pages/game/Game.jsx b/services/app/src/pages/game/Game.jsx index 5c1931bf..f6556ba2 100644 --- a/services/app/src/pages/game/Game.jsx +++ b/services/app/src/pages/game/Game.jsx @@ -29,6 +29,29 @@ const Game = () => { const { currentPlayer, lastTurn, removeFigureFromBoard, isAnotherSessionActive } = useChessBoardContext(); const [showAvatarModal, setShowAvatarModal] = useState(false); + const [isInGame, setIsInGame] = useState(true); + + useEffect(() => { + let timeoutId; + + const handleVisibilityChange = () => { + if (document.hidden) { + timeoutId = setTimeout(() => { + setIsInGame(false) + }, 5 * 60 * 1000); + } else { + clearTimeout(timeoutId); + } + }; + + document.addEventListener('visibilitychange', handleVisibilityChange); + + return () => { + document.removeEventListener('visibilitychange', handleVisibilityChange); + clearTimeout(timeoutId); + }; + }, []); + const connectToWs = useCallback(() => { openWsConnection({ @@ -68,6 +91,7 @@ const Game = () => { return (
{isAnotherSessionActive && } + {!isInGame && } setShowAvatarModal(false)}