metricFormulaContextMenu.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import {useMemo} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  4. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  5. import {IconClose, IconCopy, IconEllipsis} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {trackAnalytics} from 'sentry/utils/analytics';
  8. import {hasDDMFeature} from 'sentry/utils/metrics/features';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {useDDMContext} from 'sentry/views/ddm/context';
  11. type ContextMenuProps = {
  12. widgetIndex: number;
  13. };
  14. export function MetricFormulaContextMenu({widgetIndex}: ContextMenuProps) {
  15. const organization = useOrganization();
  16. const {removeWidget, duplicateWidget, widgets} = useDDMContext();
  17. const canDelete = widgets.length > 1;
  18. const items = useMemo<MenuItemProps[]>(
  19. () => [
  20. {
  21. leadingItems: [<IconCopy key="icon" />],
  22. key: 'duplicate',
  23. label: t('Duplicate'),
  24. onAction: () => {
  25. trackAnalytics('ddm.widget.duplicate', {
  26. organization,
  27. });
  28. Sentry.metrics.increment('ddm.widget.duplicate');
  29. duplicateWidget(widgetIndex);
  30. },
  31. },
  32. {
  33. leadingItems: [<IconClose key="icon" />],
  34. key: 'delete',
  35. label: t('Remove Query'),
  36. disabled: !canDelete,
  37. onAction: () => {
  38. Sentry.metrics.increment('ddm.widget.delete');
  39. removeWidget(widgetIndex);
  40. },
  41. },
  42. ],
  43. [canDelete, organization, duplicateWidget, widgetIndex, removeWidget]
  44. );
  45. if (!hasDDMFeature(organization)) {
  46. return null;
  47. }
  48. return (
  49. <DropdownMenu
  50. items={items}
  51. triggerProps={{
  52. 'aria-label': t('Widget actions'),
  53. size: 'md',
  54. showChevron: false,
  55. icon: <IconEllipsis direction="down" size="sm" />,
  56. }}
  57. position="bottom-end"
  58. />
  59. );
  60. }