From 999aa416dbabd658ade422508a1f06931656b4c2 Mon Sep 17 00:00:00 2001 From: Gilles De Mey Date: Wed, 13 Sep 2023 12:35:23 +0200 Subject: [PATCH] Alerting: Indicate panels without identifier (#74746) --- .../rule-editor/DashboardAnnotationField.tsx | 2 +- .../rule-editor/DashboardPicker.tsx | 27 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/public/app/features/alerting/unified/components/rule-editor/DashboardAnnotationField.tsx b/public/app/features/alerting/unified/components/rule-editor/DashboardAnnotationField.tsx index 7b935a67f86..1cbd423a3bf 100644 --- a/public/app/features/alerting/unified/components/rule-editor/DashboardAnnotationField.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/DashboardAnnotationField.tsx @@ -27,7 +27,7 @@ const DashboardAnnotationField = ({ const styles = useStyles2(getStyles); const dashboardLink = makeDashboardLink(dashboard?.uid || dashboardUid); - const panelLink = makePanelLink(dashboard?.uid || dashboardUid, panel?.id.toString() || panelId); + const panelLink = makePanelLink(dashboard?.uid || dashboardUid, panel?.id?.toString() || panelId); return (
{dashboard && ( diff --git a/public/app/features/alerting/unified/components/rule-editor/DashboardPicker.tsx b/public/app/features/alerting/unified/components/rule-editor/DashboardPicker.tsx index e8127bec171..b78338b093a 100644 --- a/public/app/features/alerting/unified/components/rule-editor/DashboardPicker.tsx +++ b/public/app/features/alerting/unified/components/rule-editor/DashboardPicker.tsx @@ -1,4 +1,5 @@ import { css, cx } from '@emotion/css'; +import { noop } from 'lodash'; import React, { CSSProperties, useCallback, useMemo, useState } from 'react'; import { useDebounce } from 'react-use'; import AutoSizer from 'react-virtualized-auto-sizer'; @@ -20,7 +21,7 @@ import { import { dashboardApi } from '../../api/dashboardApi'; export interface PanelDTO { - id: number; + id?: number; title?: string; type: string; } @@ -73,11 +74,12 @@ export const DashboardPicker = ({ dashboardUid, panelId, isOpen, onChange, onDis const filteredPanels = dashboardResult?.dashboard?.panels - ?.filter((panel): panel is PanelDTO => typeof panel.id === 'number' && typeof panel.type === 'string') ?.filter((panel) => panel.title?.toLowerCase().includes(panelFilter.toLowerCase())) .sort(panelSort) ?? []; - const currentPanel = dashboardResult?.dashboard?.panels?.find((panel) => panel.id.toString() === selectedPanelId); + const currentPanel: PanelDTO | undefined = dashboardResult?.dashboard?.panels?.find( + (panel: PanelDTO) => isValidPanelIdentifier(panel) && panel.id?.toString() === selectedPanelId + ); const selectedDashboardIndex = useMemo(() => { return filteredDashboards.map((dashboard) => dashboard.uid).indexOf(selectedDashboardUid ?? ''); @@ -128,27 +130,34 @@ export const DashboardPicker = ({ dashboardUid, panelId, isOpen, onChange, onDis const PanelRow = ({ index, style }: { index: number; style: CSSProperties }) => { const panel = filteredPanels[index]; const panelTitle = panel.title || ''; - const isSelected = selectedPanelId === panel.id.toString(); + const isSelected = panel.id && selectedPanelId === panel.id?.toString(); const isAlertingCompatible = panel.type === 'graph' || panel.type === 'timeseries'; + const disabled = !isValidPanelIdentifier(panel); return ( ); }; @@ -169,7 +178,7 @@ export const DashboardPicker = ({ dashboardUid, panelId, isOpen, onChange, onDis Dashboard: {dashboardResult?.dashboard.title} ({dashboardResult?.dashboard.uid}) in folder{' '} {dashboardResult?.meta.folderTitle ?? 'General'}
- {Boolean(currentPanel) && ( + {currentPanel && (
Panel: {currentPanel.title} ({currentPanel.id})
@@ -250,6 +259,10 @@ export const DashboardPicker = ({ dashboardUid, panelId, isOpen, onChange, onDis ); }; +const isValidPanelIdentifier = (panel: PanelDTO): boolean => { + return typeof panel.id === 'number' && typeof panel.type === 'string'; +}; + const getPickerStyles = (theme: GrafanaTheme2) => { const clearButton = clearButtonStyles(theme);