monitorHeaderActions.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 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 === MonitorStatus.DISABLED
  44. ? MonitorStatus.ACTIVE
  45. : MonitorStatus.DISABLED,
  46. });
  47. return (
  48. <ButtonBar gap={1}>
  49. <CronsFeedbackButton />
  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 !== 'disabled' ? <IconPause size="xs" /> : <IconPlay size="xs" />
  62. }
  63. onClick={toggleStatus}
  64. >
  65. {monitor.status !== 'disabled' ? t('Pause') : t('Resume')}
  66. </Button>
  67. <Button
  68. priority="primary"
  69. size="sm"
  70. icon={<IconEdit size="xs" />}
  71. to={{
  72. pathname: `/organizations/${orgId}/crons/${monitor.slug}/edit/`,
  73. // TODO(davidenwang): Right now we have to pass the environment
  74. // through the URL so that when we save the monitor and are
  75. // redirected back to the details page it queries the backend
  76. // for a monitor environment with check-in data
  77. query: {
  78. environment: selection.environments,
  79. project: selection.projects,
  80. },
  81. }}
  82. >
  83. {t('Edit')}
  84. </Button>
  85. </ButtonBar>
  86. );
  87. }
  88. export default MonitorHeaderActions;