statusToggleButton.tsx 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import type {BaseButtonProps} from 'sentry/components/button';
  2. import {Button} from 'sentry/components/button';
  3. import HookOrDefault from 'sentry/components/hookOrDefault';
  4. import {IconPause, IconPlay} from 'sentry/icons';
  5. import {t} from 'sentry/locale';
  6. import HookStore from 'sentry/stores/hookStore';
  7. import type {ObjectStatus} from 'sentry/types';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import type {Monitor} from 'sentry/views/monitors/types';
  10. interface StatusToggleButtonProps extends Omit<BaseButtonProps, 'onClick'> {
  11. monitor: Monitor;
  12. onToggleStatus: (status: ObjectStatus) => Promise<void>;
  13. }
  14. function SimpleStatusToggle({
  15. monitor,
  16. onToggleStatus,
  17. ...props
  18. }: StatusToggleButtonProps) {
  19. const organization = useOrganization();
  20. const {status} = monitor;
  21. const isDisabled = status === 'disabled';
  22. const monitorCreationCallbacks = HookStore.get('callback:on-monitor-created');
  23. const Icon = isDisabled ? IconPlay : IconPause;
  24. const label = isDisabled
  25. ? t('Enable this monitor')
  26. : t('Disable this monitor and discard incoming check-ins');
  27. return (
  28. <Button
  29. icon={<Icon />}
  30. aria-label={label}
  31. title={label}
  32. onClick={async () => {
  33. await onToggleStatus(isDisabled ? 'active' : 'disabled');
  34. // TODO(davidenwang): Lets place this in the monitor-status-toggle hook once its created
  35. monitorCreationCallbacks.map(cb => cb(organization));
  36. }}
  37. {...props}
  38. />
  39. );
  40. }
  41. const StatusToggleButton = HookOrDefault({
  42. hookName: 'component:monitor-status-toggle',
  43. defaultComponent: SimpleStatusToggle,
  44. });
  45. export type {StatusToggleButtonProps};
  46. export {StatusToggleButton};