Skip to content

Commit ec20c3b

Browse files
committed
fix: multiple polling from useOnline
1 parent 438ea59 commit ec20c3b

File tree

2 files changed

+20
-31
lines changed

2 files changed

+20
-31
lines changed

src/components/common/Banner/Banner.tsx

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useCallback, useEffect, useRef, useState } from 'react'
17+
import { useEffect, useRef, useState } from 'react'
1818

1919
import {
2020
AnimatePresence,
@@ -83,36 +83,24 @@ export const Banner = () => {
8383
const [showAnnouncementBanner, setShowAnnouncementBanner] = useState(
8484
ANNOUNCEMENT_CONFIG.message ? shouldShowAnnouncementBanner() : false,
8585
)
86-
const hasShownOnlineBanner = useRef(false)
8786
const onlineTimer = useRef<ReturnType<typeof setTimeout>>(null)
8887

89-
const onOnline = useCallback(() => {
90-
// Only show the online banner if we haven't shown it since the last offline state
91-
if (!hasShownOnlineBanner.current) {
92-
setShowOnlineBanner(true)
93-
hasShownOnlineBanner.current = true
94-
95-
// Clear any existing timer before setting a new one
96-
if (onlineTimer.current) {
97-
clearTimeout(onlineTimer.current)
98-
}
99-
100-
onlineTimer.current = setTimeout(() => setShowOnlineBanner(false), ONLINE_BANNER_TIMEOUT)
88+
const onOnline = () => {
89+
if (onlineTimer.current) {
90+
clearTimeout(onlineTimer.current)
10191
}
102-
}, [])
10392

104-
const onOffline = () => {
105-
hasShownOnlineBanner.current = false
93+
setShowOnlineBanner(true)
94+
onlineTimer.current = setTimeout(() => setShowOnlineBanner(false), ONLINE_BANNER_TIMEOUT)
10695
}
10796

108-
const isOnline = useOnline({ onOnline, onOffline })
97+
const isOnline = useOnline({ onOnline })
10998

11099
useEffect(
111100
() => () => {
112101
if (onlineTimer.current) {
113102
clearTimeout(onlineTimer.current)
114103
}
115-
hasShownOnlineBanner.current = false
116104
},
117105
[],
118106
)

src/components/common/hooks/useOnline/useOnline.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { useEffect, useRef, useState } from 'react'
22

3-
import { noop, useMainContext } from '@devtron-labs/devtron-fe-common-lib'
3+
import { getIsRequestAborted, noop, useMainContext } from '@devtron-labs/devtron-fe-common-lib'
44

55
import { getInternetConnectivity } from '@Services/service'
66

77
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
88

9-
export const useOnline = ({ onOnline = noop, onOffline = noop }: { onOnline?: () => void; onOffline?: () => void }) => {
9+
export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
1010
const [online, setOnline] = useState(structuredClone(navigator.onLine))
1111
const abortControllerRef = useRef<AbortController>(new AbortController())
1212
const timeoutRef = useRef<NodeJS.Timeout>(null)
@@ -24,14 +24,18 @@ export const useOnline = ({ onOnline = noop, onOffline = noop }: { onOnline?: ()
2424

2525
try {
2626
await getInternetConnectivity(abortControllerRef.current)
27-
setOnline(true)
28-
if (online) {
29-
onOnline()
30-
}
31-
} catch {
32-
setOnline(false)
33-
} finally {
27+
setOnline((prev) => {
28+
if (!prev) {
29+
onOnline()
30+
}
31+
return true
32+
})
3433
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
34+
} catch (error) {
35+
setOnline(false)
36+
if (!getIsRequestAborted(error)) {
37+
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
38+
}
3539
}
3640
}
3741
const handleOffline = () => {
@@ -40,9 +44,6 @@ export const useOnline = ({ onOnline = noop, onOffline = noop }: { onOnline?: ()
4044
}
4145
abortControllerRef.current.abort()
4246
setOnline(false)
43-
if (onOffline) {
44-
onOffline()
45-
}
4647
}
4748

4849
const handleOnline = async () => {

0 commit comments

Comments
 (0)