datatrails: fix: clear undefined query params on history step change (#85607)

* fix: clear undefined query params on history step change

* Minor tweak

* fix: resolve CodeQL check: Client-side cross-site scripting

---------

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
Darren Janeczek 2024-04-11 09:52:33 -04:00 committed by GitHub
parent b4b0a80e86
commit 34875344ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View File

@ -130,5 +130,19 @@ describe('DataTrail', () => {
});
});
});
describe('When going back to history step 0', () => {
beforeEach(() => {
trail.publishEvent(new MetricSelectedEvent('first_metric'));
trail.publishEvent(new MetricSelectedEvent('second_metric'));
trail.state.history.goBackToStep(0);
});
it('Should remove metric from state and url', () => {
expect(trail.state.metric).toBe(undefined);
expect(locationService.getSearchObject().metric).toBe(undefined);
expect(locationService.getSearch().has('metric')).toBe(false);
});
});
});
});

View File

@ -1,7 +1,7 @@
import { css } from '@emotion/css';
import React from 'react';
import { AdHocVariableFilter, GrafanaTheme2, VariableHide } from '@grafana/data';
import { AdHocVariableFilter, GrafanaTheme2, VariableHide, urlUtil } from '@grafana/data';
import { locationService } from '@grafana/runtime';
import {
AdHocFiltersVariable,
@ -152,8 +152,11 @@ export class DataTrail extends SceneObjectBase<DataTrailState> {
// Embedded trails should not be altering the URL
return;
}
const urlState = getUrlSyncManager().getUrlState(this);
locationService.partial(urlState, true);
const fullUrl = urlUtil.renderUrl(locationService.getLocation().pathname, urlState);
locationService.replace(encodeURI(fullUrl));
}
private _handleMetricSelectedEvent(evt: MetricSelectedEvent) {