Files
grafana/public/app/features/dashboard-scene/panel-edit/PanelDataPane/NewAlertRuleButton.tsx
Oscar Kilhed 1f2f85004d Scenes: Add alerts from panel edit (#81588)
* Display alerts in scenes dashboard

* sort of working adding alerts

* move alert button to own component

* First fixes

* Generate link/url on click

* some cleanup

* making sure all links from scene go back to scene dashboard/panel; add rule button when there are rules; styling

* remove unused import

* add &scenes to url for alert instance annotations

* Add tests from old alert tab

* Revert addition of &scenes to dashboard urls

* Refactor to simplify NewAlertRuleButton interface

* update test

* Use the raw range to calculate the relative range

---------

Co-authored-by: Torkel Ödegaard <torkel@grafana.com>
Co-authored-by: Dominik Prokop <dominik.prokop@grafana.com>
2024-02-07 12:59:18 +01:00

52 lines
1.6 KiB
TypeScript

import React from 'react';
import { useLocation } from 'react-router-dom';
import { useAsync } from 'react-use';
import { urlUtil } from '@grafana/data';
import { locationService, logInfo } from '@grafana/runtime';
import { VizPanel } from '@grafana/scenes';
import { Alert, Button } from '@grafana/ui';
import { LogMessages } from 'app/features/alerting/unified/Analytics';
import { scenesPanelToRuleFormValues } from 'app/features/alerting/unified/utils/rule-form';
interface ScenesNewRuleFromPanelButtonProps {
panel: VizPanel;
className?: string;
}
export const ScenesNewRuleFromPanelButton = ({ panel, className }: ScenesNewRuleFromPanelButtonProps) => {
const location = useLocation();
const { loading, value: formValues } = useAsync(() => scenesPanelToRuleFormValues(panel), [panel]);
if (loading) {
return <Button disabled={true}>New alert rule</Button>;
}
if (!formValues) {
return (
<Alert severity="info" title="No alerting capable query found">
Cannot create alerts from this panel because no query to an alerting capable datasource is found.
</Alert>
);
}
const onClick = async () => {
logInfo(LogMessages.alertRuleFromPanel);
const updateToDateFormValues = await scenesPanelToRuleFormValues(panel);
const ruleFormUrl = urlUtil.renderUrl('/alerting/new', {
defaults: JSON.stringify(updateToDateFormValues),
returnTo: location.pathname + location.search,
});
locationService.push(ruleFormUrl);
};
return (
<Button icon="bell" onClick={onClick} className={className} data-testid="create-alert-rule-button">
New alert rule
</Button>
);
};