Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

4.5 KB
Raw Blame
import { useMemo } from 'react';
import { buildRoutePath } from 'lib/router/buildRoutePath';
import { Color } from '@signozhq/design-tokens';
import { Dropdown, type DropdownItemType } from '@signozhq/ui/dropdown';
import { Skeleton } from 'antd';
import { Typography } from '@signozhq/ui/typography';
import {
	useGetMetricAlerts,
	useGetMetricDashboardsV2,
} from 'api/generated/services/metrics';
import { QueryParams } from 'constants/query';
import ROUTES from 'constants/routes';
import { Bell, Grid2X2 } from '@signozhq/icons';
import { openInNewTab } from 'utils/navigation';
import { pluralize } from 'utils/pluralize';

import { DashboardsAndAlertsPopoverProps } from './types';

function DashboardsAndAlertsPopover({
	metricName,
}: DashboardsAndAlertsPopoverProps): JSX.Element | null {
	const {
		data: alertsData,
		isLoading: isLoadingAlerts,
		isError: isErrorAlerts,
	} = useGetMetricAlerts(
		{
			metricName,
		},
		{
			query: {
				enabled: !!metricName,
			},
		},
	);

	const {
		data: dashboardsData,
		isLoading: isLoadingDashboards,
		isError: isErrorDashboards,
	} = useGetMetricDashboardsV2(
		{
			metricName,
		},
		{
			query: {
				enabled: !!metricName,
			},
		},
	);

	const alerts = useMemo(() => {
		return alertsData?.data.alerts ?? [];
	}, [alertsData]);

	const dashboards = useMemo(() => {
		const currentDashboards = dashboardsData?.data.dashboards ?? [];
		// The API returns one entry per referencing panel, so a dashboard repeats
		// once per panel that uses the metric.
		return currentDashboards.filter(
			(dashboard, index, self) =>
				index === self.findIndex((t) => t.dashboardId === dashboard.dashboardId),
		);
	}, [dashboardsData]);

	const alertsPopoverContent = useMemo((): DropdownItemType[] | null => {
		if (alerts && alerts.length > 0) {
			return alerts.map((alert) => ({
				type: 'item',
				value: alert.alertId,
				label: (
					<Typography.Link
						key={alert.alertId}
						onClick={(): void => {
							openInNewTab(
								`${ROUTES.ALERT_OVERVIEW}?${QueryParams.ruleId}=${alert.alertId}`,
							);
						}}
						className="dashboards-popover-content-item"
					>
						{alert.alertName || alert.alertId}
					</Typography.Link>
				),
			}));
		}
		return null;
	}, [alerts]);

	const dashboardsPopoverContent = useMemo((): DropdownItemType[] | null => {
		if (dashboards && dashboards.length > 0) {
			return dashboards.map((dashboard) => ({
				type: 'item',
				value: dashboard.dashboardId,
				label: (
					<Typography.Link
						key={dashboard.dashboardId}
						onClick={(): void => {
							openInNewTab(
								buildRoutePath(ROUTES.DASHBOARD, {
									dashboardId: dashboard.dashboardId,
								}),
							);
						}}
						className="dashboards-popover-content-item"
					>
						{dashboard.dashboardName || dashboard.dashboardId}
					</Typography.Link>
				),
			}));
		}
		return null;
	}, [dashboards]);

	if (isLoadingAlerts || isLoadingDashboards) {
		return (
			<div className="dashboards-and-alerts-popover-container">
				<Skeleton title={false} paragraph={{ rows: 1 }} active />
			</div>
		);
	}

	// If there are no dashboards or alerts or both have errors, don't show the popover
	const hidePopover =
		(!dashboardsPopoverContent && !alertsPopoverContent) ||
		(isErrorAlerts && isErrorDashboards);
	if (hidePopover) {
		return <div className="dashboards-and-alerts-popover-container" />;
	}

	return (
		<div className="dashboards-and-alerts-popover-container">
			{dashboardsPopoverContent && (
				<Dropdown
					items={dashboardsPopoverContent}
					nativeButton={false}
					align="start"
					side="bottom"
				>
					<div
						className="dashboards-and-alerts-popover dashboards-popover"
						data-testid="metric-dashboards-popover"
						style={{ backgroundColor: `${Color.BG_SIENNA_500}33` }}
					>
						<Grid2X2 size={12} color={Color.BG_SIENNA_500} />
						<Typography.Text>
							{pluralize(dashboards.length, 'dashboard')}
						</Typography.Text>
					</div>
				</Dropdown>
			)}
			{alertsPopoverContent && (
				<Dropdown
					items={alertsPopoverContent}
					nativeButton={false}
					align="start"
					side="bottom"
				>
					<div
						className="dashboards-and-alerts-popover alerts-popover"
						data-testid="metric-alerts-popover"
						style={{ backgroundColor: `${Color.BG_SAKURA_500}33` }}
					>
						<Bell size={12} color={Color.BG_SAKURA_500} />
						<Typography.Text>
							{pluralize(alerts.length, 'alert rule')}
						</Typography.Text>
					</div>
				</Dropdown>
			)}
		</div>
	);
}

export default DashboardsAndAlertsPopover;