mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
* SceneDashboard: Discard changes now works * To save model works and start at save drawer * Update * Added missing file * Refactorings to keep responsibility more logical * Refactorings * Removed file * Fixed state issue * Update * Update
59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import { StateManagerBase } from 'app/core/services/StateManagerBase';
|
|
import { dashboardLoaderSrv } from 'app/features/dashboard/services/DashboardLoaderSrv';
|
|
|
|
import { DashboardScene } from '../scene/DashboardScene';
|
|
import { transformSaveModelToScene } from '../serialization/transformSaveModelToScene';
|
|
|
|
export interface DashboardScenePageState {
|
|
dashboard?: DashboardScene;
|
|
isLoading?: boolean;
|
|
loadError?: string;
|
|
}
|
|
|
|
export class DashboardScenePageStateManager extends StateManagerBase<DashboardScenePageState> {
|
|
private cache: Record<string, DashboardScene> = {};
|
|
|
|
async loadAndInit(uid: string) {
|
|
try {
|
|
const scene = await this.loadScene(uid);
|
|
scene.startUrlSync();
|
|
|
|
this.cache[uid] = scene;
|
|
this.setState({ dashboard: scene, isLoading: false });
|
|
} catch (err) {
|
|
this.setState({ isLoading: false, loadError: String(err) });
|
|
}
|
|
}
|
|
|
|
private async loadScene(uid: string): Promise<DashboardScene> {
|
|
const fromCache = this.cache[uid];
|
|
if (fromCache) {
|
|
return fromCache;
|
|
}
|
|
|
|
this.setState({ isLoading: true });
|
|
|
|
const rsp = await dashboardLoaderSrv.loadDashboard('db', '', uid);
|
|
|
|
if (rsp.dashboard) {
|
|
return transformSaveModelToScene(rsp);
|
|
}
|
|
|
|
throw new Error('Dashboard not found');
|
|
}
|
|
|
|
public clearState() {
|
|
this.setState({ dashboard: undefined, loadError: undefined, isLoading: false });
|
|
}
|
|
}
|
|
|
|
let stateManager: DashboardScenePageStateManager | null = null;
|
|
|
|
export function getDashboardScenePageStateManager(): DashboardScenePageStateManager {
|
|
if (!stateManager) {
|
|
stateManager = new DashboardScenePageStateManager({});
|
|
}
|
|
|
|
return stateManager;
|
|
}
|