From 34875344ed27b4a83caf2e0cbeb0def42e1390b3 Mon Sep 17 00:00:00 2001 From: Darren Janeczek <38694490+darrenjaneczek@users.noreply.github.com> Date: Thu, 11 Apr 2024 09:52:33 -0400 Subject: [PATCH] datatrails: fix: clear undefined query params on history step change (#85607) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- public/app/features/trails/DataTrail.test.tsx | 14 ++++++++++++++ public/app/features/trails/DataTrail.tsx | 7 +++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/public/app/features/trails/DataTrail.test.tsx b/public/app/features/trails/DataTrail.test.tsx index 21b24017da6..9b62ddeff41 100644 --- a/public/app/features/trails/DataTrail.test.tsx +++ b/public/app/features/trails/DataTrail.test.tsx @@ -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); + }); + }); }); }); diff --git a/public/app/features/trails/DataTrail.tsx b/public/app/features/trails/DataTrail.tsx index 938d3f179be..4b33114ab02 100644 --- a/public/app/features/trails/DataTrail.tsx +++ b/public/app/features/trails/DataTrail.tsx @@ -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 { // 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) {