statusToggleButton.tsx 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import type {BaseButtonProps} from 'sentry/components/button';
  2. import {Button} from 'sentry/components/button';
  3. import {IconPause, IconPlay} from 'sentry/icons';
  4. import {t} from 'sentry/locale';
  5. import HookStore from 'sentry/stores/hookStore';
  6. import type {ObjectStatus} from 'sentry/types/core';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import type {Monitor} from 'sentry/views/monitors/types';
  9. export interface StatusToggleButtonProps extends Omit<BaseButtonProps, 'onClick'> {
  10. monitor: Monitor;
  11. onToggleStatus: (status: ObjectStatus) => Promise<void>;
  12. }
  13. export function StatusToggleButton({
  14. monitor,
  15. onToggleStatus,
  16. ...props
  17. }: StatusToggleButtonProps) {
  18. const organization = useOrganization();
  19. const {status} = monitor;
  20. const isDisabled = status === 'disabled';
  21. const monitorCreationCallbacks = HookStore.get('callback:on-monitor-created');
  22. const Icon = isDisabled ? IconPlay : IconPause;
  23. const label = isDisabled
  24. ? t('Enable this monitor')
  25. : t('Disable this monitor and discard incoming check-ins');
  26. return (
  27. <Button
  28. icon={<Icon />}
  29. aria-label={label}
  30. title={label}
  31. onClick={async () => {
  32. await onToggleStatus(isDisabled ? 'active' : 'disabled');
  33. // TODO(epurkhiser): This hook is probably too specialized and could
  34. // maybe do to be a component hook instead
  35. monitorCreationCallbacks.map(cb => cb(organization));
  36. }}
  37. {...props}
  38. />
  39. );
  40. }