blockButton.tsx 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import type {BaseButtonProps} from 'sentry/components/button';
  2. import {Button} from 'sentry/components/button';
  3. import Confirm from 'sentry/components/confirm';
  4. import {Tooltip} from 'sentry/components/tooltip';
  5. import {IconNot, IconPlay} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. type BlockTarget = 'metric' | 'tag';
  8. export interface BlockButtonProps extends BaseButtonProps {
  9. blockTarget: BlockTarget;
  10. hasAccess: boolean;
  11. isBlocked: boolean;
  12. onConfirm: () => void;
  13. }
  14. const tooltipText: Record<BlockTarget, string> = {
  15. metric: t(
  16. 'Disabling a metric blocks its ingestion and makes it inaccessible in Metrics, Alerts, and Dashboards.'
  17. ),
  18. tag: t(
  19. 'Disabling a tag blocks its ingestion and makes it inaccessible in Metrics, Alerts, and Dashboards.'
  20. ),
  21. };
  22. const blockConfirmText: Record<BlockTarget, string> = {
  23. metric: t(
  24. 'Are you sure you want to disable this metric? It will no longer be ingested, and will not be available for use in Metrics, Alerts, or Dashboards.'
  25. ),
  26. tag: t(
  27. 'Are you sure you want to disable this tag? It will no longer be ingested, and will not be available for use in Metrics, Alerts, or Dashboards.'
  28. ),
  29. };
  30. const unblockConfirmText: Record<BlockTarget, string> = {
  31. metric: t('Are you sure you want to activate this metric?'),
  32. tag: t('Are you sure you want to activate this tag?'),
  33. };
  34. const blockAriaLabel: Record<BlockTarget, string> = {
  35. metric: t('Disable metric'),
  36. tag: t('Disable tag'),
  37. };
  38. const unblockAriaLabel: Record<BlockTarget, string> = {
  39. metric: t('Activate metric'),
  40. tag: t('Activate tag'),
  41. };
  42. export function BlockButton({
  43. isBlocked,
  44. blockTarget,
  45. onConfirm,
  46. hasAccess,
  47. disabled,
  48. ...props
  49. }: BlockButtonProps) {
  50. const button = (
  51. <Confirm
  52. priority="danger"
  53. onConfirm={onConfirm}
  54. message={
  55. isBlocked ? unblockConfirmText[blockTarget] : blockConfirmText[blockTarget]
  56. }
  57. // Confirm clones the child element and adds the disabled prop to the clone
  58. // this will override the disabled prop if passed to the Button itself
  59. disabled={!hasAccess || disabled}
  60. confirmText={isBlocked ? t('Activate') : t('Disable')}
  61. >
  62. <Button
  63. {...props}
  64. aria-label={
  65. isBlocked ? unblockAriaLabel[blockTarget] : blockAriaLabel[blockTarget]
  66. }
  67. icon={isBlocked ? <IconPlay size="xs" /> : <IconNot size="xs" />}
  68. >
  69. {isBlocked ? t('Activate') : t('Disable')}
  70. </Button>
  71. </Confirm>
  72. );
  73. const hasTooltip = !hasAccess || !isBlocked;
  74. return hasTooltip ? (
  75. <Tooltip
  76. title={
  77. hasAccess
  78. ? tooltipText[blockTarget]
  79. : t('You do not have permissions to edit metrics.')
  80. }
  81. >
  82. {button}
  83. </Tooltip>
  84. ) : (
  85. button
  86. );
  87. }