monitorHeaderActions.tsx 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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} from '../types';
  13. import {StatusToggleButton} from './statusToggleButton';
  14. type Props = {
  15. monitor: Monitor;
  16. onUpdate: (data: Monitor) => void;
  17. orgId: string;
  18. };
  19. function MonitorHeaderActions({monitor, orgId, onUpdate}: Props) {
  20. const api = useApi();
  21. const {selection} = usePageFilters();
  22. const endpointOptions = {
  23. query: {
  24. project: selection.projects,
  25. environment: selection.environments,
  26. },
  27. };
  28. const handleDelete = async () => {
  29. await deleteMonitor(api, orgId, monitor.slug);
  30. browserHistory.push(
  31. normalizeUrl({
  32. pathname: `/organizations/${orgId}/crons/`,
  33. query: endpointOptions.query,
  34. })
  35. );
  36. };
  37. const handleUpdate = async (data: Partial<Monitor>) => {
  38. const resp = await updateMonitor(api, orgId, monitor.slug, data);
  39. onUpdate?.(resp);
  40. };
  41. const toggleMute = () => handleUpdate({isMuted: !monitor.isMuted});
  42. const toggleStatus = () =>
  43. handleUpdate({status: monitor.status === 'active' ? 'disabled' : 'active'});
  44. return (
  45. <ButtonBar gap={1}>
  46. <FeedbackWidgetButton />
  47. <Button
  48. size="sm"
  49. icon={monitor.isMuted ? <IconSubscribed /> : <IconUnsubscribed />}
  50. onClick={toggleMute}
  51. >
  52. {monitor.isMuted ? t('Unmute') : t('Mute')}
  53. </Button>
  54. <StatusToggleButton size="sm" onClick={toggleStatus} monitor={monitor} />
  55. <Confirm
  56. onConfirm={handleDelete}
  57. message={t('Are you sure you want to permanently delete this cron monitor?')}
  58. >
  59. <Button size="sm" icon={<IconDelete size="xs" />} aria-label={t('Delete')} />
  60. </Confirm>
  61. <Button
  62. priority="primary"
  63. size="sm"
  64. icon={<IconEdit />}
  65. to={{
  66. pathname: `/organizations/${orgId}/crons/${monitor.slug}/edit/`,
  67. // TODO(davidenwang): Right now we have to pass the environment
  68. // through the URL so that when we save the monitor and are
  69. // redirected back to the details page it queries the backend
  70. // for a monitor environment with check-in data
  71. query: {
  72. environment: selection.environments,
  73. project: selection.projects,
  74. },
  75. }}
  76. >
  77. {t('Edit')}
  78. </Button>
  79. </ButtonBar>
  80. );
  81. }
  82. export default MonitorHeaderActions;