AmRoutesExpandedRead.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { css } from '@emotion/css';
  2. import React, { FC, useState } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { Button, useStyles2 } from '@grafana/ui';
  5. import { AmRouteReceiver, FormAmRoute } from '../../types/amroutes';
  6. import { getNotificationsPermissions } from '../../utils/access-control';
  7. import { emptyRoute } from '../../utils/amroutes';
  8. import { Authorize } from '../Authorize';
  9. import { AmRoutesTable } from './AmRoutesTable';
  10. import { MuteTimingsTable } from './MuteTimingsTable';
  11. import { getGridStyles } from './gridStyles';
  12. export interface AmRoutesExpandedReadProps {
  13. onChange: (routes: FormAmRoute) => void;
  14. receivers: AmRouteReceiver[];
  15. routes: FormAmRoute;
  16. readOnly?: boolean;
  17. alertManagerSourceName: string;
  18. }
  19. export const AmRoutesExpandedRead: FC<AmRoutesExpandedReadProps> = ({
  20. onChange,
  21. receivers,
  22. routes,
  23. readOnly = false,
  24. alertManagerSourceName,
  25. }) => {
  26. const styles = useStyles2(getStyles);
  27. const gridStyles = useStyles2(getGridStyles);
  28. const permissions = getNotificationsPermissions(alertManagerSourceName);
  29. const groupWait = routes.groupWaitValue ? `${routes.groupWaitValue}${routes.groupWaitValueType}` : '-';
  30. const groupInterval = routes.groupIntervalValue
  31. ? `${routes.groupIntervalValue}${routes.groupIntervalValueType}`
  32. : '-';
  33. const repeatInterval = routes.repeatIntervalValue
  34. ? `${routes.repeatIntervalValue}${routes.repeatIntervalValueType}`
  35. : '-';
  36. const [subroutes, setSubroutes] = useState(routes.routes);
  37. const [isAddMode, setIsAddMode] = useState(false);
  38. return (
  39. <div className={gridStyles.container}>
  40. <div className={gridStyles.titleCell}>Group wait</div>
  41. <div className={gridStyles.valueCell}>{groupWait}</div>
  42. <div className={gridStyles.titleCell}>Group interval</div>
  43. <div className={gridStyles.valueCell}>{groupInterval}</div>
  44. <div className={gridStyles.titleCell}>Repeat interval</div>
  45. <div className={gridStyles.valueCell}>{repeatInterval}</div>
  46. <div className={gridStyles.titleCell}>Nested policies</div>
  47. <div className={gridStyles.valueCell}>
  48. {!!subroutes.length ? (
  49. <AmRoutesTable
  50. isAddMode={isAddMode}
  51. onCancelAdd={() => {
  52. setIsAddMode(false);
  53. setSubroutes((subroutes) => {
  54. const newSubroutes = [...subroutes];
  55. newSubroutes.pop();
  56. return newSubroutes;
  57. });
  58. }}
  59. onChange={(newRoutes) => {
  60. onChange({
  61. ...routes,
  62. routes: newRoutes,
  63. });
  64. if (isAddMode) {
  65. setIsAddMode(false);
  66. }
  67. }}
  68. receivers={receivers}
  69. routes={subroutes}
  70. alertManagerSourceName={alertManagerSourceName}
  71. />
  72. ) : (
  73. <p>No nested policies configured.</p>
  74. )}
  75. {!isAddMode && !readOnly && (
  76. <Authorize actions={[permissions.create]}>
  77. <Button
  78. className={styles.addNestedRoutingBtn}
  79. icon="plus"
  80. onClick={() => {
  81. setSubroutes((subroutes) => [...subroutes, emptyRoute]);
  82. setIsAddMode(true);
  83. }}
  84. variant="secondary"
  85. type="button"
  86. >
  87. Add nested policy
  88. </Button>
  89. </Authorize>
  90. )}
  91. </div>
  92. <div className={gridStyles.titleCell}>Mute timings</div>
  93. <div className={gridStyles.valueCell}>
  94. <MuteTimingsTable
  95. alertManagerSourceName={alertManagerSourceName!}
  96. muteTimingNames={routes.muteTimeIntervals}
  97. hideActions
  98. />
  99. </div>
  100. </div>
  101. );
  102. };
  103. const getStyles = (theme: GrafanaTheme2) => {
  104. return {
  105. addNestedRoutingBtn: css`
  106. margin-top: ${theme.spacing(2)};
  107. `,
  108. };
  109. };