Skip to content

Commit 6df13a5

Browse files
committed
#RI-3561 - fix pr comments
1 parent 00cd57c commit 6df13a5

File tree

6 files changed

+51
-79
lines changed

6 files changed

+51
-79
lines changed

redisinsight/ui/src/components/cli/components/cli-body/CliBody/styles.module.scss

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
@import '@elastic/eui/src/global_styling/mixins/helpers';
2-
@import '@elastic/eui/src/components/table/mixins';
3-
@import '@elastic/eui/src/global_styling/index';
1+
@import "@elastic/eui/src/global_styling/mixins/helpers";
2+
@import "@elastic/eui/src/components/table/mixins";
3+
@import "@elastic/eui/src/global_styling/index";
44

55
.section {
66
position: absolute;
@@ -52,4 +52,5 @@
5252

5353
:global(.cli-command-wrapper) {
5454
font: normal normal bold 14px/15px Inconsolata !important;
55+
white-space: pre-line;
5556
}
Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,12 @@
11
import React from 'react'
22
import { instance, mock } from 'ts-mockito'
33
import { render } from 'uiSrc/utils/test-utils'
4-
import QueryCardCliDefaultResult, { MAX_CARD_HEIGHT, MIN_ROWS_COUNT, Props } from './QueryCardCliDefaultResult'
4+
import QueryCardCliDefaultResult, { Props } from './QueryCardCliDefaultResult'
55

66
const mockedProps = mock<Props>()
77

88
describe('QueryCardCliDefaultResult', () => {
99
it('should render', () => {
1010
expect(render(<QueryCardCliDefaultResult {...instance(mockedProps)} />)).toBeTruthy()
1111
})
12-
13-
it(`container min-height should be ${MAX_CARD_HEIGHT} if items.length > ${MIN_ROWS_COUNT}`, () => {
14-
const mockResult: string[] = Array.from({ length: MIN_ROWS_COUNT + 1 }).fill('123')
15-
16-
const { queryByTestId } = render(
17-
<QueryCardCliDefaultResult {...instance(mockedProps)} items={mockResult} />
18-
)
19-
20-
const resultEl = queryByTestId('query-cli-card-result')
21-
22-
expect(resultEl).toHaveStyle(`min-height: ${MAX_CARD_HEIGHT}px`)
23-
expect(resultEl).toHaveTextContent(mockResult.join(''))
24-
})
25-
26-
it(
27-
`container min-height should be calculated and less than ${MAX_CARD_HEIGHT} if items.length < ${MIN_ROWS_COUNT} `,
28-
() => {
29-
const mockResult: string[] = Array.from({ length: 5 }).fill('123')
30-
31-
const { queryByTestId } = render(
32-
<QueryCardCliDefaultResult {...instance(mockedProps)} items={mockResult} />
33-
)
34-
35-
const resultEl = queryByTestId('query-cli-card-result')
36-
37-
expect(resultEl).not.toHaveStyle(`min-height: ${MAX_CARD_HEIGHT}px`)
38-
expect(resultEl).toHaveStyle('min-height: 90px')
39-
expect(resultEl).toHaveTextContent(mockResult.join(''))
40-
}
41-
)
4212
})

redisinsight/ui/src/components/query-card/QueryCardCliDefaultResult/QueryCardCliDefaultResult.tsx

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import React, { useCallback, useEffect, useState } from 'react'
1+
import React from 'react'
22
import cx from 'classnames'
33

44
import VirtualList from 'uiSrc/components/virtual-list'
5-
import { getNodeText } from 'uiSrc/utils'
65

76
import styles from './styles.module.scss'
87

@@ -11,50 +10,25 @@ export interface Props {
1110
isFullScreen?: boolean
1211
}
1312

14-
export const MIN_ROW_HEIGHT = 18
1513
export const MIN_ROWS_COUNT = 11
1614
export const MAX_CARD_HEIGHT = 210
17-
export const SYMBOL_WIDTH = 4.5
1815

1916
const QueryCardCliDefaultResult = (props: Props) => {
2017
const { items = [], isFullScreen } = props
2118

22-
const [windowDimensions, setWindowDimensions] = useState({ width: 0, height: 0 })
23-
24-
const calculateHeight = useCallback((sum: number, item: string | JSX.Element) => {
25-
const itemLength = getNodeText(item)?.length || 0
26-
const symbolsPerLine = (windowDimensions.width - 400) / SYMBOL_WIDTH
27-
28-
return sum + Math.ceil((itemLength / symbolsPerLine)) * MIN_ROW_HEIGHT
29-
}, [windowDimensions.width])
30-
31-
const calculatedHeight = items.length > MIN_ROWS_COUNT && !isFullScreen
32-
? MAX_CARD_HEIGHT
33-
: items.reduce(calculateHeight, 0)
34-
35-
useEffect(() => {
36-
updateWindowDimensions()
37-
globalThis.addEventListener('resize', updateWindowDimensions)
38-
return () => {
39-
globalThis.removeEventListener('resize', updateWindowDimensions)
40-
}
41-
}, [])
42-
43-
const updateWindowDimensions = () => {
44-
setWindowDimensions({ height: globalThis.innerHeight, width: globalThis.innerWidth })
45-
}
46-
4719
return (
4820
<div
49-
className={cx(styles.container, 'query-card-output-response-success')}
50-
style={{
51-
minHeight: isFullScreen
52-
? windowDimensions.height - 65
53-
: Math.min(calculatedHeight, MAX_CARD_HEIGHT)
54-
}}
21+
className={cx(
22+
styles.container,
23+
'query-card-output-response-success',
24+
{ fullscreen: isFullScreen },
25+
)}
5526
data-testid="query-cli-card-result"
5627
>
57-
<VirtualList items={items} />
28+
<VirtualList
29+
items={items}
30+
dynamicHeight={!isFullScreen ? { itemsCount: MIN_ROWS_COUNT, maxHeight: MAX_CARD_HEIGHT } : undefined}
31+
/>
5832
</div>
5933
)
6034
}

redisinsight/ui/src/components/query-card/QueryCardCliResultWrapper/QueryCardCliResultWrapper.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const QueryCardCliResultWrapper = (props: Props) => {
2727
return (
2828
<div className={cx('queryResultsContainer', styles.container)}>
2929
{!loading && (
30-
<div data-testid="query-cli-result">
30+
<div data-testid="query-cli-result" style={{ height: '100%' }}>
3131
{isNotStored && (
3232
<EuiText className={styles.alert} data-testid="query-cli-warning">
3333
<EuiIcon type="alert" className={styles.alertIcon} />

redisinsight/ui/src/components/query-card/styles.module.scss

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
@import '@elastic/eui/src/global_styling/mixins/helpers';
2-
@import '@elastic/eui/src/components/table/mixins';
3-
@import '@elastic/eui/src/global_styling/index';
1+
@import "@elastic/eui/src/global_styling/mixins/helpers";
2+
@import "@elastic/eui/src/components/table/mixins";
3+
@import "@elastic/eui/src/global_styling/index";
44

55
.containerWrapper {
66
min-width: 420px;
@@ -38,6 +38,9 @@
3838

3939
.container {
4040
border-color: var(--tableLightestBorderColor);
41+
display: flex;
42+
flex-direction: column;
43+
height: 100%;
4144
}
4245

4346
&.isOpen .container {
@@ -61,8 +64,9 @@
6164
color: var(--cliOutputResponseColor) !important;
6265
}
6366

64-
:global(.fullscreen .query-card-output-response-success){
67+
:global(.fullscreen .query-card-output-response-success) {
6568
max-height: calc(100vh - 65px);
69+
display: flex;
6670
}
6771

6872
:global(.query-card-output-response-fail) {

redisinsight/ui/src/components/virtual-list/VirtualList.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef } from 'react'
1+
import React, { useEffect, useRef, useState } from 'react'
22
import { ListChildComponentProps, VariableSizeList as List } from 'react-window'
33
import AutoSizer from 'react-virtualized-auto-sizer'
44

@@ -8,23 +8,33 @@ export interface Props {
88
items: (string | JSX.Element)[]
99
overscanCount?: number
1010
minRowHeight?: number
11+
dynamicHeight?: {
12+
itemsCount: number,
13+
maxHeight: number,
14+
}
1115
}
1216

1317
const PROTRUDING_OFFSET = 2
1418
const MIN_ROW_HEIGHT = 18
1519
const OVERSCAN_COUNT = 20
20+
const MAX_LIST_HEIGHT = globalThis.innerHeight
1621

1722
const VirtualList = (props: Props) => {
1823
const {
1924
items = [],
25+
dynamicHeight,
2026
overscanCount = OVERSCAN_COUNT,
2127
minRowHeight = MIN_ROW_HEIGHT,
2228
} = props
29+
const { itemsCount: dynamicItemsCount = 0, maxHeight: dynamicMaxHeight = MAX_LIST_HEIGHT } = dynamicHeight || {}
2330

2431
const listRef = useRef<List>(null)
2532
const rowHeights = useRef<{ [key: number]: number }>({})
2633
const outerRef = useRef<HTMLDivElement>(null)
2734

35+
const [listHeight, setListHeight] = useState(MIN_ROW_HEIGHT)
36+
const [, forceRender] = useState({})
37+
2838
const getRowHeight = (index: number) => (
2939
rowHeights.current[index] > minRowHeight ? (rowHeights.current[index] + 2) : minRowHeight
3040
)
@@ -34,12 +44,25 @@ const VirtualList = (props: Props) => {
3444
rowHeights.current = { ...rowHeights.current, [index]: size }
3545
}
3646

47+
const calculateHeight = () => {
48+
listRef.current?.resetAfterIndex(0)
49+
if (dynamicItemsCount && items.length > dynamicItemsCount) {
50+
setListHeight(dynamicMaxHeight)
51+
}
52+
53+
setListHeight(Math.min(
54+
items.reduce((prev, _item, index) => getRowHeight(index) + prev, 0),
55+
dynamicMaxHeight,
56+
))
57+
}
58+
3759
const Row = ({ index, style }: ListChildComponentProps) => {
3860
const rowRef = useRef<HTMLDivElement>(null)
3961

4062
useEffect(() => {
4163
if (rowRef.current) {
4264
setRowHeight(index, rowRef.current?.clientHeight)
65+
calculateHeight()
4366
}
4467
}, [rowRef])
4568

@@ -53,16 +76,16 @@ const VirtualList = (props: Props) => {
5376
}
5477

5578
return (
56-
<AutoSizer>
57-
{({ width, height }) => (
79+
<AutoSizer disableHeight={!!dynamicHeight} onResize={() => forceRender({})}>
80+
{({ width, height = 0 }) => (
5881
<List
5982
itemCount={items.length}
6083
itemSize={getRowHeight}
6184
ref={listRef}
6285
className={styles.listContent}
6386
outerRef={outerRef}
6487
overscanCount={overscanCount}
65-
height={height}
88+
height={height || listHeight}
6689
width={width - PROTRUDING_OFFSET}
6790
>
6891
{Row}

0 commit comments

Comments
 (0)