monitorHeaderActions.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {browserHistory} from 'react-router';
  2. import {deleteMonitor, updateMonitor} from 'sentry/actionCreators/monitors';
  3. import {Button} from 'sentry/components/button';
  4. import ButtonBar from 'sentry/components/buttonBar';
  5. import Confirm from 'sentry/components/confirm';
  6. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  7. import {IconDelete, IconEdit, IconSubscribed, IconUnsubscribed} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import useApi from 'sentry/utils/useApi';
  10. import usePageFilters from 'sentry/utils/usePageFilters';
  11. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  12. import {Monitor, MonitorObjectStatus} from '../types';
  13. type Props = {
  14. monitor: Monitor;
  15. onUpdate: (data: Monitor) => void;
  16. orgId: string;
  17. };
  18. function MonitorHeaderActions({monitor, orgId, onUpdate}: Props) {
  19. const api = useApi();
  20. const {selection} = usePageFilters();
  21. const endpointOptions = {
  22. query: {
  23. project: selection.projects,
  24. environment: selection.environments,
  25. },
  26. };
  27. const handleDelete = async () => {
  28. await deleteMonitor(api, orgId, monitor.slug);
  29. browserHistory.push(
  30. normalizeUrl({
  31. pathname: `/organizations/${orgId}/crons/`,
  32. query: endpointOptions.query,
  33. })
  34. );
  35. };
  36. const handleUpdate = async (data: Partial<Monitor>) => {
  37. const resp = await updateMonitor(api, orgId, monitor.slug, data);
  38. onUpdate?.(resp);
  39. };
  40. const toggleStatus = () =>
  41. handleUpdate({
  42. status:
  43. monitor.status === MonitorObjectStatus.MUTED
  44. ? MonitorObjectStatus.ACTIVE
  45. : MonitorObjectStatus.MUTED,
  46. });
  47. return (
  48. <ButtonBar gap={1}>
  49. <FeedbackWidgetButton />
  50. <Confirm
  51. onConfirm={handleDelete}
  52. message={t('Are you sure you want to permanently delete this cron monitor?')}
  53. >
  54. <Button size="sm" icon={<IconDelete size="xs" />}>
  55. {t('Delete')}
  56. </Button>
  57. </Confirm>
  58. <Button
  59. size="sm"
  60. icon={
  61. monitor.status !== MonitorObjectStatus.MUTED ? (
  62. <IconUnsubscribed size="xs" />
  63. ) : (
  64. <IconSubscribed size="xs" />
  65. )
  66. }
  67. onClick={toggleStatus}
  68. >
  69. {monitor.status !== MonitorObjectStatus.MUTED ? t('Mute') : t('Unmute')}
  70. </Button>
  71. <Button
  72. priority="primary"
  73. size="sm"
  74. icon={<IconEdit size="xs" />}
  75. to={{
  76. pathname: `/organizations/${orgId}/crons/${monitor.slug}/edit/`,
  77. // TODO(davidenwang): Right now we have to pass the environment
  78. // through the URL so that when we save the monitor and are
  79. // redirected back to the details page it queries the backend
  80. // for a monitor environment with check-in data
  81. query: {
  82. environment: selection.environments,
  83. project: selection.projects,
  84. },
  85. }}
  86. >
  87. {t('Edit')}
  88. </Button>
  89. </ButtonBar>
  90. );
  91. }
  92. export default MonitorHeaderActions;