Skip to content

Commit a94e56d

Browse files
committed
#RI-5766 - fix pathname
1 parent 0236ac6 commit a94e56d

File tree

5 files changed

+18
-12
lines changed

5 files changed

+18
-12
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ jest.mock('uiSrc/utils/routing', () => ({
1010

1111
Object.defineProperty(window, 'location', {
1212
value: {
13-
origin: 'http://localhost',
14-
pathname: 'instanceId/workbench'
13+
origin: 'http://localhost'
1514
},
1615
writable: true
1716
})

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from 'react'
22
import { EuiLink, EuiPopover } from '@elastic/eui'
3-
import { useHistory, useParams } from 'react-router-dom'
3+
import { useHistory, useLocation, useParams } from 'react-router-dom'
44
import cx from 'classnames'
55
import { isNull } from 'lodash'
66
import { getRedirectionPage } from 'uiSrc/utils/routing'
@@ -19,11 +19,12 @@ const RedisInsightLink = (props: Props) => {
1919
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
2020
const { instanceId } = useParams<{ instanceId: string }>()
2121
const history = useHistory()
22+
const location = useLocation()
2223

2324
const handleLinkClick = (e: React.MouseEvent) => {
2425
e.preventDefault()
2526

26-
const href = getRedirectionPage(url, instanceId)
27+
const href = getRedirectionPage(url, instanceId, location.pathname)
2728
if (href) {
2829
history.push(href)
2930
return

redisinsight/ui/src/services/resourcesService.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const resourcesService = axios.create({
1818
baseURL: RESOURCES_BASE_URL,
1919
})
2020

21+
// TODO: it seems it's shoudn't be location.origin
22+
// TODO: check all cases and rename this to getResourcesUrl
23+
// TODO: also might be helpful create function which returns origin url
2124
export const getOriginUrl = () => (IS_ABSOLUTE_PATH.test(RESOURCES_BASE_URL)
2225
? RESOURCES_BASE_URL
2326
: (window?.location?.origin || RESOURCES_BASE_URL))

redisinsight/ui/src/utils/routing.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@ export const findRouteByPathname = (routes: IRoute[], pathname: string): Maybe<I
2626

2727
// undefined - route was not found
2828
// null - route found but private
29-
export const getRedirectionPage = (pageInput: string, databaseId?: string): Nullable<Maybe<string>> => {
29+
export const getRedirectionPage = (
30+
pageInput: string,
31+
databaseId?: string,
32+
currentPathname?: string
33+
): Nullable<Maybe<string>> => {
3034
let page = pageInput.replace(/^\//, '')
3135
try {
32-
const pageUrl = new URL(page, window.location.origin)
33-
const { pathname, searchParams } = pageUrl
36+
const { pathname, searchParams } = new URL(page, window.location.origin)
3437

35-
if (pathname === CURRENT_PAGE_URL_SYNTAX) {
36-
return `${window.location.pathname}?${searchParams.toString()}`
38+
if (currentPathname && pathname === CURRENT_PAGE_URL_SYNTAX) {
39+
return `${currentPathname}?${searchParams.toString()}`
3740
}
3841

3942
if (searchParams.has('guidePath') || searchParams.has('tutorialId')) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ jest.mock('uiSrc/utils/routing', () => ({
66

77
Object.defineProperty(window, 'location', {
88
value: {
9-
origin: 'http://localhost',
10-
pathname: 'instanceId/workbench'
9+
origin: 'http://localhost'
1110
},
1211
writable: true
1312
})
@@ -24,7 +23,8 @@ const getRedirectionPageTests = [
2423
{ input: ['/analytics'], expected: null },
2524
{ input: ['some-page'], expected: undefined },
2625
{ input: ['/workbench?guidePath=introduction.md', databaseId], expected: '/1/workbench?guidePath=introduction.md&insights=open' },
27-
{ input: ['/_?tutorialId=tutorial'], expected: 'instanceId/workbench?tutorialId=tutorial' },
26+
{ input: ['/_?tutorialId=tutorial'], expected: undefined },
27+
{ input: ['/_?tutorialId=tutorial', databaseId, `/${databaseId}/workbench`], expected: '/1/workbench?tutorialId=tutorial' },
2828
]
2929

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

0 commit comments

Comments
 (0)