Skip to content

Commit e253302

Browse files
committed
chore: code refactoring
1 parent 0a5b562 commit e253302

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed
Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1-
export const getFallbackInternetConnectivity = (controller: AbortController): Promise<any> => {
2-
const timeoutId = setTimeout(() => {
3-
controller.abort()
4-
}, 20000)
5-
6-
return fetch('https://www.google.com/favicon.ico', {
7-
method: 'HEAD',
8-
mode: 'no-cors',
9-
signal: controller.signal,
10-
}).finally(() => {
11-
clearTimeout(timeoutId)
12-
})
1+
import { Routes } from '@Config/constants'
2+
3+
const DEFAULT_TIMEOUT = 10000
4+
5+
const fetchWithTimeout = (url: string, options: RequestInit, controller: AbortController): Promise<any> => {
6+
const timeoutId = setTimeout(() => controller.abort(), DEFAULT_TIMEOUT)
7+
8+
return fetch(url, { ...options, signal: controller.signal }).finally(() => clearTimeout(timeoutId))
9+
}
10+
11+
export const getFallbackInternetConnectivity = (controller: AbortController): Promise<any> =>
12+
fetchWithTimeout(
13+
'https://www.google.com/favicon.ico',
14+
{
15+
method: 'HEAD',
16+
mode: 'no-cors',
17+
},
18+
controller,
19+
)
20+
21+
export const getInternetConnectivity = (controller: AbortController): Promise<any> => {
22+
const baseUrl = window._env_?.CENTRAL_API_ENDPOINT ?? 'https://api.devtron.ai'
23+
const url = `${baseUrl}/${Routes.HEALTH}`
24+
25+
return fetchWithTimeout(url, {}, controller).then((res) => res.json())
1326
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { useEffect, useRef, useState } from 'react'
22

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

5-
import { getInternetConnectivity } from '@Services/service'
6-
75
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
8-
import { getFallbackInternetConnectivity } from './service'
6+
import { getFallbackInternetConnectivity, getInternetConnectivity } from './service'
97

108
export const useOnline = ({ onOnline = noop }: { onOnline?: () => void }) => {
119
const [online, setOnline] = useState(structuredClone(navigator.onLine))

src/services/service.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,11 @@ import { Routes } from '../config'
3737
import {
3838
CDPipelines,
3939
AppListMin,
40-
ProjectFilteredApps,
4140
AppOtherEnvironment,
4241
ClusterEnvironmentDetailList,
4342
ClusterListResponse,
4443
LoginCountType,
4544
ConfigOverrideWorkflowDetailsResponse,
46-
AllWorkflows,
4745
MinChartRefDTO,
4846
ClusterEnvTeams,
4947
} from './service.types'
@@ -558,16 +556,3 @@ export const validateContainerConfiguration = (request: any): Promise<any> => {
558556
export const getTemplateOptions = (appId: number, envId: number): Promise<ResponseType<TemplateListDTO[]>> =>
559557
get(getUrlWithSearchParams(Routes.DEPLOYMENT_OPTIONS, { appId, envId }))
560558

561-
export const getInternetConnectivity = (controller: AbortController): Promise<any> => {
562-
const timeoutId = setTimeout(() => {
563-
controller.abort()
564-
}, 10000)
565-
566-
return fetch(`${window._env_?.CENTRAL_API_ENDPOINT ?? 'https://api.devtron.ai'}/${Routes.HEALTH}`, {
567-
signal: controller.signal,
568-
})
569-
.then((res) => res.json())
570-
.finally(() => {
571-
clearTimeout(timeoutId)
572-
})
573-
}

0 commit comments

Comments
 (0)