Skip to content

Commit 928aa1d

Browse files
egor-zalenskiArtem
authored andcommitted
#RI-3438 - Some Keys are displayed with empty names when page was scrolled and then reopened
1 parent 577f2a1 commit 928aa1d

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

redisinsight/ui/src/pages/browser/components/key-list/KeyList.spec.tsx

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React from 'react'
22
import { render } from 'uiSrc/utils/test-utils'
3-
import { KeysStoreData } from 'uiSrc/slices/interfaces/keys'
3+
import { KeysStoreData, KeyViewType } from 'uiSrc/slices/interfaces/keys'
4+
import { keysSelector, setLastBatchKeys } from 'uiSrc/slices/browser/keys'
45
import KeyList from './KeyList'
56

67
const propsMock = {
@@ -41,6 +42,20 @@ const propsMock = {
4142
handleAddKeyPanel: jest.fn(),
4243
}
4344

45+
jest.mock('uiSrc/slices/browser/keys', () => ({
46+
...jest.requireActual('uiSrc/slices/browser/keys'),
47+
setLastBatchKeys: jest.fn(),
48+
keysSelector: jest.fn().mockResolvedValue({
49+
viewType: 'Browser',
50+
isSearch: false,
51+
isFiltered: false,
52+
}),
53+
}))
54+
55+
afterEach(() => {
56+
setLastBatchKeys.mockRestore()
57+
})
58+
4459
describe('KeyList', () => {
4560
it('should render', () => {
4661
expect(render(<KeyList {...propsMock} />)).toBeTruthy()
@@ -53,4 +68,34 @@ describe('KeyList', () => {
5368
)
5469
expect(rows).toHaveLength(3)
5570
})
71+
72+
it('should call "setLastBatchKeys" after unmount for Browser view', () => {
73+
keysSelector.mockImplementation(() => ({
74+
viewType: KeyViewType.Browser,
75+
isSearch: false,
76+
isFiltered: false,
77+
}))
78+
79+
const { unmount } = render(<KeyList {...propsMock} />)
80+
expect(setLastBatchKeys).not.toBeCalled()
81+
82+
unmount()
83+
84+
expect(setLastBatchKeys).toBeCalledTimes(1)
85+
})
86+
87+
it('should not call "setLastBatchKeys" after unmount for Tree view', () => {
88+
keysSelector.mockImplementation(() => ({
89+
viewType: KeyViewType.Tree,
90+
isSearch: false,
91+
isFiltered: false,
92+
}))
93+
94+
const { unmount } = render(<KeyList {...propsMock} />)
95+
expect(setLastBatchKeys).not.toBeCalled()
96+
97+
unmount()
98+
99+
expect(setLastBatchKeys).not.toBeCalled()
100+
})
56101
})

redisinsight/ui/src/pages/browser/components/key-list/KeyList.tsx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useRef, useState } from 'react'
1+
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState } from 'react'
22
import { useDispatch, useSelector } from 'react-redux'
33
import cx from 'classnames'
44

@@ -27,6 +27,7 @@ import {
2727
keysDataSelector,
2828
keysSelector,
2929
selectedKeySelector,
30+
setLastBatchKeys,
3031
sourceKeysFetch,
3132
} from 'uiSrc/slices/browser/keys'
3233
import {
@@ -35,7 +36,7 @@ import {
3536
} from 'uiSrc/slices/app/context'
3637
import { GroupBadge } from 'uiSrc/components'
3738
import { SCAN_COUNT_DEFAULT } from 'uiSrc/constants/api'
38-
import { KeysStoreData } from 'uiSrc/slices/interfaces/keys'
39+
import { KeysStoreData, KeyViewType } from 'uiSrc/slices/interfaces/keys'
3940
import VirtualTable from 'uiSrc/components/virtual-table/VirtualTable'
4041
import { ITableColumn } from 'uiSrc/components/virtual-table/interfaces'
4142
import { OVER_RENDER_BUFFER_COUNT, TableCellAlignment, TableCellTextAlignment } from 'uiSrc/constants'
@@ -62,7 +63,7 @@ const KeyList = forwardRef((props: Props, ref) => {
6263

6364
const { data: selectedKey } = useSelector(selectedKeySelector)
6465
const { total, nextCursor, previousResultCount } = useSelector(keysDataSelector)
65-
const { isSearched, isFiltered } = useSelector(keysSelector)
66+
const { isSearched, isFiltered, viewType } = useSelector(keysSelector)
6667
const { keyList: { scrollTopPosition } } = useSelector(appContextBrowser)
6768

6869
const [items, setItems] = useState(keysState.keys)
@@ -77,6 +78,17 @@ const KeyList = forwardRef((props: Props, ref) => {
7778
}
7879
}))
7980

81+
useEffect(() =>
82+
() => {
83+
if (viewType === KeyViewType.Tree) {
84+
return
85+
}
86+
setItems((prevItems) => {
87+
dispatch(setLastBatchKeys(prevItems.slice(-SCAN_COUNT_DEFAULT)))
88+
return []
89+
})
90+
}, [])
91+
8092
useEffect(() => {
8193
const newKeys = bufferFormatRangeItems(keysState.keys, 0, OVER_RENDER_BUFFER_COUNT, formatItem)
8294

redisinsight/ui/src/slices/browser/keys.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ const keysSlice = createSlice({
131131
state.error = payload
132132
},
133133

134+
setLastBatchKeys: (state, { payload }) => {
135+
const newKeys = state.data.keys
136+
newKeys.splice(-payload.length, payload.length, ...payload)
137+
state.data.keys = newKeys
138+
},
139+
134140
loadKeyInfoSuccess: (state, { payload }) => {
135141
state.selectedKey = {
136142
...state.selectedKey,
@@ -348,6 +354,7 @@ export const {
348354
defaultSelectedKeyAction,
349355
defaultSelectedKeyActionSuccess,
350356
defaultSelectedKeyActionFailure,
357+
setLastBatchKeys,
351358
addKey,
352359
addKeySuccess,
353360
addKeyFailure,

redisinsight/ui/src/slices/tests/browser/keys.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import reducer, {
5454
addListKey,
5555
addStringKey,
5656
addZsetKey,
57+
setLastBatchKeys,
5758
updateSelectedKeyRefreshTime,
5859
} from '../../browser/keys'
5960
import { getString } from '../../browser/string'
@@ -375,6 +376,39 @@ describe('keys slice', () => {
375376
})
376377
})
377378

379+
describe('setLastBatchKeys', () => {
380+
it('should properly set the state', () => {
381+
// Arrange
382+
const strToKey = (name:string) => ({ name, nameString: name, ttl: 1, size: 1, type: 'hash' })
383+
const data = ['44', '55', '66'].map(strToKey)
384+
385+
const state = {
386+
...initialState,
387+
data: {
388+
...initialState.data,
389+
keys: ['1', '2', '3', '44', '55', '66'].map(strToKey),
390+
}
391+
}
392+
393+
const prevState = {
394+
...initialState,
395+
data: {
396+
...initialState.data,
397+
keys: ['1', '2', '3', '4', '5', '6'].map(strToKey),
398+
}
399+
}
400+
401+
// Act
402+
const nextState = reducer(prevState, setLastBatchKeys(data))
403+
404+
// Assert
405+
const rootState = Object.assign(initialStateDefault, {
406+
browser: { keys: nextState },
407+
})
408+
expect(keysSelector(rootState)).toEqual(state)
409+
})
410+
})
411+
378412
describe('refreshKeyInfo', () => {
379413
it('should properly set the state', () => {
380414
// Arrange

0 commit comments

Comments
 (0)