2022-11-17 16:15:51 +01:00
|
|
|
// Libraries
|
2023-01-05 09:55:55 -08:00
|
|
|
import React, { useEffect } from 'react';
|
2022-11-17 16:15:51 +01:00
|
|
|
|
2023-03-16 09:19:50 +01:00
|
|
|
import { PageLayoutType } from '@grafana/data';
|
2022-11-17 16:15:51 +01:00
|
|
|
import { Page } from 'app/core/components/Page/Page';
|
|
|
|
|
import PageLoader from 'app/core/components/PageLoader/PageLoader';
|
|
|
|
|
import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
|
2023-10-18 12:40:34 +02:00
|
|
|
import { DashboardPageRouteParams } from 'app/features/dashboard/containers/types';
|
2023-10-19 09:27:56 +02:00
|
|
|
import { DashboardModel } from 'app/features/dashboard/state';
|
2023-10-18 12:40:34 +02:00
|
|
|
import { DashboardRoutes } from 'app/types';
|
2022-11-17 16:15:51 +01:00
|
|
|
|
2023-08-29 14:17:55 +02:00
|
|
|
import { getDashboardScenePageStateManager } from './DashboardScenePageStateManager';
|
2022-11-17 16:15:51 +01:00
|
|
|
|
2023-10-19 09:27:56 +02:00
|
|
|
export interface Props extends GrafanaRouteComponentProps<DashboardPageRouteParams> {
|
|
|
|
|
preloadedDashboard?: DashboardModel;
|
|
|
|
|
}
|
2022-11-17 16:15:51 +01:00
|
|
|
|
2023-10-19 09:27:56 +02:00
|
|
|
export function DashboardScenePage({ match, route, preloadedDashboard }: Props) {
|
2023-08-29 14:17:55 +02:00
|
|
|
const stateManager = getDashboardScenePageStateManager();
|
2023-09-11 13:51:05 +02:00
|
|
|
const { dashboard, isLoading, loadError } = stateManager.useState();
|
2022-11-17 16:15:51 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-10-19 09:27:56 +02:00
|
|
|
if (!preloadedDashboard) {
|
|
|
|
|
if (route.routeName === DashboardRoutes.Home) {
|
|
|
|
|
stateManager.loadDashboard(route.routeName);
|
|
|
|
|
} else {
|
|
|
|
|
stateManager.loadDashboard(match.params.uid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
(preloadedDashboard && preloadedDashboard.uid === match.params.uid) ||
|
|
|
|
|
(preloadedDashboard && preloadedDashboard.uid === null && route.routeName === DashboardRoutes.Home)
|
|
|
|
|
) {
|
|
|
|
|
stateManager.loadSceneFromDashboardModel(preloadedDashboard, route.routeName === DashboardRoutes.Home);
|
2023-10-18 12:40:34 +02:00
|
|
|
}
|
2023-09-11 13:51:05 +02:00
|
|
|
|
2023-03-16 09:19:50 +01:00
|
|
|
return () => {
|
2023-08-29 14:17:55 +02:00
|
|
|
stateManager.clearState();
|
2023-03-16 09:19:50 +01:00
|
|
|
};
|
2023-10-19 09:27:56 +02:00
|
|
|
}, [stateManager, match.params.uid, route.routeName, preloadedDashboard]);
|
2022-11-17 16:15:51 +01:00
|
|
|
|
|
|
|
|
if (!dashboard) {
|
|
|
|
|
return (
|
2023-03-16 09:19:50 +01:00
|
|
|
<Page layout={PageLayoutType.Canvas}>
|
2022-11-17 16:15:51 +01:00
|
|
|
{isLoading && <PageLoader />}
|
2023-09-11 13:51:05 +02:00
|
|
|
{loadError && <h2>{loadError}</h2>}
|
2022-11-17 16:15:51 +01:00
|
|
|
</Page>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <dashboard.Component model={dashboard} />;
|
2023-08-29 14:17:55 +02:00
|
|
|
}
|
2022-11-17 16:15:51 +01:00
|
|
|
|
|
|
|
|
export default DashboardScenePage;
|