Skip to content

Commit 673b44c

Browse files
authored
fix(SettingsView): Fix back button after modifying the max nodes setting (#234)
1 parent 4f95549 commit 673b44c

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/pages/ProfilesExplorerView/components/SceneProfilesExplorer/components/domain/useHeader.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { SceneObject, SceneVariable } from '@grafana/scenes';
33
import { displaySuccess } from '@shared/domain/displayStatus';
44
import { useCallback } from 'react';
55
import { useHistory } from 'react-router-dom';
6-
import { PLUGIN_BASE_URL } from 'src/constants';
6+
import { PLUGIN_BASE_URL, ROUTES } from 'src/constants';
77

88
import { ProfilesDataSourceVariable } from '../../../../domain/variables/ProfilesDataSourceVariable';
99
import { ExplorationType } from '../../SceneProfilesExplorer';
@@ -62,7 +62,8 @@ export function useHeader({ explorationType, controls, body, $variables, onChang
6262
onClickShareLink,
6363
onClickUserSettings: useCallback(() => {
6464
reportInteraction('g_pyroscope_app_user_settings_clicked');
65-
history.push(`${PLUGIN_BASE_URL}/settings`);
65+
66+
history.push(`${PLUGIN_BASE_URL}${ROUTES.SETTINGS}`, { referrer: window.location.href });
6667
}, [history]),
6768
},
6869
};

src/pages/SettingsView/domain/__tests__/useSettingsView.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ jest.mock('@shared/domain/url-params/useMaxNodesFromUrl', () => ({
3232
useMaxNodesFromUrl: () => [, setMaxNodes],
3333
}));
3434

35+
// useHistory dependency
36+
jest.mock('react-router-dom', () => ({
37+
useHistory: () => ({
38+
push: jest.fn(),
39+
location: {
40+
state: {
41+
referrer: 'http://unit.test/pass',
42+
},
43+
},
44+
}),
45+
}));
46+
3547
// tests
3648
describe('useSettingsView(plugin)', () => {
3749
it('returns an object with "data" and "actions" fields', () => {

src/pages/SettingsView/domain/useSettingsView.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ import { displayError, displaySuccess } from '@shared/domain/displayStatus';
22
import { useMaxNodesFromUrl } from '@shared/domain/url-params/useMaxNodesFromUrl';
33
import { DEFAULT_SETTINGS, PluginSettings } from '@shared/infrastructure/settings/PluginSettings';
44
import { useFetchPluginSettings } from '@shared/infrastructure/settings/useFetchPluginSettings';
5-
import { useEffect, useState } from 'react';
5+
import { useEffect, useRef, useState } from 'react';
6+
import { useHistory } from 'react-router-dom';
7+
8+
import { PLUGIN_BASE_URL, ROUTES } from '../../../constants';
69

710
export function useSettingsView() {
811
const { settings, error: fetchError, mutate } = useFetchPluginSettings();
9-
const [, setMaxNodes] = useMaxNodesFromUrl();
10-
12+
const [maxNodesFromUrl, setMaxNodes] = useMaxNodesFromUrl();
1113
const [currentSettings, setCurrentSettings] = useState<PluginSettings>(settings ?? DEFAULT_SETTINGS);
1214

15+
const history = useHistory();
16+
const referrerRef = useRef(history.location.state?.referrer);
17+
1318
useEffect(() => {
1419
if (settings) {
1520
setCurrentSettings(settings);
@@ -61,7 +66,19 @@ export function useSettingsView() {
6166
}
6267
},
6368
goBack() {
64-
history.back();
69+
if (!referrerRef.current) {
70+
history.push(`${PLUGIN_BASE_URL}${ROUTES.PROFILES_EXPLORER_VIEW}`);
71+
return;
72+
}
73+
74+
const backUrl = new URL(referrerRef.current);
75+
76+
// when calling saveSettings() above, the new maxNodes value is set and the URL search parameter is updated (see useMaxNodesFromUrl.ts)
77+
if (maxNodesFromUrl) {
78+
backUrl.searchParams.set('maxNodes', String(maxNodesFromUrl));
79+
}
80+
81+
history.push(`${backUrl.pathname}${backUrl.search}`);
6582
},
6683
},
6784
};

0 commit comments

Comments
 (0)