DashboardScene: Fixes minor issue transitioning between dashboards (#86262)

* DashboardScene: Fixes minor issue transitioning between dashboards

* Update
This commit is contained in:
Torkel Ödegaard 2024-04-18 05:45:38 +02:00 committed by GitHub
parent 420067a7e1
commit a8b81e1481
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 33 additions and 9 deletions

View File

@ -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 (
<>
<dashboard.Component model={dashboard} key={dashboard.state.key} />

View File

@ -1,6 +1,6 @@
import { advanceBy } from 'jest-date-mock';
import { locationService } from '@grafana/runtime';
import { BackendSrv, locationService, setBackendSrv } from '@grafana/runtime';
import { getUrlSyncManager } from '@grafana/scenes';
import store from 'app/core/store';
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');
});
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 () => {
const loader = new DashboardScenePageStateManager({});
const localStorageDashboard = { uid: 'fake-dash' };

View File

@ -90,11 +90,8 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
case DashboardRoutes.Home:
rsp = await getBackendSrv().get('/api/dashboards/home');
// If user specified a custom home dashboard redirect to that
if (rsp?.redirectUri) {
const newUrl = locationUtil.stripBaseFromUrl(rsp.redirectUri);
locationService.replace(newUrl);
return null;
if (rsp.redirectUri) {
return rsp;
}
if (rsp?.meta) {
@ -171,6 +168,10 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
public async loadDashboard(options: LoadDashboardOptions) {
try {
const dashboard = await this.loadScene(options);
if (!dashboard) {
return;
}
if (!(config.publicDashboardAccessToken && dashboard.state.controls?.state.hideTimeControls)) {
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(
localStorageStore.getObject<DashboardDTO>(DASHBOARD_FROM_LS_KEY) &&
options.keepDashboardFromExploreInLocalStorage === false
);
this.setState({ isLoading: true });
const rsp = await this.fetchDashboard(options);
const fromCache = this.cache[options.uid];
@ -198,8 +201,6 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
}
}
this.setState({ isLoading: true });
if (rsp?.dashboard) {
const scene = transformSaveModelToScene(rsp);
@ -211,6 +212,12 @@ export class DashboardScenePageStateManager extends StateManagerBase<DashboardSc
return scene;
}
if (rsp?.redirectUri) {
const newUrl = locationUtil.stripBaseFromUrl(rsp.redirectUri);
locationService.replace(newUrl);
return null;
}
throw new Error('Dashboard not found');
}