AlertManagerPicker.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { css } from '@emotion/css';
  2. import React, { FC, useMemo } from 'react';
  3. import { GrafanaTheme2, SelectableValue } from '@grafana/data';
  4. import { Field, Select, useStyles2 } from '@grafana/ui';
  5. import { AlertManagerDataSource, GRAFANA_RULES_SOURCE_NAME } from '../utils/datasource';
  6. interface Props {
  7. onChange: (alertManagerSourceName: string) => void;
  8. current?: string;
  9. disabled?: boolean;
  10. dataSources: AlertManagerDataSource[];
  11. }
  12. function getAlertManagerLabel(alertManager: AlertManagerDataSource) {
  13. return alertManager.name === GRAFANA_RULES_SOURCE_NAME ? 'Grafana' : alertManager.name.slice(0, 37);
  14. }
  15. export const AlertManagerPicker: FC<Props> = ({ onChange, current, dataSources, disabled = false }) => {
  16. const styles = useStyles2(getStyles);
  17. const options: Array<SelectableValue<string>> = useMemo(() => {
  18. return dataSources.map((ds) => ({
  19. label: getAlertManagerLabel(ds),
  20. value: ds.name,
  21. imgUrl: ds.imgUrl,
  22. meta: ds.meta,
  23. }));
  24. }, [dataSources]);
  25. return (
  26. <Field
  27. className={styles.field}
  28. label={disabled ? 'Alertmanager' : 'Choose Alertmanager'}
  29. disabled={disabled || options.length === 1}
  30. data-testid="alertmanager-picker"
  31. >
  32. <Select
  33. aria-label={disabled ? 'Alertmanager' : 'Choose Alertmanager'}
  34. width={29}
  35. className="ds-picker select-container"
  36. backspaceRemovesValue={false}
  37. onChange={(value) => value.value && onChange(value.value)}
  38. options={options}
  39. maxMenuHeight={500}
  40. noOptionsMessage="No datasources found"
  41. value={current}
  42. getOptionLabel={(o) => o.label}
  43. />
  44. </Field>
  45. );
  46. };
  47. const getStyles = (theme: GrafanaTheme2) => ({
  48. field: css`
  49. margin-bottom: ${theme.spacing(4)};
  50. `,
  51. });