Skip to content

Commit 9db4eda

Browse files
committed
#RI-5763 - add possibility for tutorials to open current page with tutorial id
1 parent b641c1e commit 9db4eda

File tree

4 files changed

+41
-13
lines changed

4 files changed

+41
-13
lines changed

redisinsight/ui/src/components/markdown/RedisInsightLink/RedisInsightLink.spec.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@ import { render, screen, fireEvent } from 'uiSrc/utils/test-utils'
44

55
import RedisInsightLink from './RedisInsightLink'
66

7+
jest.mock('uiSrc/utils/routing', () => ({
8+
...jest.requireActual('uiSrc/utils/routing')
9+
}))
10+
11+
Object.defineProperty(window, 'location', {
12+
value: {
13+
origin: 'http://localhost',
14+
pathname: 'instanceId/workbench'
15+
},
16+
writable: true
17+
})
18+
719
describe('RedisInsightLink', () => {
820
it('should render', () => {
921
expect(render(<RedisInsightLink url="/" text="label" />)).toBeTruthy()
@@ -13,11 +25,10 @@ describe('RedisInsightLink', () => {
1325
const pushMock = jest.fn()
1426
reactRouterDom.useHistory = jest.fn().mockReturnValue({ push: pushMock })
1527

16-
// getRedirectionPage is mocked and already has tests
17-
render(<RedisInsightLink url="/" text="label" />)
28+
render(<RedisInsightLink url="/settings" text="label" />)
1829

1930
fireEvent.click(screen.getByTestId('redisinsight-link'))
2031

21-
expect(pushMock).toHaveBeenCalledWith('/')
32+
expect(pushMock).toHaveBeenCalledWith('/settings')
2233
})
2334
})

redisinsight/ui/src/components/markdown/RedisInsightLink/RedisInsightLink.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { useState } from 'react'
22
import { EuiLink, EuiPopover } from '@elastic/eui'
33
import { useHistory, useParams } from 'react-router-dom'
44
import cx from 'classnames'
5+
import { isNull } from 'lodash'
56
import { getRedirectionPage } from 'uiSrc/utils/routing'
67
import DatabaseNotOpened from 'uiSrc/components/messages/database-not-opened'
78

@@ -22,18 +23,15 @@ const RedisInsightLink = (props: Props) => {
2223
const handleLinkClick = (e: React.MouseEvent) => {
2324
e.preventDefault()
2425

25-
if (!instanceId) {
26-
setIsPopoverOpen(true)
27-
return
28-
}
29-
3026
const href = getRedirectionPage(url, instanceId)
3127
if (href) {
3228
history.push(href)
3329
return
3430
}
3531

36-
history.push('/')
32+
if (isNull(href)) {
33+
setIsPopoverOpen(true)
34+
}
3735
}
3836

3937
return (

redisinsight/ui/src/utils/routing.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { IRoute } from 'uiSrc/constants'
22
import { Maybe, Nullable } from 'uiSrc/utils'
33
import DEFAULT_ROUTES from 'uiSrc/components/main-router/constants/defaultRoutes'
44

5+
const CURRENT_PAGE_URL_SYNTAX = '/_'
6+
57
export const findRouteByPathname = (routes: IRoute[], pathname: string): Maybe<IRoute> => {
68
let findRoute
79

@@ -22,18 +24,24 @@ export const findRouteByPathname = (routes: IRoute[], pathname: string): Maybe<I
2224
return findRoute
2325
}
2426

25-
export const getRedirectionPage = (pageInput: string, databaseId?: string): Nullable<string> => {
27+
// undefined - route was not found
28+
// null - route found but private
29+
export const getRedirectionPage = (pageInput: string, databaseId?: string): Nullable<Maybe<string>> => {
2630
let page = pageInput.replace(/^\//, '')
2731
try {
2832
const pageUrl = new URL(page, window.location.origin)
2933
const { pathname, searchParams } = pageUrl
3034

35+
if (pathname === CURRENT_PAGE_URL_SYNTAX) {
36+
return `${window.location.pathname}?${searchParams.toString()}`
37+
}
38+
3139
if (searchParams.has('guidePath') || searchParams.has('tutorialId')) {
3240
page += '&insights=open'
3341
}
3442

3543
const foundRoute = findRouteByPathname(DEFAULT_ROUTES, pathname)
36-
if (!foundRoute) return null
44+
if (!foundRoute) return undefined
3745

3846
if (foundRoute.path.includes(':instanceId')) {
3947
if (databaseId) {
@@ -44,7 +52,7 @@ export const getRedirectionPage = (pageInput: string, databaseId?: string): Null
4452

4553
return `/${page}`
4654
} catch (_e) {
47-
return `/${page}`
55+
return undefined
4856
}
4957
}
5058

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ jest.mock('uiSrc/utils/routing', () => ({
44
...jest.requireActual('uiSrc/utils/routing')
55
}))
66

7+
Object.defineProperty(window, 'location', {
8+
value: {
9+
origin: 'http://localhost',
10+
pathname: 'instanceId/workbench'
11+
},
12+
writable: true
13+
})
14+
715
const databaseId = '1'
816
const getRedirectionPageTests = [
917
{ input: ['settings'], expected: '/settings' },
@@ -12,8 +20,11 @@ const getRedirectionPageTests = [
1220
{ input: ['/analytics/slowlog', databaseId], expected: '/1/analytics/slowlog' },
1321
{ input: ['/analytics/slowlog'], expected: null },
1422
{ input: ['/analytics', databaseId], expected: '/1/analytics' },
15-
{ input: ['/analytics/page', databaseId], expected: null },
23+
{ input: ['/analytics/page', databaseId], expected: undefined },
24+
{ input: ['/analytics'], expected: null },
25+
{ input: ['some-page'], expected: undefined },
1626
{ input: ['/workbench?guidePath=introduction.md', databaseId], expected: '/1/workbench?guidePath=introduction.md&insights=open' },
27+
{ input: ['/_?tutorialId=tutorial'], expected: 'instanceId/workbench?tutorialId=tutorial' },
1728
]
1829

1930
describe('getRedirectionPage', () => {

0 commit comments

Comments
 (0)