Skip to content

Commit 2394b5e

Browse files
committed
OCPBUGS-50650: Retain original path when detecting perspective
1 parent fbb40b7 commit 2394b5e

3 files changed

Lines changed: 18 additions & 10 deletions

File tree

frontend/packages/console-app/src/components/detect-perspective/PerspectiveDetector.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import * as React from 'react';
2+
import { useLocation } from 'react-router';
23
import { Perspective, ResolvedExtension } from '@console/dynamic-plugin-sdk';
34
import { usePerspectives } from '@console/shared/src';
45

56
type DetectorProps = {
6-
setActivePerspective: (perspective: string) => void;
7+
setActivePerspective: (perspective: string, next: string) => void;
78
perspectiveExtensions: Perspective[];
89
detectors: (
910
| undefined
@@ -12,14 +13,15 @@ type DetectorProps = {
1213
};
1314

1415
type PerspectiveDetectorProps = {
15-
setActivePerspective: (perspective: string) => void;
16+
setActivePerspective: (perspective: string, next: string) => void;
1617
};
1718

1819
const Detector: React.FC<DetectorProps> = ({
1920
setActivePerspective,
2021
perspectiveExtensions,
2122
detectors,
2223
}) => {
24+
const { pathname } = useLocation() ?? {};
2325
let detectedPerspective: string;
2426
const defaultPerspective =
2527
perspectiveExtensions.find((p) => p.properties.default) || perspectiveExtensions[0];
@@ -38,16 +40,17 @@ const Detector: React.FC<DetectorProps> = ({
3840

3941
React.useEffect(() => {
4042
if (detectedPerspective) {
41-
setActivePerspective(detectedPerspective);
43+
setActivePerspective(detectedPerspective, pathname);
4244
} else if (defaultPerspective && (detectors.length < 1 || detectionComplete)) {
4345
// set default perspective if there are no detectors or none of the detections were successfull
44-
setActivePerspective(defaultPerspective.properties.id);
46+
setActivePerspective(defaultPerspective.properties.id, pathname);
4547
}
4648
}, [
4749
defaultPerspective,
4850
detectedPerspective,
4951
detectionComplete,
5052
detectors.length,
53+
pathname,
5154
setActivePerspective,
5255
]);
5356

frontend/packages/console-app/src/components/detect-perspective/__tests__/PerspectiveDetector.spec.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ jest.mock('@console/shared/src', () => ({
1414
usePerspectives: jest.fn(),
1515
}));
1616

17+
jest.mock('react-router', () => {
18+
return {
19+
...require.requireActual('react-router'),
20+
useLocation: jest.fn(() => ({ pathname: '' })),
21+
};
22+
});
23+
1724
const mockPerspectives = [
1825
{
1926
type: 'console.perspective',
@@ -41,7 +48,7 @@ describe('PerspectiveDetector', () => {
4148

4249
const wrapper = mount(<PerspectiveDetector setActivePerspective={setActivePerspective} />);
4350
expect(wrapper.isEmptyRender()).toBe(true);
44-
expect(setActivePerspective).toHaveBeenCalledWith('admin');
51+
expect(setActivePerspective).toHaveBeenCalledWith('admin', '');
4552
});
4653

4754
it('should set detected perspective if detection is successful', async () => {
@@ -60,7 +67,7 @@ describe('PerspectiveDetector', () => {
6067
promiseResolver(() => [true, false]);
6168
});
6269
expect(wrapper.isEmptyRender()).toBe(true);
63-
expect(setActivePerspective).toHaveBeenCalledWith('dev');
70+
expect(setActivePerspective).toHaveBeenCalledWith('dev', '');
6471
});
6572

6673
it('should set default perspective if detection fails', async () => {
@@ -79,7 +86,7 @@ describe('PerspectiveDetector', () => {
7986
promiseResolver(() => [false, false]);
8087
});
8188
expect(wrapper.isEmptyRender()).toBe(true);
82-
expect(setActivePerspective).toHaveBeenCalledWith('admin');
89+
expect(setActivePerspective).toHaveBeenCalledWith('admin', '');
8390
});
8491

8592
it('should set admin as default perspective if all perspectives are disabled', async () => {
@@ -127,6 +134,6 @@ describe('PerspectiveDetector', () => {
127134
promiseResolver(() => [false, false]);
128135
});
129136
expect(wrapper.isEmptyRender()).toBe(true);
130-
expect(setActivePerspective).toHaveBeenCalledWith('admin');
137+
expect(setActivePerspective).toHaveBeenCalledWith('admin', '');
131138
});
132139
});

frontend/packages/console-app/src/components/detect-perspective/useValuesForPerspectiveContext.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export const useValuesForPerspectiveContext = (): [
3232
// Navigate to next or root and let the default page determine where to go to next
3333
navigate(next || '/');
3434
fireTelemetryEvent('Perspective Changed', { perspective: newPerspective });
35-
// eslint-disable-next-line no-console
36-
console.log('DEBUG: setting perspective', newPerspective, next);
3735
};
3836

3937
return [isValidPerspective ? perspective : undefined, setPerspective, loaded];

0 commit comments

Comments
 (0)