MuteTimings.tsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import React, { useCallback, useEffect } from 'react';
  2. import { useDispatch } from 'react-redux';
  3. import { Route, Redirect, Switch } from 'react-router-dom';
  4. import { Alert, LoadingPlaceholder } from '@grafana/ui';
  5. import { useQueryParams } from 'app/core/hooks/useQueryParams';
  6. import { MuteTimeInterval } from 'app/plugins/datasource/alertmanager/types';
  7. import MuteTimingForm from './components/amroutes/MuteTimingForm';
  8. import { useAlertManagerSourceName } from './hooks/useAlertManagerSourceName';
  9. import { useAlertManagersByPermission } from './hooks/useAlertManagerSources';
  10. import { useUnifiedAlertingSelector } from './hooks/useUnifiedAlertingSelector';
  11. import { fetchAlertManagerConfigAction } from './state/actions';
  12. import { initialAsyncRequestState } from './utils/redux';
  13. const MuteTimings = () => {
  14. const [queryParams] = useQueryParams();
  15. const dispatch = useDispatch();
  16. const alertManagers = useAlertManagersByPermission('notification');
  17. const [alertManagerSourceName] = useAlertManagerSourceName(alertManagers);
  18. const amConfigs = useUnifiedAlertingSelector((state) => state.amConfigs);
  19. const fetchConfig = useCallback(() => {
  20. if (alertManagerSourceName) {
  21. dispatch(fetchAlertManagerConfigAction(alertManagerSourceName));
  22. }
  23. }, [alertManagerSourceName, dispatch]);
  24. useEffect(() => {
  25. fetchConfig();
  26. }, [fetchConfig]);
  27. const { result, error, loading } =
  28. (alertManagerSourceName && amConfigs[alertManagerSourceName]) || initialAsyncRequestState;
  29. const config = result?.alertmanager_config;
  30. const getMuteTimingByName = useCallback(
  31. (id: string): MuteTimeInterval | undefined => {
  32. return config?.mute_time_intervals?.find(({ name }: MuteTimeInterval) => name === id);
  33. },
  34. [config]
  35. );
  36. return (
  37. <>
  38. {loading && <LoadingPlaceholder text="Loading mute timing" />}
  39. {error && !loading && (
  40. <Alert severity="error" title={`Error loading Alertmanager config for ${alertManagerSourceName}`}>
  41. {error.message || 'Unknown error.'}
  42. </Alert>
  43. )}
  44. {result && !error && (
  45. <Switch>
  46. <Route exact path="/alerting/routes/mute-timing/new">
  47. <MuteTimingForm />
  48. </Route>
  49. <Route exact path="/alerting/routes/mute-timing/edit">
  50. {() => {
  51. if (queryParams['muteName']) {
  52. const muteTiming = getMuteTimingByName(String(queryParams['muteName']));
  53. return <MuteTimingForm muteTiming={muteTiming} showError={!muteTiming} />;
  54. }
  55. return <Redirect to="/alerting/routes" />;
  56. }}
  57. </Route>
  58. </Switch>
  59. )}
  60. </>
  61. );
  62. };
  63. export default MuteTimings;