NotificationsListPage.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import React, { useState, FC, useEffect } from 'react';
  2. import { useAsyncFn } from 'react-use';
  3. import { getBackendSrv } from '@grafana/runtime';
  4. import { HorizontalGroup, Button, LinkButton } from '@grafana/ui';
  5. import EmptyListCTA from 'app/core/components/EmptyListCTA/EmptyListCTA';
  6. import Page from 'app/core/components/Page/Page';
  7. import { appEvents } from 'app/core/core';
  8. import { useNavModel } from 'app/core/hooks/useNavModel';
  9. import { AlertNotification } from 'app/types/alerting';
  10. import { ShowConfirmModalEvent } from '../../types/events';
  11. const NotificationsListPage: FC = () => {
  12. const navModel = useNavModel('channels');
  13. const [notifications, setNotifications] = useState<AlertNotification[]>([]);
  14. const getNotifications = async () => {
  15. return await getBackendSrv().get(`/api/alert-notifications`);
  16. };
  17. const [state, fetchNotifications] = useAsyncFn(getNotifications);
  18. useEffect(() => {
  19. fetchNotifications().then((res) => {
  20. setNotifications(res);
  21. });
  22. }, [fetchNotifications]);
  23. const deleteNotification = (id: number) => {
  24. appEvents.publish(
  25. new ShowConfirmModalEvent({
  26. title: 'Delete',
  27. text: 'Do you want to delete this notification channel?',
  28. text2: `Deleting this notification channel will not delete from alerts any references to it`,
  29. icon: 'trash-alt',
  30. confirmText: 'Delete',
  31. yesText: 'Delete',
  32. onConfirm: async () => {
  33. deleteNotificationConfirmed(id);
  34. },
  35. })
  36. );
  37. };
  38. const deleteNotificationConfirmed = async (id: number) => {
  39. await getBackendSrv().delete(`/api/alert-notifications/${id}`);
  40. const notifications = await fetchNotifications();
  41. setNotifications(notifications);
  42. };
  43. return (
  44. <Page navModel={navModel}>
  45. <Page.Contents>
  46. {state.error && <p>{state.error}</p>}
  47. {!!notifications.length && (
  48. <>
  49. <div className="page-action-bar">
  50. <div className="page-action-bar__spacer" />
  51. <LinkButton icon="channel-add" href="alerting/notification/new">
  52. New channel
  53. </LinkButton>
  54. </div>
  55. <table className="filter-table filter-table--hover">
  56. <thead>
  57. <tr>
  58. <th style={{ minWidth: '200px' }}>
  59. <strong>Name</strong>
  60. </th>
  61. <th style={{ minWidth: '100px' }}>Type</th>
  62. <th style={{ width: '1%' }}></th>
  63. </tr>
  64. </thead>
  65. <tbody>
  66. {notifications.map((notification) => (
  67. <tr key={notification.id}>
  68. <td className="link-td">
  69. <a href={`alerting/notification/${notification.id}/edit`}>{notification.name}</a>
  70. </td>
  71. <td className="link-td">
  72. <a href={`alerting/notification/${notification.id}/edit`}>{notification.type}</a>
  73. </td>
  74. <td className="text-right">
  75. <HorizontalGroup justify="flex-end">
  76. {notification.isDefault && (
  77. <Button disabled variant="secondary" size="sm">
  78. default
  79. </Button>
  80. )}
  81. <Button
  82. variant="destructive"
  83. icon="times"
  84. size="sm"
  85. onClick={() => {
  86. deleteNotification(notification.id);
  87. }}
  88. />
  89. </HorizontalGroup>
  90. </td>
  91. </tr>
  92. ))}
  93. </tbody>
  94. </table>
  95. </>
  96. )}
  97. {!(notifications.length || state.loading) && (
  98. <EmptyListCTA
  99. title="There are no notification channels defined yet"
  100. buttonIcon="channel-add"
  101. buttonLink="alerting/notification/new"
  102. buttonTitle="Add channel"
  103. proTip="You can include images in your alert notifications."
  104. proTipLink="http://docs.grafana.org/alerting/notifications/"
  105. proTipLinkTitle="Learn more"
  106. proTipTarget="_blank"
  107. />
  108. )}
  109. </Page.Contents>
  110. </Page>
  111. );
  112. };
  113. export default NotificationsListPage;