DashboardScene: Add support for showing notices in panel header (#79146)

This commit is contained in:
Dominik Prokop 2023-12-07 10:24:22 +01:00 committed by GitHub
parent 393711c3a1
commit dfc139b2ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -0,0 +1,50 @@
import React from 'react';
import { SceneComponentProps, SceneObjectBase, VizPanel, sceneGraph } from '@grafana/scenes';
import { PanelHeaderNotices } from 'app/features/dashboard/dashgrid/PanelHeader/PanelHeaderNotices';
import { getPanelIdForVizPanel } from '../utils/utils';
export class PanelNotices extends SceneObjectBase {
static Component = PanelNoticesRenderer;
constructor() {
super({});
this.addActivationHandler(this.onActivate);
}
private onActivate = () => {
const panel = this.parent;
if (!panel || !(panel instanceof VizPanel)) {
throw new Error('PanelNotices can be used only as title items for VizPanel');
}
};
public getPanel() {
const panel = this.parent;
if (panel && panel instanceof VizPanel) {
return panel;
}
return null;
}
}
function PanelNoticesRenderer({ model }: SceneComponentProps<PanelNotices>) {
const panel = model.getPanel();
const dataObject = sceneGraph.getData(model);
const data = dataObject.useState();
if (!panel) {
return null;
}
const panelId = getPanelIdForVizPanel(panel);
if (data.data?.series) {
return <PanelHeaderNotices frames={data.data?.series} panelId={panelId} />;
}
return null;
}

View File

@ -40,6 +40,7 @@ import { DashboardScene } from '../scene/DashboardScene';
import { LibraryVizPanel } from '../scene/LibraryVizPanel';
import { VizPanelLinks, VizPanelLinksMenu } from '../scene/PanelLinks';
import { getPanelLinksBehavior, panelMenuBehavior } from '../scene/PanelMenuBehavior';
import { PanelNotices } from '../scene/PanelNotices';
import { PanelRepeaterGridItem } from '../scene/PanelRepeaterGridItem';
import { PanelTimeRange } from '../scene/PanelTimeRange';
import { RowRepeaterBehavior } from '../scene/RowRepeaterBehavior';
@ -391,14 +392,18 @@ export function buildGridItemForLibPanel(panel: PanelModel) {
export function buildGridItemForPanel(panel: PanelModel): SceneGridItemLike {
const hasPanelLinks = panel.links && panel.links.length > 0;
const titleItems: SceneObject[] = [];
let panelLinks;
if (hasPanelLinks) {
panelLinks = new VizPanelLinks({
menu: new VizPanelLinksMenu({ $behaviors: [getPanelLinksBehavior(panel)] }),
});
titleItems.push(panelLinks);
}
titleItems.push(new PanelNotices());
const vizPanelState: VizPanelState = {
key: getVizPanelKeyForPanelId(panel.id),
title: panel.title,
@ -414,7 +419,7 @@ export function buildGridItemForPanel(panel: PanelModel): SceneGridItemLike {
menu: new VizPanelMenu({
$behaviors: [panelMenuBehavior],
}),
titleItems: panelLinks,
titleItems,
extendPanelContext: setDashboardPanelContext,
_UNSAFE_customMigrationHandler: getAngularPanelMigrationHandler(panel),