mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
DashboardScene: Fixes minor issue transitioning between dashboards (#86262)
* DashboardScene: Fixes minor issue transitioning between dashboards * Update
This commit is contained in:
@@ -72,6 +72,11 @@ export function DashboardScenePage({ match, route, queryParams, history }: Props
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Do not render anything when transitioning from one dashboard to another
|
||||||
|
if (dashboard.state.uid && dashboard.state.uid !== match.params.uid) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<dashboard.Component model={dashboard} key={dashboard.state.key} />
|
<dashboard.Component model={dashboard} key={dashboard.state.key} />
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { advanceBy } from 'jest-date-mock';
|
import { advanceBy } from 'jest-date-mock';
|
||||||
|
|
||||||
import { locationService } from '@grafana/runtime';
|
import { BackendSrv, locationService, setBackendSrv } from '@grafana/runtime';
|
||||||
import { getUrlSyncManager } from '@grafana/scenes';
|
import { getUrlSyncManager } from '@grafana/scenes';
|
||||||
import store from 'app/core/store';
|
import store from 'app/core/store';
|
||||||
import { DASHBOARD_FROM_LS_KEY } from 'app/features/dashboard/state/initDashboard';
|
import { DASHBOARD_FROM_LS_KEY } from 'app/features/dashboard/state/initDashboard';
|
||||||
@@ -41,6 +41,18 @@ describe('DashboardScenePageStateManager', () => {
|
|||||||
expect(loader.state.loadError).toBe('Error: Dashboard not found');
|
expect(loader.state.loadError).toBe('Error: Dashboard not found');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should handle home dashboard redirect', async () => {
|
||||||
|
setBackendSrv({
|
||||||
|
get: () => Promise.resolve({ redirectUri: '/d/asd' }),
|
||||||
|
} as unknown as BackendSrv);
|
||||||
|
|
||||||
|
const loader = new DashboardScenePageStateManager({});
|
||||||
|
await loader.loadDashboard({ uid: '', route: DashboardRoutes.Home });
|
||||||
|
|
||||||
|
expect(loader.state.dashboard).toBeUndefined();
|
||||||
|
expect(loader.state.loadError).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
it('shoud fetch dashboard from local storage and remove it after if it exists', async () => {
|
it('shoud fetch dashboard from local storage and remove it after if it exists', async () => {
|
||||||
const loader = new DashboardScenePageStateManager({});
|
const loader = new DashboardScenePageStateManager({});
|
||||||
const localStorageDashboard = { uid: 'fake-dash' };
|
const localStorageDashboard = { uid: 'fake-dash' };
|
||||||
|
|||||||
@@ -90,11 +90,8 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
|
|||||||
case DashboardRoutes.Home:
|
case DashboardRoutes.Home:
|
||||||
rsp = await getBackendSrv().get('/api/dashboards/home');
|
rsp = await getBackendSrv().get('/api/dashboards/home');
|
||||||
|
|
||||||
// If user specified a custom home dashboard redirect to that
|
if (rsp.redirectUri) {
|
||||||
if (rsp?.redirectUri) {
|
return rsp;
|
||||||
const newUrl = locationUtil.stripBaseFromUrl(rsp.redirectUri);
|
|
||||||
locationService.replace(newUrl);
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rsp?.meta) {
|
if (rsp?.meta) {
|
||||||
@@ -171,6 +168,10 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
|
|||||||
public async loadDashboard(options: LoadDashboardOptions) {
|
public async loadDashboard(options: LoadDashboardOptions) {
|
||||||
try {
|
try {
|
||||||
const dashboard = await this.loadScene(options);
|
const dashboard = await this.loadScene(options);
|
||||||
|
if (!dashboard) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!(config.publicDashboardAccessToken && dashboard.state.controls?.state.hideTimeControls)) {
|
if (!(config.publicDashboardAccessToken && dashboard.state.controls?.state.hideTimeControls)) {
|
||||||
dashboard.startUrlSync();
|
dashboard.startUrlSync();
|
||||||
}
|
}
|
||||||
@@ -181,12 +182,14 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async loadScene(options: LoadDashboardOptions): Promise<DashboardScene> {
|
private async loadScene(options: LoadDashboardOptions): Promise<DashboardScene | null> {
|
||||||
const comingFromExplore = Boolean(
|
const comingFromExplore = Boolean(
|
||||||
localStorageStore.getObject<DashboardDTO>(DASHBOARD_FROM_LS_KEY) &&
|
localStorageStore.getObject<DashboardDTO>(DASHBOARD_FROM_LS_KEY) &&
|
||||||
options.keepDashboardFromExploreInLocalStorage === false
|
options.keepDashboardFromExploreInLocalStorage === false
|
||||||
);
|
);
|
||||||
|
|
||||||
|
this.setState({ isLoading: true });
|
||||||
|
|
||||||
const rsp = await this.fetchDashboard(options);
|
const rsp = await this.fetchDashboard(options);
|
||||||
|
|
||||||
const fromCache = this.cache[options.uid];
|
const fromCache = this.cache[options.uid];
|
||||||
@@ -198,8 +201,6 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setState({ isLoading: true });
|
|
||||||
|
|
||||||
if (rsp?.dashboard) {
|
if (rsp?.dashboard) {
|
||||||
const scene = transformSaveModelToScene(rsp);
|
const scene = transformSaveModelToScene(rsp);
|
||||||
|
|
||||||
@@ -211,6 +212,12 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
|
|||||||
return scene;
|
return scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (rsp?.redirectUri) {
|
||||||
|
const newUrl = locationUtil.stripBaseFromUrl(rsp.redirectUri);
|
||||||
|
locationService.replace(newUrl);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
throw new Error('Dashboard not found');
|
throw new Error('Dashboard not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user