monitorHeaderActions.tsx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {deleteMonitor, updateMonitor} from 'sentry/actionCreators/monitors';
  2. import {Button, LinkButton} from 'sentry/components/button';
  3. import ButtonBar from 'sentry/components/buttonBar';
  4. import Confirm from 'sentry/components/confirm';
  5. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  6. import {IconDelete, IconEdit, IconSubscribed, IconUnsubscribed} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import {browserHistory} from 'sentry/utils/browserHistory';
  9. import normalizeUrl from 'sentry/utils/url/normalizeUrl';
  10. import useApi from 'sentry/utils/useApi';
  11. import usePageFilters from 'sentry/utils/usePageFilters';
  12. import type {Monitor} from '../types';
  13. import {StatusToggleButton} from './statusToggleButton';
  14. type Props = {
  15. monitor: Monitor;
  16. onUpdate: (data: Monitor) => void;
  17. orgSlug: string;
  18. /**
  19. * TODO(epurkhiser): Remove once crons exists only in alerts
  20. */
  21. linkToAlerts?: boolean;
  22. };
  23. function MonitorHeaderActions({monitor, orgSlug, onUpdate, linkToAlerts}: Props) {
  24. const api = useApi();
  25. const {selection} = usePageFilters();
  26. const endpointOptions = {
  27. query: {
  28. project: selection.projects,
  29. environment: selection.environments,
  30. },
  31. };
  32. const handleDelete = async () => {
  33. await deleteMonitor(api, orgSlug, monitor);
  34. browserHistory.push(
  35. normalizeUrl({
  36. pathname: linkToAlerts
  37. ? `/organizations/${orgSlug}/insights/backend/crons/`
  38. : `/organizations/${orgSlug}/crons/`,
  39. query: endpointOptions.query,
  40. })
  41. );
  42. };
  43. const handleUpdate = async (data: Partial<Monitor>) => {
  44. const resp = await updateMonitor(api, orgSlug, monitor, data);
  45. if (resp !== null) {
  46. onUpdate?.(resp);
  47. }
  48. };
  49. return (
  50. <ButtonBar gap={1}>
  51. <FeedbackWidgetButton />
  52. <Button
  53. size="sm"
  54. icon={monitor.isMuted ? <IconSubscribed /> : <IconUnsubscribed />}
  55. onClick={() => handleUpdate({isMuted: !monitor.isMuted})}
  56. >
  57. {monitor.isMuted ? t('Unmute') : t('Mute')}
  58. </Button>
  59. <StatusToggleButton
  60. size="sm"
  61. monitor={monitor}
  62. onToggleStatus={status => handleUpdate({status})}
  63. />
  64. <Confirm
  65. onConfirm={handleDelete}
  66. message={t('Are you sure you want to permanently delete this cron monitor?')}
  67. >
  68. <Button size="sm" icon={<IconDelete size="xs" />} aria-label={t('Delete')} />
  69. </Confirm>
  70. <LinkButton
  71. size="sm"
  72. icon={<IconEdit />}
  73. to={{
  74. pathname: linkToAlerts
  75. ? `/organizations/${orgSlug}/alerts/crons-rules/${monitor.project.slug}/${monitor.slug}/`
  76. : `/organizations/${orgSlug}/crons/${monitor.project.slug}/${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 Monitor')}
  88. </LinkButton>
  89. </ButtonBar>
  90. );
  91. }
  92. export default MonitorHeaderActions;