Skip to content

Commit 688c2ce

Browse files
committed
chore: code refactoring
1 parent f550273 commit 688c2ce

File tree

6 files changed

+140
-87
lines changed

6 files changed

+140
-87
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import React from 'react'
2+
3+
import {
4+
BulkSelection,
5+
ClusterFiltersType,
6+
SortableTableHeaderCell,
7+
useUrlFilters,
8+
} from '@devtron-labs/devtron-fe-common-lib'
9+
10+
import { ClusterListRow } from './ClusterListRow'
11+
import { ClusterMapListSortableKeys, ClusterMapListSortableTitle } from './constants'
12+
import { ClusterListTypes } from './types'
13+
import { parseSearchParams } from './utils'
14+
15+
export const ClusterList = ({
16+
filteredList,
17+
clusterListLoader,
18+
showKubeConfigModal,
19+
onChangeShowKubeConfigModal,
20+
setSelectedClusterName,
21+
}: ClusterListTypes) => {
22+
const { sortBy, sortOrder, handleSorting } = useUrlFilters<
23+
ClusterMapListSortableKeys,
24+
{ clusterFilter: ClusterFiltersType }
25+
>({
26+
parseSearchParams,
27+
initialSortKey: ClusterMapListSortableKeys.CLUSTER_NAME,
28+
})
29+
30+
const handleCellSorting = (cellToSort: ClusterMapListSortableKeys) => () => {
31+
handleSorting(cellToSort)
32+
}
33+
34+
return (
35+
<div data-testid="cluster-list-container" className="flexbox-col flex-grow-1">
36+
<div className="cluster-list-row fw-6 cn-7 fs-12 dc__border-bottom pt-8 pb-8 pr-20 pl-20 dc__uppercase bg__primary dc__position-sticky dc__top-0 dc__zi-3">
37+
{Object.entries(ClusterMapListSortableKeys).map(([cellName, cellKey]) => (
38+
<React.Fragment key={cellName}>
39+
{cellKey === ClusterMapListSortableKeys.CLUSTER_NAME && (
40+
<BulkSelection showPagination={false} />
41+
)}
42+
<SortableTableHeaderCell
43+
key={cellName}
44+
title={ClusterMapListSortableTitle[cellName]}
45+
isSorted={sortBy === cellKey}
46+
sortOrder={sortOrder}
47+
isSortable
48+
disabled={false}
49+
triggerSorting={handleCellSorting(cellKey)}
50+
/>
51+
</React.Fragment>
52+
))}
53+
</div>
54+
{filteredList.map((clusterData) => (
55+
<ClusterListRow
56+
clusterData={clusterData}
57+
clusterListLoader={clusterListLoader}
58+
showKubeConfigModal={showKubeConfigModal}
59+
onChangeShowKubeConfigModal={onChangeShowKubeConfigModal}
60+
setSelectedClusterName={setSelectedClusterName}
61+
/>
62+
))}
63+
</div>
64+
)
65+
}

src/components/ClusterNodes/ClusterListRow.tsx

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,26 @@ import { ClusterMapInitialStatus } from './ClusterMapInitialStatus'
2828
import { CLUSTER_PROD_TYPE } from './constants'
2929
import { ClusterListRowTypes } from './types'
3030

31+
const CompareClusterButton = importComponentFromFELibrary('CompareClusterButton', null, 'function')
32+
const ClusterStatusCell = importComponentFromFELibrary('ClusterStatus', null, 'function')
3133
const KubeConfigButton = importComponentFromFELibrary('KubeConfigButton', null, 'function')
3234

33-
export const ClusterListRow = ({ clusterData, index, clusterListLoader }: ClusterListRowTypes) => {
34-
const ClusterStatusCell = importComponentFromFELibrary('ClusterStatus', null, 'function')
35-
const CompareClusterButton = importComponentFromFELibrary('CompareClusterButton', null, 'function')
36-
35+
export const ClusterListRow = ({
36+
clusterData,
37+
clusterListLoader,
38+
onChangeShowKubeConfigModal,
39+
setSelectedClusterName,
40+
}: ClusterListRowTypes) => {
3741
const {
3842
selectedIdentifiers: bulkSelectionState,
39-
isBulkSelectionApplied,
4043
handleBulkSelection,
4144
getSelectedIdentifiersCount,
4245
} = useBulkSelection<BulkSelectionIdentifiersType<ClusterDetail>>()
4346
const errorCount = clusterData.nodeErrors ? Object.keys(clusterData.nodeErrors).length : 0
4447

4548
const handleSelection = () => {
4649
const { name } = clusterData
47-
if (isBulkSelectionApplied) {
48-
handleBulkSelection({
49-
action: BulkSelectionEvents.CLEAR_IDENTIFIERS_AFTER_ACROSS_SELECTION,
50-
data: {
51-
identifierIds: [name],
52-
},
53-
})
54-
} else if (bulkSelectionState[name]) {
50+
if (bulkSelectionState[name]) {
5551
handleBulkSelection({
5652
action: BulkSelectionEvents.CLEAR_IDENTIFIERS,
5753
data: {
@@ -69,10 +65,9 @@ export const ClusterListRow = ({ clusterData, index, clusterListLoader }: Cluste
6965
},
7066
})
7167
}
68+
setSelectedClusterName('')
7269
}
7370

74-
const terminalURL = `${URLS.RESOURCE_BROWSER}/${clusterData.id}/all}/${AppDetailsTabs.terminal}/${K8S_EMPTY_GROUP}`
75-
7671
const hideDataOnLoad = (value) => {
7772
if (clusterListLoader) {
7873
return null
@@ -87,7 +82,7 @@ export const ClusterListRow = ({ clusterData, index, clusterListLoader }: Cluste
8782

8883
return <ClusterMapInitialStatus errorInNodeListing={errorInNodeListing} />
8984
}
90-
const isIdentifierSelected = !!bulkSelectionState[clusterData.name] || isBulkSelectionApplied
85+
const isIdentifierSelected = !!bulkSelectionState[clusterData.name]
9186

9287
return (
9388
<div
@@ -99,22 +94,21 @@ export const ClusterListRow = ({ clusterData, index, clusterListLoader }: Cluste
9994
className={
10095
isIdentifierSelected || getSelectedIdentifiersCount() > 0
10196
? 'dc__visible'
102-
: 'dc__visible-hover--child flexbox'
97+
: 'dc__visible-hover--child flex left'
10398
}
10499
>
105100
<Checkbox
106101
isChecked={isIdentifierSelected}
107102
onChange={handleSelection}
108103
rootClassName="icon-dim-20 m-0"
109104
dataTestId={`activate-${clusterData.name}`}
110-
tabIndex={index - 1}
111105
value={CHECKBOX_VALUE.CHECKED}
112106
/>
113107
</div>
114108

115109
{!isIdentifierSelected && getSelectedIdentifiersCount() === 0 && (
116110
<div className="dc__visible-hover--hide-child flex left">
117-
<Icon name="ic-bg-cluster" color={null} size={20} />
111+
<Icon name="ic-bg-cluster" color={null} size={24} />
118112
</div>
119113
)}
120114
<div data-testid={`cluster-row-${clusterData.name}`} className="flex left dc__overflow-hidden">
@@ -137,14 +131,20 @@ export const ClusterListRow = ({ clusterData, index, clusterListLoader }: Cluste
137131
variant={ButtonVariantType.borderLess}
138132
component={ButtonComponentType.link}
139133
linkProps={{
140-
to: terminalURL,
134+
to: `${URLS.RESOURCE_BROWSER}/${clusterData.id}/${ALL_NAMESPACE_OPTION.value}/${AppDetailsTabs.terminal}/${K8S_EMPTY_GROUP}`,
141135
}}
142136
/>
143137
)}
144138
{CompareClusterButton && clusterData.status !== ClusterStatusType.CONNECTION_FAILED && (
145139
<CompareClusterButton sourceClusterId={clusterData.id} isIconButton />
146140
)}
147-
{KubeConfigButton && <KubeConfigButton clusterName={clusterData.name} />}
141+
{KubeConfigButton && (
142+
<KubeConfigButton
143+
onChangeShowKubeConfigModal={onChangeShowKubeConfigModal}
144+
clusterName={clusterData.name}
145+
setSelectedClusterName={setSelectedClusterName}
146+
/>
147+
)}
148148
</div>
149149
</div>
150150
</div>

src/components/ClusterNodes/ClusterOverview.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
getUrlWithSearchParams,
2727
showError,
2828
ClusterCapacityType,
29+
Icon,
2930
} from '@devtron-labs/devtron-fe-common-lib'
3031
import {
3132
ClusterErrorType,
@@ -412,7 +413,7 @@ function ClusterOverview({ selectedCluster, addTab }: ClusterOverviewProps) {
412413
<aside className="flexbox-col dc__gap-16 w-300 dc__no-shrink">
413414
<div className="flexbox-col dc__gap-12">
414415
<div>
415-
<ClusterOverviewIcon className="icon-dim-48" />
416+
<Icon name='ic-bg-cluster' size={48} color={null} />
416417
</div>
417418
<div className="fs-16 fw-7 lh-24 cn-9 font-merriweather" data-testid="clusterOveviewName">
418419
{clusterDetails?.clusterName}

src/components/ClusterNodes/ClusterSelectionList.tsx

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

17-
import React, { useEffect, useMemo, useState } from 'react'
17+
import React, { useMemo, useState } from 'react'
1818
import dayjs, { Dayjs } from 'dayjs'
1919

2020
import {
21-
BulkSelection,
2221
BulkSelectionEvents,
2322
BulkSelectionIdentifiersType,
2423
BulkSelectionProvider,
@@ -27,7 +26,6 @@ import {
2726
GenericEmptyState,
2827
SearchBar,
2928
SelectAllDialogStatus,
30-
SortableTableHeaderCell,
3129
useBulkSelection,
3230
useUrlFilters,
3331
} from '@devtron-labs/devtron-fe-common-lib'
@@ -38,10 +36,10 @@ import Timer from '@Components/common/DynamicTabs/DynamicTabs.timer'
3836
import { AddClusterButton } from '@Components/ResourceBrowser/PageHeader.buttons'
3937

4038
import { ClusterSelectionType } from '../ResourceBrowser/Types'
41-
import { ClusterListRow } from './ClusterListRow'
39+
import { ClusterList } from './ClusterList'
4240
import ClusterNodeEmptyState from './ClusterNodeEmptyStates'
43-
import { ClusterMapListSortableKeys, ClusterMapListSortableTitle, ClusterStatusByFilter } from './constants'
44-
import { getSortedClusterList } from './utils'
41+
import { ClusterMapListSortableKeys, ClusterStatusByFilter } from './constants'
42+
import { getSortedClusterList, parseSearchParams } from './utils'
4543

4644
import './clusterNodes.scss'
4745

@@ -52,10 +50,7 @@ const ClusterBulkSelectionActionWidget = importComponentFromFELibrary(
5250
null,
5351
'function',
5452
)
55-
56-
const parseSearchParams = (searchParams: URLSearchParams) => ({
57-
clusterFilter: (searchParams.get('clusterFilter') as ClusterFiltersType) || ClusterFiltersType.ALL_CLUSTERS,
58-
})
53+
const KubeConfigModal = importComponentFromFELibrary('KubeConfigModal', null, 'function')
5954

6055
const ClusterSelectionList: React.FC<ClusterSelectionType> = ({
6156
clusterOptions,
@@ -65,34 +60,18 @@ const ClusterSelectionList: React.FC<ClusterSelectionType> = ({
6560
parentRef,
6661
}) => {
6762
const [lastSyncTime, setLastSyncTime] = useState<Dayjs>(dayjs())
63+
const [showKubeConfigModal, setKubeConfigModal] = useState(false)
64+
const [selectedClusterName, setSelectedClusterName] = useState('')
6865

69-
const {
70-
searchKey,
71-
clusterFilter,
72-
updateSearchParams,
73-
handleSearch,
74-
clearFilters,
75-
sortBy,
76-
sortOrder,
77-
handleSorting,
78-
} = useUrlFilters<ClusterMapListSortableKeys, { clusterFilter: ClusterFiltersType }>({
79-
parseSearchParams,
80-
initialSortKey: ClusterMapListSortableKeys.CLUSTER_NAME,
81-
})
66+
const { searchKey, clusterFilter, updateSearchParams, handleSearch, clearFilters, sortBy, sortOrder } =
67+
useUrlFilters<ClusterMapListSortableKeys, { clusterFilter: ClusterFiltersType }>({
68+
parseSearchParams,
69+
initialSortKey: ClusterMapListSortableKeys.CLUSTER_NAME,
70+
})
8271

83-
const { handleBulkSelection, setIdentifiers, isBulkSelectionApplied, getSelectedIdentifiersCount } =
72+
const { handleBulkSelection, isBulkSelectionApplied, getSelectedIdentifiersCount } =
8473
useBulkSelection<BulkSelectionIdentifiersType<ClusterDetail>>()
8574

86-
useEffect(() => {
87-
if (clusterOptions.length) {
88-
const clusterIdentifier: BulkSelectionIdentifiersType<ClusterDetail> = {}
89-
clusterOptions.forEach((cluster) => {
90-
clusterIdentifier[cluster.name] = cluster
91-
})
92-
setIdentifiers(clusterIdentifier)
93-
}
94-
}, [clusterOptions])
95-
9675
const filteredList: ClusterDetail[] = useMemo(() => {
9776
const loweredSearchKey = searchKey.toLowerCase()
9877
const updatedClusterOptions = [...clusterOptions]
@@ -141,11 +120,15 @@ const ClusterSelectionList: React.FC<ClusterSelectionType> = ({
141120
)
142121
}
143122

144-
const handleCellSorting = (cellToSort: ClusterMapListSortableKeys) => () => {
145-
handleSorting(cellToSort)
123+
const onChangeShowKubeConfigModal = () => {
124+
setKubeConfigModal(true)
146125
}
147126

148-
const renderClusterListContent = () => (
127+
const onChangeCloseKubeConfigModal = () => {
128+
setKubeConfigModal(false)
129+
}
130+
131+
const renderClusterList = () => (
149132
<div className="cluster-list-main-container flex-grow-1 flexbox-col bg__primary dc__overflow-auto">
150133
<div className="flexbox dc__content-space pl-20 pr-20 pt-16 pb-16 dc__zi-4">
151134
<div className="flex dc__gap-12">
@@ -198,29 +181,13 @@ const ClusterSelectionList: React.FC<ClusterSelectionType> = ({
198181
<ClusterNodeEmptyState actionHandler={clearFilters} />
199182
</div>
200183
) : (
201-
<div data-testid="cluster-list-container" className="flexbox-col flex-grow-1">
202-
<div className="cluster-list-row fw-6 cn-7 fs-12 dc__border-bottom pt-8 pb-8 pr-20 pl-20 dc__uppercase bg__primary dc__position-sticky dc__top-0 dc__zi-3">
203-
{Object.entries(ClusterMapListSortableKeys).map(([cellName, cellKey]) => (
204-
<>
205-
{cellKey === ClusterMapListSortableKeys.CLUSTER_NAME && (
206-
<BulkSelection showPagination={false} />
207-
)}
208-
<SortableTableHeaderCell
209-
key={cellName}
210-
title={ClusterMapListSortableTitle[cellName]}
211-
isSorted={sortBy === cellKey}
212-
sortOrder={sortOrder}
213-
isSortable
214-
disabled={false}
215-
triggerSorting={handleCellSorting(cellKey)}
216-
/>
217-
</>
218-
))}
219-
</div>
220-
{filteredList.map((clusterData, index) => (
221-
<ClusterListRow clusterData={clusterData} index={index} clusterListLoader={clusterListLoader} />
222-
))}
223-
</div>
184+
<ClusterList
185+
filteredList={filteredList}
186+
clusterListLoader={clusterListLoader}
187+
setSelectedClusterName={setSelectedClusterName}
188+
showKubeConfigModal={showKubeConfigModal}
189+
onChangeShowKubeConfigModal={onChangeShowKubeConfigModal}
190+
/>
224191
)}
225192
</div>
226193
)
@@ -232,6 +199,7 @@ const ClusterSelectionList: React.FC<ClusterSelectionType> = ({
232199
parentRef={parentRef}
233200
count={isBulkSelectionApplied ? clusterOptions?.length ?? 0 : getSelectedIdentifiersCount()}
234201
handleClearBulkSelection={handleClearBulkSelection}
202+
onChangeShowKubeConfigModal={onChangeShowKubeConfigModal}
235203
/>
236204
)
237205
}
@@ -241,7 +209,14 @@ const ClusterSelectionList: React.FC<ClusterSelectionType> = ({
241209
return (
242210
<div ref={parentRef} className="flexbox-col flex-grow-1">
243211
{renderClusterBulkSelection()}
244-
{renderClusterListContent()}
212+
{renderClusterList()}
213+
{showKubeConfigModal && KubeConfigModal && (
214+
<KubeConfigModal
215+
clusterName={selectedClusterName || getSelectedIdentifiersCount() === 0}
216+
handleModalClose={onChangeCloseKubeConfigModal}
217+
isSingleClusterButton={!!selectedClusterName}
218+
/>
219+
)}
245220
</div>
246221
)
247222
}
@@ -257,10 +232,12 @@ const BaseClusterList = (props: ClusterSelectionType) => {
257232
[clusterOptions],
258233
)
259234

235+
const getSelectAllDialogStatus = () => SelectAllDialogStatus.CLOSED
236+
260237
return (
261238
<BulkSelectionProvider<BulkSelectionIdentifiersType<ClusterDetail>>
262239
identifiers={allOnThisPageIdentifiers}
263-
getSelectAllDialogStatus={() => SelectAllDialogStatus.CLOSED}
240+
getSelectAllDialogStatus={getSelectAllDialogStatus}
264241
>
265242
<ClusterSelectionList {...props} />
266243
</BulkSelectionProvider>

src/components/ClusterNodes/types.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,13 @@ export interface ClusterMapInitialStatusType {
294294
errorInNodeListing: string
295295
}
296296

297-
export interface ClusterListRowTypes {
298-
clusterData: ClusterDetail
299-
index: number
297+
export interface ClusterListTypes {
298+
filteredList: ClusterDetail[]
300299
clusterListLoader: boolean
300+
showKubeConfigModal: boolean
301+
onChangeShowKubeConfigModal: () => void
302+
setSelectedClusterName: React.Dispatch<React.SetStateAction<string>>
303+
}
304+
export interface ClusterListRowTypes extends Omit<ClusterListTypes, 'filteredList'> {
305+
clusterData: ClusterDetail
301306
}

0 commit comments

Comments
 (0)