import { css } from '@emotion/css'; import React, { FC } from 'react'; import { GrafanaTheme2 } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { Alert, CustomScrollbar, LoadingPlaceholder, useStyles2 } from '@grafana/ui'; import { contextSrv } from 'app/core/services/context_srv'; import { DashboardModel, PanelModel } from 'app/features/dashboard/state'; import { NewRuleFromPanelButton } from './components/panel-alerts-tab/NewRuleFromPanelButton'; import { RulesTable } from './components/rules/RulesTable'; import { usePanelCombinedRules } from './hooks/usePanelCombinedRules'; import { getRulesPermissions } from './utils/access-control'; interface Props { dashboard: DashboardModel; panel: PanelModel; } export const PanelAlertTabContent: FC = ({ dashboard, panel }) => { const styles = useStyles2(getStyles); const { errors, loading, rules } = usePanelCombinedRules({ dashboard, panel, poll: true, }); const permissions = getRulesPermissions('grafana'); const canCreateRules = contextSrv.hasPermission(permissions.create); const alert = errors.length ? ( {errors.map((error, index) => (
Failed to load Grafana rules state: {error.message || 'Unknown error.'}
))}
) : null; if (loading && !rules.length) { return (
{alert}
); } if (rules.length) { return (
{alert} {!!dashboard.meta.canSave && canCreateRules && ( )}
); } return (
{alert} {!!dashboard.uid && ( <>

There are no alert rules linked to this panel.

{!!dashboard.meta.canSave && canCreateRules && } )} {!dashboard.uid && !!dashboard.meta.canSave && ( Dashboard must be saved before alerts can be added. )}
); }; const getStyles = (theme: GrafanaTheme2) => ({ newButton: css` margin-top: ${theme.spacing(3)}; `, innerWrapper: css` padding: ${theme.spacing(2)}; `, noRulesWrapper: css` margin: ${theme.spacing(2)}; background-color: ${theme.colors.background.secondary}; padding: ${theme.spacing(3)}; `, });