Skip to content

Commit 7f369f2

Browse files
committed
#RI-5162 - fix page re-render only when located on the same page
1 parent b04e6a7 commit 7f369f2

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

redisinsight/ui/src/pages/instance/InstancePage.tsx

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { EuiResizableContainer } from '@elastic/eui'
2-
import React, { useCallback, useEffect, useState } from 'react'
2+
import React, { useCallback, useEffect, useRef, useState } from 'react'
33
import { useDispatch, useSelector } from 'react-redux'
4-
import { useParams } from 'react-router-dom'
4+
import { useLocation, useParams } from 'react-router-dom'
55
import cx from 'classnames'
66

77
import { setInitialAnalyticsSettings } from 'uiSrc/slices/analytics/settings'
@@ -34,6 +34,7 @@ import { setClusterDetailsInitialState } from 'uiSrc/slices/analytics/clusterDet
3434
import { setDatabaseAnalysisInitialState } from 'uiSrc/slices/analytics/dbAnalysis'
3535
import { resetRedisearchKeysData, setRedisearchInitialState } from 'uiSrc/slices/browser/redisearch'
3636
import { setTriggeredFunctionsInitialState } from 'uiSrc/slices/triggeredFunctions/triggeredFunctions'
37+
import { getPageName } from 'uiSrc/utils/routing'
3738
import InstancePageRouter from './InstancePageRouter'
3839

3940
import styles from './styles.module.scss'
@@ -66,12 +67,16 @@ const InstancePage = ({ routes = [] }: Props) => {
6667
const [isShouldChildrenRerender, setIsShouldChildrenRerender] = useState(false)
6768

6869
const dispatch = useDispatch()
70+
const { pathname } = useLocation()
71+
6972
const { instanceId: connectionInstanceId } = useParams<{ instanceId: string }>()
7073
const { isShowCli, isShowHelper } = useSelector(cliSettingsSelector)
7174
const { data: modulesData } = useSelector(instancesSelector)
7275
const { isShowMonitor } = useSelector(monitorSelector)
7376
const { contextInstanceId } = useSelector(appContextSelector)
7477

78+
const lastPageRef = useRef<string>()
79+
7580
const isShowBottomGroup = isShowCli || isShowHelper || isShowMonitor
7681

7782
useEffect(() => {
@@ -82,9 +87,10 @@ const InstancePage = ({ routes = [] }: Props) => {
8287
dispatch(fetchConnectedInstanceInfoAction(connectionInstanceId))
8388

8489
if (contextInstanceId && contextInstanceId !== connectionInstanceId) {
85-
// rerender children from scratch to clear all component states
86-
setIsShouldChildrenRerender(true)
87-
requestAnimationFrame(() => setIsShouldChildrenRerender(false))
90+
// rerender children only if the same page from scratch to clear all component states
91+
if (lastPageRef.current === getPageName(connectionInstanceId, pathname)) {
92+
setIsShouldChildrenRerender(true)
93+
}
8894

8995
resetContext()
9096
}
@@ -93,6 +99,14 @@ const InstancePage = ({ routes = [] }: Props) => {
9399
dispatch(setDbConfig(localStorageService.get(BrowserStorageItem.dbConfig + connectionInstanceId)))
94100
}, [connectionInstanceId])
95101

102+
useEffect(() => {
103+
lastPageRef.current = getPageName(connectionInstanceId, pathname)
104+
}, [pathname])
105+
106+
useEffect(() => {
107+
if (isShouldChildrenRerender) setIsShouldChildrenRerender(false)
108+
}, [isShouldChildrenRerender])
109+
96110
useEffect(() => () => {
97111
setSizes((prevSizes: ResizablePanelSize) => {
98112
localStorageService.set(BrowserStorageItem.cliResizableContainer, {

redisinsight/ui/src/slices/oauth/cloud.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
} from 'uiSrc/components/notifications/components'
1313
import successMessages from 'uiSrc/components/notifications/success-messages'
1414
import { getCloudSsoUtmParams } from 'uiSrc/utils/oauth/cloudSsoUtm'
15-
import { resetKeys } from 'uiSrc/slices/browser/keys'
1615
import { CloudUser } from 'apiSrc/modules/cloud/user/models'
1716
import { CloudJobInfo } from 'apiSrc/modules/cloud/job/models'
1817
import { CloudSubscriptionPlanResponse } from 'apiSrc/modules/cloud/subscription/dto'
@@ -33,7 +32,6 @@ import {
3332
removeInfiniteNotification
3433
} from '../app/notifications'
3534
import { checkConnectToInstanceAction, setConnectedInstanceId } from '../instances/instances'
36-
import { setAppContextInitialState } from '../app/context'
3735

3836
export const initialState: StateAppOAuth = {
3937
loading: false,
@@ -245,8 +243,6 @@ export function createFreeDbSuccess(id: string, history: any) {
245243
dispatch(removeInfiniteNotification(InfiniteMessagesIds.oAuthSuccess))
246244

247245
if (!isConnected) {
248-
dispatch(resetKeys())
249-
dispatch(setAppContextInitialState())
250246
dispatch(setConnectedInstanceId(id ?? ''))
251247
dispatch(checkConnectToInstanceAction(id))
252248
}

redisinsight/ui/src/utils/routing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,5 @@ export const getRedirectionPage = (pageInput: string, databaseId?: string): Null
4343
return `/${page}`
4444
}
4545
}
46+
47+
export const getPageName = (databaseId: string, path: string) => path?.replace(`/${databaseId}`, '')

redisinsight/ui/src/utils/tests/routing.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getRedirectionPage } from 'uiSrc/utils/routing'
1+
import { getPageName, getRedirectionPage } from 'uiSrc/utils/routing'
22

33
jest.mock('uiSrc/utils/routing', () => ({
44
...jest.requireActual('uiSrc/utils/routing')
@@ -25,3 +25,20 @@ describe('getRedirectionPage', () => {
2525
}
2626
)
2727
})
28+
29+
const getPageNameTests = [
30+
{ input: ['instanceId', '/instanceId/page1'], expected: '/page1' },
31+
{ input: ['instanceId', '/instanceId/page1/page2'], expected: '/page1/page2' },
32+
{ input: ['instanceId', '/page1'], expected: '/page1' },
33+
]
34+
35+
describe('getPageName', () => {
36+
test.each(getPageNameTests)(
37+
'%j',
38+
({ input, expected }) => {
39+
// @ts-ignore
40+
const result = getPageName(...input)
41+
expect(result).toEqual(expected)
42+
}
43+
)
44+
})

0 commit comments

Comments
 (0)