mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
HelpWizard: Use scenes to render dashboard inline dashboard (#64936)
Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
This commit is contained in:
@@ -42,7 +42,6 @@ export function HelpWizard({ panel, plugin, onClose }: Props) {
|
|||||||
currentTab,
|
currentTab,
|
||||||
loading,
|
loading,
|
||||||
error,
|
error,
|
||||||
iframeLoading,
|
|
||||||
options,
|
options,
|
||||||
showMessage,
|
showMessage,
|
||||||
snapshotSize,
|
snapshotSize,
|
||||||
@@ -50,18 +49,13 @@ export function HelpWizard({ panel, plugin, onClose }: Props) {
|
|||||||
snapshotText,
|
snapshotText,
|
||||||
randomize,
|
randomize,
|
||||||
panelTitle,
|
panelTitle,
|
||||||
snapshotUpdate,
|
scene,
|
||||||
} = service.useState();
|
} = service.useState();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
service.buildDebugDashboard();
|
service.buildDebugDashboard();
|
||||||
}, [service, plugin, randomize]);
|
}, [service, plugin, randomize]);
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
// Listen for messages from loaded iframe
|
|
||||||
return service.subscribeToIframeLoadingMessage();
|
|
||||||
}, [service]);
|
|
||||||
|
|
||||||
if (!plugin) {
|
if (!plugin) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -211,20 +205,7 @@ export function HelpWizard({ panel, plugin, onClose }: Props) {
|
|||||||
|
|
||||||
<AutoSizer disableWidth>
|
<AutoSizer disableWidth>
|
||||||
{({ height }) => (
|
{({ height }) => (
|
||||||
<>
|
<div style={{ height, overflow: 'auto' }}>{scene && <scene.Component model={scene} />}</div>
|
||||||
<iframe
|
|
||||||
title="Support snapshot preview"
|
|
||||||
src={`${config.appUrl}dashboard/new?orgId=${contextSrv.user.orgId}&kiosk&${snapshotUpdate}`}
|
|
||||||
width="100%"
|
|
||||||
height={height - 100}
|
|
||||||
frameBorder="0"
|
|
||||||
style={{
|
|
||||||
display: iframeLoading ? 'block' : 'none',
|
|
||||||
marginTop: 16,
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
{!iframeLoading && <div> </div>}
|
|
||||||
</>
|
|
||||||
)}
|
)}
|
||||||
</AutoSizer>
|
</AutoSizer>
|
||||||
</>
|
</>
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import saveAs from 'file-saver';
|
|||||||
|
|
||||||
import { dateTimeFormat, formattedValueToString, getValueFormat, SelectableValue } from '@grafana/data';
|
import { dateTimeFormat, formattedValueToString, getValueFormat, SelectableValue } from '@grafana/data';
|
||||||
import { config } from '@grafana/runtime';
|
import { config } from '@grafana/runtime';
|
||||||
|
import { SceneObject } from '@grafana/scenes';
|
||||||
import { StateManagerBase } from 'app/core/services/StateManagerBase';
|
import { StateManagerBase } from 'app/core/services/StateManagerBase';
|
||||||
|
import { createDashboardSceneFromDashboardModel } from 'app/features/scenes/dashboard/DashboardsLoader';
|
||||||
|
|
||||||
import { getTimeSrv } from '../../services/TimeSrv';
|
import { getTimeSrv } from '../../services/TimeSrv';
|
||||||
import { PanelModel } from '../../state';
|
import { DashboardModel, PanelModel } from '../../state';
|
||||||
import { setDashboardToFetchFromLocalStorage } from '../../state/initDashboard';
|
import { setDashboardToFetchFromLocalStorage } from '../../state/initDashboard';
|
||||||
|
|
||||||
import { Randomize } from './randomizer';
|
import { Randomize } from './randomizer';
|
||||||
@@ -19,7 +21,6 @@ interface SupportSnapshotState {
|
|||||||
markdownText: string;
|
markdownText: string;
|
||||||
snapshotSize?: string;
|
snapshotSize?: string;
|
||||||
randomize: Randomize;
|
randomize: Randomize;
|
||||||
iframeLoading?: boolean;
|
|
||||||
loading?: boolean;
|
loading?: boolean;
|
||||||
error?: {
|
error?: {
|
||||||
title: string;
|
title: string;
|
||||||
@@ -31,6 +32,7 @@ interface SupportSnapshotState {
|
|||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
snapshot?: any;
|
snapshot?: any;
|
||||||
snapshotUpdate: number;
|
snapshotUpdate: number;
|
||||||
|
scene?: SceneObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
export enum SnapshotTab {
|
export enum SnapshotTab {
|
||||||
@@ -70,17 +72,25 @@ export class SupportSnapshotService extends StateManagerBase<SupportSnapshotStat
|
|||||||
}
|
}
|
||||||
|
|
||||||
async buildDebugDashboard() {
|
async buildDebugDashboard() {
|
||||||
const { panel, randomize, snapshotUpdate, iframeLoading, currentTab } = this.state;
|
const { panel, randomize, snapshotUpdate } = this.state;
|
||||||
const snapshot = await getDebugDashboard(panel, randomize, getTimeSrv().timeRange());
|
const snapshot = await getDebugDashboard(panel, randomize, getTimeSrv().timeRange());
|
||||||
const snapshotText = JSON.stringify(snapshot, null, 2);
|
const snapshotText = JSON.stringify(snapshot, null, 2);
|
||||||
const markdownText = getGithubMarkdown(panel, snapshotText);
|
const markdownText = getGithubMarkdown(panel, snapshotText);
|
||||||
const snapshotSize = formattedValueToString(getValueFormat('bytes')(snapshotText?.length ?? 0));
|
const snapshotSize = formattedValueToString(getValueFormat('bytes')(snapshotText?.length ?? 0));
|
||||||
|
|
||||||
if (iframeLoading && currentTab === SnapshotTab.Support) {
|
let scene: SceneObject | undefined = undefined;
|
||||||
setDashboardToFetchFromLocalStorage({ meta: {}, dashboard: snapshot });
|
|
||||||
|
if (config.featureToggles.scenes && !panel.isAngularPlugin()) {
|
||||||
|
try {
|
||||||
|
const oldModel = new DashboardModel(snapshot);
|
||||||
|
const dash = createDashboardSceneFromDashboardModel(oldModel);
|
||||||
|
scene = dash.state.body; // skip the wrappers
|
||||||
|
} catch (ex) {
|
||||||
|
console.log('Error creating scene:', ex);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.setState({ snapshot, snapshotText, markdownText, snapshotSize, snapshotUpdate: snapshotUpdate + 1 });
|
this.setState({ snapshot, snapshotText, markdownText, snapshotSize, snapshotUpdate: snapshotUpdate + 1, scene });
|
||||||
}
|
}
|
||||||
|
|
||||||
onCurrentTabChange = (value: SnapshotTab) => {
|
onCurrentTabChange = (value: SnapshotTab) => {
|
||||||
@@ -134,18 +144,4 @@ export class SupportSnapshotService extends StateManagerBase<SupportSnapshotStat
|
|||||||
global.open(config.appUrl + 'dashboard/new', '_blank');
|
global.open(config.appUrl + 'dashboard/new', '_blank');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
subscribeToIframeLoadingMessage() {
|
|
||||||
const handleEvent = (evt: MessageEvent<string>) => {
|
|
||||||
if (evt.data === 'GrafanaAppInit') {
|
|
||||||
setDashboardToFetchFromLocalStorage({ meta: {}, dashboard: this.state.snapshot });
|
|
||||||
this.setState({ iframeLoading: true });
|
|
||||||
}
|
|
||||||
};
|
|
||||||
window.addEventListener('message', handleEvent, false);
|
|
||||||
|
|
||||||
return function cleanup() {
|
|
||||||
window.removeEventListener('message', handleEvent);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ function getTransformsRow(saveModel: any): string {
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return `<tr>
|
return `<tr>
|
||||||
<th>Transforms (${saveModel.transformations.length})</th>
|
<th>Transform</th>
|
||||||
<td>${saveModel.transformations.map((t: DataTransformerConfig) => t.id).join(', ')}</td>
|
<td>${saveModel.transformations.map((t: DataTransformerConfig) => t.id).join(', ')}</td>
|
||||||
</tr>`;
|
</tr>`;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user