Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
PageSidebarBody,
} from '@patternfly/react-core';
import { useTranslation } from 'react-i18next';
import { createPath, useLocation } from 'react-router';
import { createPath, useLocation, useNavigate } from 'react-router';
import type { Perspective, ReduxReducer, ContextProvider } from '@console/dynamic-plugin-sdk';
import {
PerspectiveContext,
Expand Down Expand Up @@ -168,12 +168,21 @@ export const DetectContext: FC<{ children: ReactNode }> = ({ children }) => {
const perspectiveExtensions = usePerspectives();
const perspectiveParam = getPerspectiveURLParam(perspectiveExtensions);
const location = useLocation();
const navigate = useNavigate();

useEffect(() => {
if (perspectiveParam && perspectiveParam !== activePerspective) {
setActivePerspective(perspectiveParam, createPath(location));
if (perspectiveParam) {
const params = new URLSearchParams(location.search);
params.delete('perspective');
const search = params.toString();
const cleanPath = createPath({ ...location, search: search ? `?${search}` : '' });
if (perspectiveParam !== activePerspective) {
setActivePerspective(perspectiveParam, cleanPath);
} else {
navigate(cleanPath, { replace: true });
}
}
}, [perspectiveParam, activePerspective, setActivePerspective, location]);
}, [perspectiveParam, activePerspective, setActivePerspective, navigate, location]);

useEffect(() => {
if (reducersResolved) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ jest.mock('@console/internal/redux', () => ({

jest.mock('react-router', () => ({
useLocation: jest.fn(),
createPath: jest.fn((loc) => loc.pathname),
useNavigate: jest.fn(() => jest.fn()),
createPath: jest.fn((loc) => `${loc.pathname}${loc.search || ''}${loc.hash || ''}`),
}));

const useValuesForPerspectiveContextMock = useValuesForPerspectiveContext as jest.Mock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useCallback, useState } from 'react';
import { useNavigate } from 'react-router';
import { createPath, useNavigate } from 'react-router';
import type { PerspectiveType, UseActivePerspective } from '@console/dynamic-plugin-sdk';
import {
usePerspectiveExtension,
Expand Down Expand Up @@ -36,13 +36,19 @@ export const useValuesForPerspectiveContext = (): [

const setPerspective = useCallback<SetActivePerspective>(
(newPerspective, next) => {
const perspectiveChanged = newPerspective !== perspective;
setLastPerspective(newPerspective);
setActivePerspective(newPerspective);
// Navigate to next or root and let the default page determine where to go to next
navigate(next || '/');
// Only navigate if perspective changed
if (perspectiveChanged) {
const targetPath = next || '/';
if (targetPath !== createPath(window.location)) {
navigate(targetPath);
}
}
fireTelemetryEvent('Perspective Changed', { perspective: newPerspective });
},
[setLastPerspective, setActivePerspective, navigate, fireTelemetryEvent],
[setLastPerspective, setActivePerspective, navigate, fireTelemetryEvent, perspective],
);

return [isValidPerspective ? perspective : '', setPerspective, loaded];
Expand Down