import { useLocation } from 'react-router-dom-v5-compat';
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 ;
}
if (!formValues) {
return (
Cannot create alerts from this panel because no query to an alerting capable datasource is found.
);
}
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 (
);
};