useMuteTimingOptions.ts 1.4 KB

12345678910111213141516171819202122232425262728293031
  1. import { useMemo } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { AlertmanagerConfig } from 'app/plugins/datasource/alertmanager/types';
  4. import { timeIntervalToString } from '../utils/alertmanager';
  5. import { initialAsyncRequestState } from '../utils/redux';
  6. import { useAlertManagerSourceName } from './useAlertManagerSourceName';
  7. import { useAlertManagersByPermission } from './useAlertManagerSources';
  8. import { useUnifiedAlertingSelector } from './useUnifiedAlertingSelector';
  9. export function useMuteTimingOptions(): Array<SelectableValue<string>> {
  10. const alertManagers = useAlertManagersByPermission('notification');
  11. const [alertManagerSourceName] = useAlertManagerSourceName(alertManagers);
  12. const amConfigs = useUnifiedAlertingSelector((state) => state.amConfigs);
  13. return useMemo(() => {
  14. const { result } = (alertManagerSourceName && amConfigs[alertManagerSourceName]) || initialAsyncRequestState;
  15. const config: AlertmanagerConfig = result?.alertmanager_config ?? {};
  16. const muteTimingsOptions: Array<SelectableValue<string>> =
  17. config?.mute_time_intervals?.map((value) => ({
  18. value: value.name,
  19. label: value.name,
  20. description: value.time_intervals.map((interval) => timeIntervalToString(interval)).join(', AND '),
  21. })) ?? [];
  22. return muteTimingsOptions;
  23. }, [alertManagerSourceName, amConfigs]);
  24. }