DashboardReload: Do not preserve or restore URL state if dashboard version invalid (#104375)
do not preserve or restore url state if dashboard version invalid
This commit is contained in:
@@ -29,6 +29,17 @@ describe('dashboardSessionState', () => {
|
||||
expect(window.sessionStorage.getItem(PRESERVED_SCENE_STATE_KEY)).toBeNull();
|
||||
});
|
||||
|
||||
it('should do nothing if dashboard version is 0', () => {
|
||||
const scene = buildTestScene();
|
||||
scene.setState({ version: 0 });
|
||||
|
||||
const deactivate = scene.activate();
|
||||
expect(window.sessionStorage.getItem(PRESERVED_SCENE_STATE_KEY)).toBeNull();
|
||||
|
||||
deactivate();
|
||||
expect(window.sessionStorage.getItem(PRESERVED_SCENE_STATE_KEY)).toBeNull();
|
||||
});
|
||||
|
||||
it('should capture dashboard scene state and save it to session storage on deactivation', () => {
|
||||
const scene = buildTestScene();
|
||||
|
||||
@@ -84,6 +95,19 @@ describe('dashboardSessionState', () => {
|
||||
|
||||
expect(locationService.getLocation().search).toBe('?var-customVar=b&from=now-6h&to=now&timezone=browser');
|
||||
});
|
||||
|
||||
it('should not restore state if dashboard version is 0', () => {
|
||||
window.sessionStorage.setItem(
|
||||
PRESERVED_SCENE_STATE_KEY,
|
||||
'?var-customVarNotOnDB=b&from=now-5m&to=now&timezone=browser'
|
||||
);
|
||||
const scene = buildTestScene();
|
||||
scene.setState({ version: 0 });
|
||||
|
||||
restoreDashboardStateFromLocalStorage(scene);
|
||||
|
||||
expect(locationService.getLocation().search).toBe('?var-customVar=b&from=now-6h&to=now&timezone=browser');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -7,6 +7,10 @@ import { DashboardScene } from '../scene/DashboardScene';
|
||||
export const PRESERVED_SCENE_STATE_KEY = `grafana.dashboard.preservedUrlFiltersState`;
|
||||
|
||||
export function restoreDashboardStateFromLocalStorage(dashboard: DashboardScene) {
|
||||
if (!dashboard.state.version) {
|
||||
return;
|
||||
}
|
||||
|
||||
const preservedUrlState = window.sessionStorage.getItem(PRESERVED_SCENE_STATE_KEY);
|
||||
|
||||
if (preservedUrlState) {
|
||||
@@ -49,7 +53,7 @@ export function preserveDashboardSceneStateInLocalStorage(scene: DashboardScene)
|
||||
|
||||
return () => {
|
||||
// Skipping saving state for default home dashboard
|
||||
if (!scene.state.uid) {
|
||||
if (!scene.state.uid || !scene.state.version) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user