Skip to content

Commit b4b9d53

Browse files
authored
Merge pull request #2982 from RedisInsight/fe/bugfix/capability-promotion
#RI-5348 - change logic to hide columns
2 parents 071fe23 + 83013c9 commit b4b9d53

File tree

9 files changed

+151
-46
lines changed

9 files changed

+151
-46
lines changed

redisinsight/ui/src/pages/home/HomePage.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ const HomePage = () => {
252252
hidden: !instances.length && !loading,
253253
fullWidth: !openRightPanel,
254254
openedRightPanel: !!openRightPanel,
255-
[styles.contentActive]: openRightPanel === RightPanelName.EditDatabase,
256255
})
257256
}}
258257
>
@@ -282,7 +281,6 @@ const HomePage = () => {
282281
className: cx('home__resizePanelRight', {
283282
hidden: !openRightPanel,
284283
fullWidth: !instances.length,
285-
[styles.contentActive]: openRightPanel === RightPanelName.EditDatabase,
286284
})
287285
}}
288286
id="form"

redisinsight/ui/src/pages/home/components/databases-list-component/DatabasesListWrapper.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export interface Props {
5353
}
5454

5555
const suffix = '_db_instance'
56+
const COLS_TO_HIDE = ['connectionType', 'modules', 'lastConnection']
5657

5758
const DatabasesListWrapper = ({
5859
width,
@@ -300,7 +301,6 @@ const DatabasesListWrapper = ({
300301
sortable: true,
301302
width: '180px',
302303
truncateText: true,
303-
hideForMobile: true,
304304
render: (cellData: ConnectionType) =>
305305
CONNECTION_TYPE_DISPLAY[cellData] || capitalize(cellData),
306306
},
@@ -409,7 +409,7 @@ const DatabasesListWrapper = ({
409409
width={width}
410410
editedInstance={editedInstance}
411411
columns={columns}
412-
columnsToHide={['connectionType', 'modules', 'lastConnection']}
412+
columnsToHide={COLS_TO_HIDE}
413413
onDelete={handleDeleteInstances}
414414
onExport={handleExportInstances}
415415
onWheel={closePopover}
@@ -418,4 +418,4 @@ const DatabasesListWrapper = ({
418418
)
419419
}
420420

421-
export default DatabasesListWrapper
421+
export default React.memo(DatabasesListWrapper)

redisinsight/ui/src/pages/home/components/databases-list-component/databases-list/DatabasesList.tsx

Lines changed: 52 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
PropertySort,
88
} from '@elastic/eui'
99
import cx from 'classnames'
10-
import { find } from 'lodash'
1110
import React, { useEffect, useRef, useState } from 'react'
1211
import { useSelector } from 'react-redux'
1312
import { instancesSelector } from 'uiSrc/slices/instances/instances'
@@ -18,6 +17,7 @@ import { localStorageService } from 'uiSrc/services'
1817
import { BrowserStorageItem } from 'uiSrc/constants'
1918

2019
import { ActionBar, DeleteAction, ExportAction } from './components'
20+
import { findColumn, getColumnWidth, hideColumn } from './utils'
2121

2222
import styles from '../styles.module.scss'
2323

@@ -31,7 +31,6 @@ export interface Props {
3131
onWheel: () => void
3232
}
3333

34-
const MIN_COLUMN_LENGTH = 180
3534
const loadingMsg = 'loading...'
3635

3736
function DatabasesList({
@@ -50,13 +49,20 @@ function DatabasesList({
5049

5150
const tableRef = useRef<EuiInMemoryTable<Instance>>(null)
5251
const containerTableRef = useRef<HTMLDivElement>(null)
52+
const hiddenCols = useRef<Set<string>>(new Set([]))
53+
const lastHiddenColumn = useRef<EuiTableFieldDataColumnType<Instance>>()
5354

5455
useEffect(() => {
55-
if (containerTableRef.current) {
56+
if (columnsToHide?.length && containerTableRef.current) {
5657
const { offsetWidth } = containerTableRef.current
57-
const columnsResults = adjustColumns(columns, offsetWidth)
58-
if (columnsResults?.length !== columns?.length) {
59-
setColumns(columnsResults)
58+
const beforeAdjustHiddenCols = hiddenCols.current.size
59+
try {
60+
const columnsResults = adjustColumns(columns, offsetWidth)
61+
if (beforeAdjustHiddenCols !== hiddenCols.current.size) {
62+
setColumns(columnsResults)
63+
}
64+
} catch (_) {
65+
// ignore
6066
}
6167
}
6268
}, [width])
@@ -70,41 +76,53 @@ function DatabasesList({
7076
onSelectionChange: (selected: Instance[]) => setSelection(selected),
7177
}
7278

73-
const getColumnWidth = (width?: string) => (width && /^[0-9]+px/.test(width) ? parseInt(width, 10) : MIN_COLUMN_LENGTH)
74-
7579
const adjustColumns = (
7680
cols: EuiTableFieldDataColumnType<Instance>[],
7781
offsetWidth: number,
78-
numberOfChangedColumns: number = 0
7982
): EuiTableFieldDataColumnType<Instance>[] => {
80-
const sum = cols?.reduce((prev, next) => {
81-
const columnWidth = getColumnWidth(next.width)
82-
return prev + columnWidth
83-
}, 0)
84-
85-
// remove columns
86-
if (sum > offsetWidth && columnsProp.length < columnsToHide.length + cols.length) {
87-
return adjustColumns(
88-
cols.filter(({ field }) => !columnsToHide.slice(0, numberOfChangedColumns + 1).includes(field)),
89-
offsetWidth,
90-
numberOfChangedColumns + 1
91-
)
83+
let sum = cols?.reduce((prev, next) => prev + getColumnWidth(next.width), 0)
84+
const visibleColumnsLength = cols.length - hiddenCols.current.size
85+
86+
// hide columns
87+
if (sum > offsetWidth && columnsToHide.length + visibleColumnsLength) {
88+
let resultsCol = [...cols]
89+
while (sum > offsetWidth) {
90+
const colToHide = columnsToHide[hiddenCols.current.size]
91+
const initialCol = findColumn(columnsProp, colToHide)
92+
if (!initialCol) return resultsCol
93+
94+
sum -= getColumnWidth(initialCol?.width)
95+
hiddenCols.current.add(colToHide)
96+
lastHiddenColumn.current = initialCol
97+
resultsCol = resultsCol.map((item) => (item.field === colToHide ? hideColumn(item) : item))
98+
}
99+
100+
return resultsCol
92101
}
93102

94-
// recover columns
95-
if (columnsProp.length > cols.length) {
96-
const lastRemovedColumnName = columnsToHide[columnsProp.length - cols.length - 1]
97-
if (lastRemovedColumnName) {
98-
const lastRemovedColumn = find(columnsProp, ({ field }) => field === lastRemovedColumnName)
99-
const lastRemovedColumnWidth = getColumnWidth(lastRemovedColumn?.width)
100-
101-
if (lastRemovedColumnWidth + sum < offsetWidth) {
102-
return adjustColumns(
103-
columnsProp.filter(({ field }) => find([...cols, lastRemovedColumn], (item) => item?.field === field)),
104-
offsetWidth,
105-
)
106-
}
103+
// show columns
104+
if (columnsProp.length > visibleColumnsLength) {
105+
// early return to not calculate other columns
106+
const lastHiddenColWidth = getColumnWidth(lastHiddenColumn.current?.width)
107+
if (sum + lastHiddenColWidth > offsetWidth) {
108+
return cols
107109
}
110+
111+
let resultsCol = [...cols]
112+
Array.from(hiddenCols.current).reverse().forEach((hiddenCol) => {
113+
const initialCol = findColumn(columnsProp, hiddenCol)
114+
if (!initialCol) return
115+
116+
const hiddenColWidth = getColumnWidth(initialCol.width)
117+
if (hiddenColWidth + sum < offsetWidth) {
118+
hiddenCols.current.delete(hiddenCol)
119+
sum += hiddenColWidth
120+
lastHiddenColumn.current = initialCol
121+
resultsCol = resultsCol.map((item) => (item.field === hiddenCol ? initialCol : item))
122+
}
123+
})
124+
125+
return resultsCol
108126
}
109127

110128
return cols
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { getColumnWidth, findColumn, hideColumn, MIN_COLUMN_WIDTH } from './utils'
2+
3+
const getColumnWidthTests: any[] = [
4+
['10px', 10],
5+
['350px', 350],
6+
['100', MIN_COLUMN_WIDTH],
7+
['x', MIN_COLUMN_WIDTH],
8+
]
9+
10+
describe('getColumnWidth', () => {
11+
it.each(getColumnWidthTests)('for input: %s (input), should be output: %s',
12+
(input, expected) => {
13+
const result = getColumnWidth(input)
14+
expect(result).toBe(expected)
15+
})
16+
})
17+
18+
const findColumnTests: any[] = [
19+
[
20+
[
21+
[{ field: '1' }, { field: '2' }],
22+
'1'
23+
],
24+
{ field: '1' }
25+
],
26+
[
27+
[
28+
[{ field: '1' }, { field: '2' }],
29+
'3'
30+
],
31+
undefined
32+
]
33+
]
34+
35+
describe('findColumn', () => {
36+
it.each(findColumnTests)('for input: %s (input), should be output: %s',
37+
(input, expected) => {
38+
const result = findColumn(...input as [any, string])
39+
expect(result).toEqual(expected)
40+
})
41+
})
42+
43+
const hideColumnTests: any[] = [
44+
[
45+
{ field: '1' },
46+
{ field: '1', width: '0px', className: 'hiddenColumn' }
47+
],
48+
]
49+
50+
describe('hideColumn', () => {
51+
it.each(hideColumnTests)('for input: %s (input), should be output: %s',
52+
(input, expected) => {
53+
const result = hideColumn(input)
54+
expect(result).toEqual(expected)
55+
})
56+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { EuiTableFieldDataColumnType } from '@elastic/eui'
2+
import { find } from 'lodash'
3+
import { Instance } from 'uiSrc/slices/interfaces'
4+
5+
const MIN_COLUMN_WIDTH = 180
6+
const getColumnWidth = (width?: string) => (width && /^[0-9]+px/.test(width) ? parseInt(width, 10) : MIN_COLUMN_WIDTH)
7+
const hideColumn = (column: EuiTableFieldDataColumnType<Instance>) => ({
8+
...column,
9+
width: '0px',
10+
className: 'hiddenColumn',
11+
})
12+
const findColumn = (columns: EuiTableFieldDataColumnType<Instance>[], colName: string) =>
13+
find(columns, ({ field }) => field === colName)
14+
15+
export {
16+
MIN_COLUMN_WIDTH,
17+
getColumnWidth,
18+
hideColumn,
19+
findColumn,
20+
}

redisinsight/ui/src/pages/home/components/databases-list-component/styles.module.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ $breakpoint-l: 1400px;
4646
margin-right: 5px;
4747
}
4848

49+
:global {
50+
.hiddenColumn {
51+
width: 0 !important;
52+
display: none !important;
53+
}
54+
}
55+
4956
.columnModules {
5057
height: 40px;
5158
padding-right: 0 !important;

redisinsight/ui/src/pages/home/components/welcome-component/WelcomeComponent.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ const WelcomeComponent = ({ onAddInstance }: Props) => {
228228
</EuiTitle>
229229
<CapabilityPromotion wrapperClassName={cx(styles.section, styles.capabilityPromotion)} mode="reduced" />
230230
</div>
231+
<EuiTitle className={styles.addDbTitle} size="s"><span>Add Redis databases</span></EuiTitle>
231232
<div className={styles.controlsGroup}>
232233
<EuiTitle className={styles.controlsGroupTitle} size="s">
233234
<h5>Redis Cloud Database</h5>

redisinsight/ui/src/pages/home/components/welcome-component/styles.module.scss

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,26 @@
3333
}
3434

3535
.controls {
36-
margin-top: 40px;
36+
margin-top: 32px;
3737
text-align: left;
3838
}
3939

4040
.controlsGroup {
41-
margin-bottom: 40px;
41+
margin-bottom: 32px;
42+
}
43+
44+
.addDbTitle {
45+
display: inline-block;
46+
width: 100%;
47+
48+
font-size: 18px !important;
49+
text-align: center;
50+
margin-bottom: 32px;
4251
}
4352

4453
.controlsGroupTitle {
4554
margin-bottom: 12px;
55+
font-size: 18px !important;
4656
}
4757

4858
.section {
@@ -131,7 +141,6 @@
131141

132142
.links {
133143
font-size: 14px;
134-
margin-top: 60px;
135144

136145
a {
137146
color: var(--euiTextColor);

redisinsight/ui/src/pages/home/styles.module.scss

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
@import "@elastic/eui/src/global_styling/index";
22
@import "uiSrc/styles/mixins/_global.scss";
33

4-
.contentActive {
5-
border: 1px solid var(--euiColorPrimary);
6-
}
7-
84
.pageHeader {
95
min-height: 56px;
106
}

0 commit comments

Comments
 (0)