monitorHeaderActions.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 {IconDelete, IconEdit, IconPause, IconPlay} from 'sentry/icons';
  7. import {t} from 'sentry/locale';
  8. import useApi from 'sentry/utils/useApi';
  9. import usePageFilters from 'sentry/utils/usePageFilters';
  10. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  11. import {Monitor, MonitorStatus} from '../types';
  12. import CronsFeedbackButton from './cronsFeedbackButton';
  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 handleDelete = async () => {
  22. await deleteMonitor(api, orgId, monitor.slug);
  23. browserHistory.push(normalizeUrl(`/organizations/${orgId}/crons/`));
  24. };
  25. const handleUpdate = async (data: Partial<Monitor>) => {
  26. const resp = await updateMonitor(api, orgId, monitor.slug, data);
  27. onUpdate?.(resp);
  28. };
  29. const toggleStatus = () =>
  30. handleUpdate({
  31. status:
  32. monitor.status === MonitorStatus.DISABLED
  33. ? MonitorStatus.ACTIVE
  34. : MonitorStatus.DISABLED,
  35. });
  36. return (
  37. <ButtonBar gap={1}>
  38. <CronsFeedbackButton />
  39. <Confirm
  40. onConfirm={handleDelete}
  41. message={t('Are you sure you want to permanently delete this cron monitor?')}
  42. >
  43. <Button size="sm" icon={<IconDelete size="xs" />}>
  44. {t('Delete')}
  45. </Button>
  46. </Confirm>
  47. <Button
  48. size="sm"
  49. icon={
  50. monitor.status !== 'disabled' ? <IconPause size="xs" /> : <IconPlay size="xs" />
  51. }
  52. onClick={toggleStatus}
  53. >
  54. {monitor.status !== 'disabled' ? t('Pause') : t('Resume')}
  55. </Button>
  56. <Button
  57. priority="primary"
  58. size="sm"
  59. icon={<IconEdit size="xs" />}
  60. to={{
  61. pathname: `/organizations/${orgId}/crons/${monitor.slug}/edit/`,
  62. // TODO(davidenwang): Right now we have to pass the environment
  63. // through the URL so that when we save the monitor and are
  64. // redirected back to the details page it queries the backend
  65. // for a monitor environment with check-in data
  66. query: {environment: selection.environments},
  67. }}
  68. >
  69. {t('Edit')}
  70. </Button>
  71. </ButtonBar>
  72. );
  73. }
  74. export default MonitorHeaderActions;