DashboardScene: Do not add hide flags to URL (#93641)

* DashboardScene: Do not add hide flags to URL

* Update
pull/93445/head
Torkel Ödegaard 10 months ago committed by GitHub
parent c28b37a67b
commit ae6ea459b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 3
      .betterer.results
  2. 25
      public/app/features/dashboard-scene/scene/DashboardControls.test.tsx
  3. 39
      public/app/features/dashboard-scene/scene/DashboardControls.tsx

@ -2750,9 +2750,6 @@ exports[`better eslint`] = {
"public/app/features/dashboard-scene/saving/shared.tsx:5381": [ "public/app/features/dashboard-scene/saving/shared.tsx:5381": [
[0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "0"] [0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "0"]
], ],
"public/app/features/dashboard-scene/scene/DashboardControls.tsx:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"]
],
"public/app/features/dashboard-scene/scene/NavToolbarActions.tsx:5381": [ "public/app/features/dashboard-scene/scene/NavToolbarActions.tsx:5381": [
[0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "0"], [0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "0"],
[0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "1"], [0, 0, 0, "No untranslated strings. Wrap text with <Trans />", "1"],

@ -66,23 +66,15 @@ describe('DashboardControls', () => {
expect(scene._urlSync.getKeys()).toEqual(['_dash.hideTimePicker', '_dash.hideVariables', '_dash.hideLinks']); expect(scene._urlSync.getKeys()).toEqual(['_dash.hideTimePicker', '_dash.hideVariables', '_dash.hideLinks']);
}); });
it('should return url state', () => { it('should not return url state for hide flags', () => {
const scene = buildTestScene(); const scene = buildTestScene();
expect(scene.getUrlState()).toEqual({ expect(scene.getUrlState()).toEqual({});
'_dash.hideTimePicker': undefined,
'_dash.hideVariables': undefined,
'_dash.hideLinks': undefined,
});
scene.setState({ scene.setState({
hideTimeControls: true, hideTimeControls: true,
hideVariableControls: true, hideVariableControls: true,
hideLinksControls: true, hideLinksControls: true,
}); });
expect(scene.getUrlState()).toEqual({ expect(scene.getUrlState()).toEqual({});
'_dash.hideTimePicker': 'true',
'_dash.hideVariables': 'true',
'_dash.hideLinks': 'true',
});
}); });
it('should update from url', () => { it('should update from url', () => {
@ -114,19 +106,16 @@ describe('DashboardControls', () => {
}); });
it('should not call setState if no changes', () => { it('should not call setState if no changes', () => {
const scene = buildTestScene(); const scene = buildTestScene({ hideTimeControls: true, hideVariableControls: true, hideLinksControls: true });
const setState = jest.spyOn(scene, 'setState'); const setState = jest.spyOn(scene, 'setState');
scene.updateFromUrl({ scene.updateFromUrl({
'_dash.hideTimePicker': 'true', '_dash.hideTimePicker': 'true',
'_dash.hideVariables': 'true', '_dash.hideVariables': 'true',
'_dash.hideLinks': 'true', '_dash.hideLinks': 'true',
}); });
scene.updateFromUrl({
'_dash.hideTimePicker': 'true', expect(setState).toHaveBeenCalledTimes(0);
'_dash.hideVariables': 'true',
'_dash.hideLinks': 'true',
});
expect(setState).toHaveBeenCalledTimes(1);
}); });
}); });
}); });

@ -43,28 +43,31 @@ export class DashboardControls extends SceneObjectBase<DashboardControlsState> {
keys: ['_dash.hideTimePicker', '_dash.hideVariables', '_dash.hideLinks'], keys: ['_dash.hideTimePicker', '_dash.hideVariables', '_dash.hideLinks'],
}); });
/**
* We want the hideXX url keys to only sync one way (url => state) on init
* We don't want these flags to be added to URL.
*/
getUrlState() { getUrlState() {
return { return {};
'_dash.hideTimePicker': this.state.hideTimeControls ? 'true' : undefined,
'_dash.hideVariables': this.state.hideVariableControls ? 'true' : undefined,
'_dash.hideLinks': this.state.hideLinksControls ? 'true' : undefined,
};
} }
updateFromUrl(values: SceneObjectUrlValues) { updateFromUrl(values: SceneObjectUrlValues) {
const update: Partial<DashboardControlsState> = {}; const { hideTimeControls, hideVariableControls, hideLinksControls } = this.state;
const isEnabledViaUrl = (key: string) => values[key] === 'true' || values[key] === '';
update.hideTimeControls =
values['_dash.hideTimePicker'] === 'true' || values['_dash.hideTimePicker'] === '' || this.state.hideTimeControls; // Only allow hiding, never "unhiding" from url
update.hideVariableControls = // Becasue this should really only change on first init it's fine to do multiple setState here
values['_dash.hideVariables'] === 'true' ||
values['_dash.hideVariables'] === '' || if (!hideTimeControls && isEnabledViaUrl('_dash.hideTimePicker')) {
this.state.hideVariableControls; this.setState({ hideTimeControls: true });
update.hideLinksControls = }
values['_dash.hideLinks'] === 'true' || values['_dash.hideLinks'] === '' || this.state.hideLinksControls;
if (!hideVariableControls && isEnabledViaUrl('_dash.hideVariables')) {
if (Object.entries(update).some(([k, v]) => v !== this.state[k as keyof DashboardControlsState])) { this.setState({ hideVariableControls: true });
this.setState(update); }
if (!hideLinksControls && isEnabledViaUrl('_dash.hideLinks')) {
this.setState({ hideLinksControls: true });
} }
} }

Loading…
Cancel
Save