Skip to content

Commit d892a35

Browse files
authored
Merge pull request #2766 from devtron-labs/fix/online-connectivity-36
chore: online & cluster selector fix
2 parents 50a0fe7 + 3a5a998 commit d892a35

File tree

2 files changed

+25
-59
lines changed

2 files changed

+25
-59
lines changed

src/components/ResourceBrowser/ResourceList/ClusterSelector.tsx

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,27 @@ const ClusterSelector: React.FC<ClusterSelectorType> = ({
114114

115115
{defaultOption?.isProd && <span className="px-6 py-2 br-4 bcb-1 cb-7 fs-12 lh-16 fw-5">Production</span>}
116116

117-
<PopupMenu autoClose>
118-
<PopupMenu.Button rootClassName="flex ml-auto p-4 border__secondary" isKebab>
119-
<MenuDots className="icon-dim-16 fcn-7" data-testid="popup-menu-button" />
120-
</PopupMenu.Button>
121-
122-
<PopupMenu.Body rootClassName="dc__border p-4">
123-
<div className="w-150 flexbox-col">
124-
<button
125-
type="button"
126-
className="dc__outline-none flexbox dc__gap-8 dc__transparent dc__hover-n50 px-12 py-6 dc__align-items-center"
127-
onClick={handleOpenDeleteModal}
128-
data-testid="delete_cluster_button"
129-
>
130-
<Icon name="ic-delete" color="R500" />
131-
<span className="fs-14 lh-1-5 cr-5">Delete</span>
132-
</button>
133-
</div>
134-
</PopupMenu.Body>
135-
</PopupMenu>
117+
{defaultOption?.value !== String(DEFAULT_CLUSTER_ID) && (
118+
<PopupMenu autoClose>
119+
<PopupMenu.Button rootClassName="flex ml-auto p-4 border__secondary" isKebab>
120+
<MenuDots className="icon-dim-16 fcn-7" data-testid="popup-menu-button" />
121+
</PopupMenu.Button>
122+
123+
<PopupMenu.Body rootClassName="dc__border p-4">
124+
<div className="w-150 flexbox-col">
125+
<button
126+
type="button"
127+
className="dc__outline-none flexbox dc__gap-8 dc__transparent dc__hover-n50 px-12 py-6 dc__align-items-center"
128+
onClick={handleOpenDeleteModal}
129+
data-testid="delete_cluster_button"
130+
>
131+
<Icon name="ic-delete" color="R500" />
132+
<span className="fs-14 lh-1-5 cr-5">Delete</span>
133+
</button>
134+
</div>
135+
</PopupMenu.Body>
136+
</PopupMenu>
137+
)}
136138

137139
{openDeleteClusterModal && (
138140
<DeleteClusterConfirmationModal

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

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,21 @@
1-
import { useEffect, useRef, useState } from 'react'
1+
import { useEffect, useState } from 'react'
22

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

5-
import { getInternetConnectivity } from '@Services/service'
6-
7-
import { INTERNET_CONNECTIVITY_INTERVAL } from '../constants'
8-
95
export const useOnline = ({ onOnline = noop, onOffline = noop }: { onOnline?: () => void; onOffline?: () => void }) => {
106
const [online, setOnline] = useState(structuredClone(navigator.onLine))
11-
const abortControllerRef = useRef<AbortController>(new AbortController())
12-
const timeoutRef = useRef<NodeJS.Timeout>(null)
137
const { isAirgapped } = useMainContext()
148

15-
const checkConnectivity = async () => {
16-
if (isAirgapped) return
17-
// Cancel any pending request
18-
if (abortControllerRef.current) {
19-
abortControllerRef.current.abort()
20-
}
21-
22-
const controller = new AbortController()
23-
abortControllerRef.current = controller
24-
25-
try {
26-
await getInternetConnectivity(abortControllerRef.current)
27-
setOnline(true)
28-
if (online) {
29-
onOnline()
30-
}
31-
} catch {
32-
setOnline(false)
33-
} finally {
34-
timeoutRef.current = setTimeout(checkConnectivity, INTERNET_CONNECTIVITY_INTERVAL)
35-
}
36-
}
379
const handleOffline = () => {
38-
if (timeoutRef.current) {
39-
clearTimeout(timeoutRef.current)
40-
}
41-
abortControllerRef.current.abort()
4210
setOnline(false)
4311
if (onOffline) {
4412
onOffline()
4513
}
4614
}
4715

48-
const handleOnline = async () => {
49-
// Verify connectivity when browser reports online
50-
await checkConnectivity()
16+
const handleOnline = () => {
17+
setOnline(navigator.onLine)
18+
onOnline()
5119
}
5220

5321
useEffect(() => {
@@ -56,12 +24,8 @@ export const useOnline = ({ onOnline = noop, onOffline = noop }: { onOnline?: ()
5624
window.addEventListener('offline', handleOffline)
5725

5826
return () => {
59-
if (timeoutRef.current) {
60-
clearTimeout(timeoutRef.current)
61-
}
6227
window.removeEventListener('offline', handleOffline)
6328
window.removeEventListener('online', handleOnline)
64-
abortControllerRef.current.abort()
6529
}
6630
}, [isAirgapped])
6731

0 commit comments

Comments
 (0)