Skip to content

Commit

Permalink
fix: snapshot status client ping (#1343)
Browse files Browse the repository at this point in the history
* fix: ping only from focused tab

* fix: use visibilityChange to detect focused tab

* fix: add undefined check for document
  • Loading branch information
1emu authored Oct 18, 2023
1 parent a1ae920 commit 2476920
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/components/Debug/SnapshotStatus.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function SnapshotStatus() {
const t = useFormatMessage()
const [showTopBar, setShowTopBar] = useState(false)
const { setStatus } = useBurgerMenu()
const [ping, setPing] = useState(false)

const updateServiceStatus = async () => {
const status = await Governance.get().getSnapshotStatus()
Expand All @@ -34,10 +35,29 @@ export default function SnapshotStatus() {
}

useEffect(() => {
const intervalId = setInterval(updateServiceStatus, PING_INTERVAL_IN_MS)
return () => clearInterval(intervalId)
if (typeof document !== 'undefined') {
const handleVisibilityChange = () => {
setPing(!document.hidden)
}

document.addEventListener('visibilitychange', handleVisibilityChange)

return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange)
}
}
}, [])

useEffect(() => {
const intervalId = setInterval(() => {
if (ping) {
updateServiceStatus()
}
}, PING_INTERVAL_IN_MS)

return () => clearInterval(intervalId)
}, [ping])

return (
<div className={classNames(`SnapshotStatus__TopBar`, showTopBar && 'SnapshotStatus__TopBar--visible')}>
<WarningTriangle size="18" />
Expand Down

0 comments on commit 2476920

Please sign in to comment.