statusToggleButton.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import {BaseButtonProps, Button} from 'sentry/components/button';
  2. import HookOrDefault from 'sentry/components/hookOrDefault';
  3. import {IconPause, IconPlay} from 'sentry/icons';
  4. import {t} from 'sentry/locale';
  5. import HookStore from 'sentry/stores/hookStore';
  6. import {ObjectStatus} from 'sentry/types';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import {Monitor} from 'sentry/views/monitors/types';
  9. interface StatusToggleButtonProps extends Omit<BaseButtonProps, 'onClick'> {
  10. monitor: Monitor;
  11. onToggleStatus: (status: ObjectStatus) => Promise<void>;
  12. }
  13. function SimpleStatusToggle({
  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(davidenwang): Lets place this in the monitor-status-toggle hook once its created
  34. monitorCreationCallbacks.map(cb => cb(organization));
  35. }}
  36. {...props}
  37. />
  38. );
  39. }
  40. const StatusToggleButton = HookOrDefault({
  41. hookName: 'component:monitor-status-toggle',
  42. defaultComponent: SimpleStatusToggle,
  43. });
  44. export {StatusToggleButton, StatusToggleButtonProps};