grafana/public/app/features/alerting/unified/ExistingRuleEditor.tsx
Sonia Aguilar 169c5262a5
Alerting: Add Modify export feature for Grafana-managed alert rules (#75114)
* Initial POC for modified rule expor

* Add rule and group export options to modified export

* Add feature toggle for modifier export

* Rename GrafanaRuleDesigner to ModifyExportRuleForm to identify it easily as a rule form

* Refactor naming and folder for RuleDesigner => ModifyExport

* Don't render more action drop-down button when no more actions are allowed

* Redirect cancel button to alert list view

* Fix modify export page being reloaded correctly without errors

* Fix test

* Protect modify-export route when toggle-feature is not enabled

* Fix css betterer error

* Address pr review coments

---------

Co-authored-by: Konrad Lalik <konrad.lalik@grafana.com>
2023-09-28 16:07:45 +02:00

67 lines
2.0 KiB
TypeScript

import React, { useEffect } from 'react';
import { Alert, LoadingPlaceholder } from '@grafana/ui';
import { useCleanup } from 'app/core/hooks/useCleanup';
import { useDispatch } from 'app/types';
import { RuleIdentifier } from 'app/types/unified-alerting';
import { AlertWarning } from './AlertWarning';
import { AlertRuleForm } from './components/rule-editor/alert-rule-form/AlertRuleForm';
import { useIsRuleEditable } from './hooks/useIsRuleEditable';
import { useUnifiedAlertingSelector } from './hooks/useUnifiedAlertingSelector';
import { fetchEditableRuleAction } from './state/actions';
import { initialAsyncRequestState } from './utils/redux';
import * as ruleId from './utils/rule-id';
interface ExistingRuleEditorProps {
identifier: RuleIdentifier;
id?: string;
}
export function ExistingRuleEditor({ identifier, id }: ExistingRuleEditorProps) {
useCleanup((state) => (state.unifiedAlerting.ruleForm.existingRule = initialAsyncRequestState));
const {
loading: loadingAlertRule,
result,
error,
dispatched,
} = useUnifiedAlertingSelector((state) => state.ruleForm.existingRule);
const dispatch = useDispatch();
const { isEditable, loading: loadingEditable } = useIsRuleEditable(
ruleId.ruleIdentifierToRuleSourceName(identifier),
result?.rule
);
const loading = loadingAlertRule || loadingEditable;
useEffect(() => {
if (!dispatched) {
dispatch(fetchEditableRuleAction(identifier));
}
}, [dispatched, dispatch, identifier]);
if (loading || isEditable === undefined) {
return <LoadingPlaceholder text="Loading rule..." />;
}
if (error) {
return (
<Alert severity="error" title="Failed to load rule">
{error.message}
</Alert>
);
}
if (!result) {
return <AlertWarning title="Rule not found">Sorry! This rule does not exist.</AlertWarning>;
}
if (isEditable === false) {
return <AlertWarning title="Cannot edit rule">Sorry! You do not have permission to edit this rule.</AlertWarning>;
}
return <AlertRuleForm existing={result} />;
}