mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
DashboardScene: Do not add hide flags to URL (#93641)
* DashboardScene: Do not add hide flags to URL * Update
This commit is contained in:
@@ -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 =
|
// Only allow hiding, never "unhiding" from url
|
||||||
values['_dash.hideTimePicker'] === 'true' || values['_dash.hideTimePicker'] === '' || this.state.hideTimeControls;
|
// Becasue this should really only change on first init it's fine to do multiple setState here
|
||||||
update.hideVariableControls =
|
|
||||||
values['_dash.hideVariables'] === 'true' ||
|
|
||||||
values['_dash.hideVariables'] === '' ||
|
|
||||||
this.state.hideVariableControls;
|
|
||||||
update.hideLinksControls =
|
|
||||||
values['_dash.hideLinks'] === 'true' || values['_dash.hideLinks'] === '' || this.state.hideLinksControls;
|
|
||||||
|
|
||||||
if (Object.entries(update).some(([k, v]) => v !== this.state[k as keyof DashboardControlsState])) {
|
if (!hideTimeControls && isEnabledViaUrl('_dash.hideTimePicker')) {
|
||||||
this.setState(update);
|
this.setState({ hideTimeControls: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hideVariableControls && isEnabledViaUrl('_dash.hideVariables')) {
|
||||||
|
this.setState({ hideVariableControls: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hideLinksControls && isEnabledViaUrl('_dash.hideLinks')) {
|
||||||
|
this.setState({ hideLinksControls: true });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user