Receivers.tsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import React, { FC, useEffect } from 'react';
  2. import { useDispatch } from 'react-redux';
  3. import { Redirect, Route, RouteChildrenProps, Switch, useLocation } from 'react-router-dom';
  4. import { Alert, LoadingPlaceholder, withErrorBoundary } from '@grafana/ui';
  5. import { AlertManagerPicker } from './components/AlertManagerPicker';
  6. import { AlertingPageWrapper } from './components/AlertingPageWrapper';
  7. import { NoAlertManagerWarning } from './components/NoAlertManagerWarning';
  8. import { EditReceiverView } from './components/receivers/EditReceiverView';
  9. import { EditTemplateView } from './components/receivers/EditTemplateView';
  10. import { GlobalConfigForm } from './components/receivers/GlobalConfigForm';
  11. import { NewReceiverView } from './components/receivers/NewReceiverView';
  12. import { NewTemplateView } from './components/receivers/NewTemplateView';
  13. import { ReceiversAndTemplatesView } from './components/receivers/ReceiversAndTemplatesView';
  14. import { useAlertManagerSourceName } from './hooks/useAlertManagerSourceName';
  15. import { useAlertManagersByPermission } from './hooks/useAlertManagerSources';
  16. import { useUnifiedAlertingSelector } from './hooks/useUnifiedAlertingSelector';
  17. import { fetchAlertManagerConfigAction, fetchGrafanaNotifiersAction } from './state/actions';
  18. import { GRAFANA_RULES_SOURCE_NAME } from './utils/datasource';
  19. import { initialAsyncRequestState } from './utils/redux';
  20. const Receivers: FC = () => {
  21. const alertManagers = useAlertManagersByPermission('notification');
  22. const [alertManagerSourceName, setAlertManagerSourceName] = useAlertManagerSourceName(alertManagers);
  23. const dispatch = useDispatch();
  24. const location = useLocation();
  25. const isRoot = location.pathname.endsWith('/alerting/notifications');
  26. const configRequests = useUnifiedAlertingSelector((state) => state.amConfigs);
  27. const {
  28. result: config,
  29. loading,
  30. error,
  31. } = (alertManagerSourceName && configRequests[alertManagerSourceName]) || initialAsyncRequestState;
  32. const receiverTypes = useUnifiedAlertingSelector((state) => state.grafanaNotifiers);
  33. const shouldLoadConfig = isRoot || !config;
  34. useEffect(() => {
  35. if (alertManagerSourceName && shouldLoadConfig) {
  36. dispatch(fetchAlertManagerConfigAction(alertManagerSourceName));
  37. }
  38. }, [alertManagerSourceName, dispatch, shouldLoadConfig]);
  39. useEffect(() => {
  40. if (
  41. alertManagerSourceName === GRAFANA_RULES_SOURCE_NAME &&
  42. !(receiverTypes.result || receiverTypes.loading || receiverTypes.error)
  43. ) {
  44. dispatch(fetchGrafanaNotifiersAction());
  45. }
  46. }, [alertManagerSourceName, dispatch, receiverTypes]);
  47. const disableAmSelect = !isRoot;
  48. if (!alertManagerSourceName) {
  49. return isRoot ? (
  50. <AlertingPageWrapper pageId="receivers">
  51. <NoAlertManagerWarning availableAlertManagers={alertManagers} />
  52. </AlertingPageWrapper>
  53. ) : (
  54. <Redirect to="/alerting/notifications" />
  55. );
  56. }
  57. return (
  58. <AlertingPageWrapper pageId="receivers">
  59. <AlertManagerPicker
  60. current={alertManagerSourceName}
  61. disabled={disableAmSelect}
  62. onChange={setAlertManagerSourceName}
  63. dataSources={alertManagers}
  64. />
  65. {error && !loading && (
  66. <Alert severity="error" title="Error loading Alertmanager config">
  67. {error.message || 'Unknown error.'}
  68. </Alert>
  69. )}
  70. {loading && !config && <LoadingPlaceholder text="loading configuration..." />}
  71. {config && !error && (
  72. <Switch>
  73. <Route exact={true} path="/alerting/notifications">
  74. <ReceiversAndTemplatesView config={config} alertManagerName={alertManagerSourceName} />
  75. </Route>
  76. <Route exact={true} path="/alerting/notifications/templates/new">
  77. <NewTemplateView config={config} alertManagerSourceName={alertManagerSourceName} />
  78. </Route>
  79. <Route exact={true} path="/alerting/notifications/templates/:name/edit">
  80. {({ match }: RouteChildrenProps<{ name: string }>) =>
  81. match?.params.name && (
  82. <EditTemplateView
  83. alertManagerSourceName={alertManagerSourceName}
  84. config={config}
  85. templateName={decodeURIComponent(match?.params.name)}
  86. />
  87. )
  88. }
  89. </Route>
  90. <Route exact={true} path="/alerting/notifications/receivers/new">
  91. <NewReceiverView config={config} alertManagerSourceName={alertManagerSourceName} />
  92. </Route>
  93. <Route exact={true} path="/alerting/notifications/receivers/:name/edit">
  94. {({ match }: RouteChildrenProps<{ name: string }>) =>
  95. match?.params.name && (
  96. <EditReceiverView
  97. alertManagerSourceName={alertManagerSourceName}
  98. config={config}
  99. receiverName={decodeURIComponent(match?.params.name)}
  100. />
  101. )
  102. }
  103. </Route>
  104. <Route exact={true} path="/alerting/notifications/global-config">
  105. <GlobalConfigForm config={config} alertManagerSourceName={alertManagerSourceName} />
  106. </Route>
  107. </Switch>
  108. )}
  109. </AlertingPageWrapper>
  110. );
  111. };
  112. export default withErrorBoundary(Receivers, { style: 'page' });