Skip to content

Commit d30b96d

Browse files
committed
chore: code refactroing
1 parent b165267 commit d30b96d

File tree

4 files changed

+22
-49
lines changed

4 files changed

+22
-49
lines changed

src/components/common/hooks/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ export const FILE_READING_FAILED_STATUS = {
3333
}
3434

3535
export const ONLINE_BANNER_TIMEOUT = 3000
36-
export const INTERNET_CONNECTIVITY_INTERVAL = 20000
36+
export const INTERNET_CONNECTIVITY_INTERVAL = 30000

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,27 @@ import { Routes } from '@Config/constants'
33
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
44
import { CheckConnectivityParamsType, FetchConnectivityParamsType } from './types'
55

6-
const fetchWithTimeout = ({
7-
url,
8-
options,
9-
controller,
10-
setTimeoutRef,
11-
checkConnectivity,
12-
}: FetchConnectivityParamsType): Promise<any> => {
6+
const fetchWithTimeout = ({ url, options, controller }: FetchConnectivityParamsType): Promise<any> => {
137
const timeoutId = setTimeout(() => controller.abort(), INTERNET_CONNECTIVITY_INTERVAL)
148

159
return fetch(url, { ...options, signal: controller.signal }).finally(() => {
1610
clearTimeout(timeoutId)
17-
const nextRef = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
18-
setTimeoutRef(nextRef)
1911
})
2012
}
2113

22-
export const getFallbackInternetConnectivity = ({
23-
controller,
24-
setTimeoutRef,
25-
checkConnectivity,
26-
}: CheckConnectivityParamsType): Promise<any> =>
14+
export const getFallbackInternetConnectivity = ({ controller }: CheckConnectivityParamsType): Promise<any> =>
2715
fetchWithTimeout({
2816
url: 'https://www.google.com/favicon.ico',
2917
options: {
3018
method: 'HEAD',
3119
mode: 'no-cors',
3220
},
3321
controller,
34-
setTimeoutRef,
35-
checkConnectivity,
3622
})
3723

38-
export const getInternetConnectivity = ({
39-
controller,
40-
setTimeoutRef,
41-
checkConnectivity,
42-
}: CheckConnectivityParamsType): Promise<any> => {
24+
export const getInternetConnectivity = ({ controller }: CheckConnectivityParamsType): Promise<any> => {
4325
const baseUrl = window._env_?.CENTRAL_API_ENDPOINT ?? 'https://api.devtron.ai'
4426
const url = `${baseUrl}/${Routes.HEALTH}`
4527

46-
return fetchWithTimeout({ url, options: {}, controller, setTimeoutRef, checkConnectivity }).then((res) =>
47-
res.json(),
48-
)
28+
return fetchWithTimeout({ url, options: {}, controller })
4929
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ export interface FetchConnectivityParamsType {
22
url: string
33
options: RequestInit
44
controller: AbortController
5-
setTimeoutRef: (ref: NodeJS.Timeout) => void
6-
checkConnectivity: () => void
75
}
86

9-
export interface CheckConnectivityParamsType
10-
extends Pick<FetchConnectivityParamsType, 'controller' | 'setTimeoutRef' | 'checkConnectivity'> {}
7+
export interface CheckConnectivityParamsType extends Pick<FetchConnectivityParamsType, 'controller'> {}

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

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

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

55
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
66
import { getFallbackInternetConnectivity, getInternetConnectivity } from './service'
@@ -18,18 +18,12 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
1818
abortControllerRef.current.abort()
1919
}
2020

21-
const onConnectivitySuccess = () => {
21+
const onConnectivitySuccess = (checkConnectivity) => {
2222
setOnline((prev) => {
2323
if (!prev) onOnline()
2424
return true
2525
})
26-
}
27-
28-
const setTimeoutRef = (ref: NodeJS.Timeout) => {
29-
if (timeoutRef.current) {
30-
clearTimeout(timeoutRef.current)
31-
}
32-
timeoutRef.current = ref
26+
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
3327
}
3428

3529
const checkConnectivity = async () => {
@@ -42,26 +36,28 @@ export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
4236
try {
4337
await getFallbackInternetConnectivity({
4438
controller: abortControllerRef.current,
45-
setTimeoutRef,
46-
checkConnectivity,
4739
})
48-
onConnectivitySuccess()
49-
} catch (error) {
50-
if (getIsRequestAborted(error)) return
40+
onConnectivitySuccess(checkConnectivity)
41+
} catch {
42+
if (abortControllerRef.current.signal.aborted) {
43+
if (timeoutRef.current) {
44+
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
45+
}
46+
return
47+
}
5148
const fallbackController = new AbortController()
5249
abortControllerRef.current = fallbackController
5350
try {
5451
await getInternetConnectivity({
5552
controller: abortControllerRef.current,
56-
setTimeoutRef,
57-
checkConnectivity,
5853
})
59-
onConnectivitySuccess()
60-
} catch (fallbackError) {
61-
if (!getIsRequestAborted(fallbackError)) {
54+
onConnectivitySuccess(checkConnectivity)
55+
} catch {
56+
if (!abortControllerRef.current.signal.aborted) {
6257
setOnline(false)
58+
} else if (timeoutRef.current) {
59+
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
6360
}
64-
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
6561
}
6662
}
6763
}

0 commit comments

Comments
 (0)