AmRootRoute.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { css } from '@emotion/css';
  2. import React, { FC } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { Button, useStyles2 } from '@grafana/ui';
  5. import { Authorize } from '../../components/Authorize';
  6. import { AmRouteReceiver, FormAmRoute } from '../../types/amroutes';
  7. import { getNotificationsPermissions } from '../../utils/access-control';
  8. import { isVanillaPrometheusAlertManagerDataSource } from '../../utils/datasource';
  9. import { AmRootRouteForm } from './AmRootRouteForm';
  10. import { AmRootRouteRead } from './AmRootRouteRead';
  11. export interface AmRootRouteProps {
  12. isEditMode: boolean;
  13. onEnterEditMode: () => void;
  14. onExitEditMode: () => void;
  15. onSave: (data: Partial<FormAmRoute>) => void;
  16. receivers: AmRouteReceiver[];
  17. routes: FormAmRoute;
  18. alertManagerSourceName: string;
  19. }
  20. export const AmRootRoute: FC<AmRootRouteProps> = ({
  21. isEditMode,
  22. onSave,
  23. onEnterEditMode,
  24. onExitEditMode,
  25. receivers,
  26. routes,
  27. alertManagerSourceName,
  28. }) => {
  29. const styles = useStyles2(getStyles);
  30. const permissions = getNotificationsPermissions(alertManagerSourceName);
  31. const isReadOnly = isVanillaPrometheusAlertManagerDataSource(alertManagerSourceName);
  32. return (
  33. <div className={styles.container} data-testid="am-root-route-container">
  34. <div className={styles.titleContainer}>
  35. <h5 className={styles.title}>
  36. Root policy - <i>default for all alerts</i>
  37. </h5>
  38. {!isEditMode && !isReadOnly && (
  39. <Authorize actions={[permissions.update]}>
  40. <Button icon="pen" onClick={onEnterEditMode} size="sm" type="button" variant="secondary">
  41. Edit
  42. </Button>
  43. </Authorize>
  44. )}
  45. </div>
  46. <p>
  47. All alerts will go to the default contact point, unless you set additional matchers in the specific routing
  48. area.
  49. </p>
  50. {isEditMode ? (
  51. <AmRootRouteForm
  52. alertManagerSourceName={alertManagerSourceName}
  53. onCancel={onExitEditMode}
  54. onSave={onSave}
  55. receivers={receivers}
  56. routes={routes}
  57. />
  58. ) : (
  59. <AmRootRouteRead routes={routes} />
  60. )}
  61. </div>
  62. );
  63. };
  64. const getStyles = (theme: GrafanaTheme2) => {
  65. return {
  66. container: css`
  67. background-color: ${theme.colors.background.secondary};
  68. color: ${theme.colors.text.secondary};
  69. padding: ${theme.spacing(2)};
  70. `,
  71. titleContainer: css`
  72. color: ${theme.colors.text.primary};
  73. display: flex;
  74. flex-flow: row nowrap;
  75. `,
  76. title: css`
  77. flex: 100%;
  78. `,
  79. };
  80. };