metricQueryContextMenu.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import {useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {openAddToDashboardModal, openModal} from 'sentry/actionCreators/modal';
  5. import {navigateTo} from 'sentry/actionCreators/navigation';
  6. import Feature from 'sentry/components/acl/feature';
  7. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  8. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  9. import {
  10. IconClose,
  11. IconCopy,
  12. IconDashboard,
  13. IconEllipsis,
  14. IconSettings,
  15. IconSiren,
  16. } from 'sentry/icons';
  17. import {t} from 'sentry/locale';
  18. import type {Organization} from 'sentry/types/organization';
  19. import {trackAnalytics} from 'sentry/utils/analytics';
  20. import {isCustomMeasurement, isCustomMetric} from 'sentry/utils/metrics';
  21. import {
  22. convertToDashboardWidget,
  23. encodeWidgetQuery,
  24. getWidgetAsQueryParams,
  25. getWidgetQuery,
  26. } from 'sentry/utils/metrics/dashboard';
  27. import {hasCustomMetrics} from 'sentry/utils/metrics/features';
  28. import {
  29. isMetricsQueryWidget,
  30. type MetricDisplayType,
  31. type MetricsQuery,
  32. } from 'sentry/utils/metrics/types';
  33. import useOrganization from 'sentry/utils/useOrganization';
  34. import usePageFilters from 'sentry/utils/usePageFilters';
  35. import useRouter from 'sentry/utils/useRouter';
  36. import {useMetricsContext} from 'sentry/views/metrics/context';
  37. import {CreateAlertModal} from 'sentry/views/metrics/createAlertModal';
  38. import {OrganizationContext} from 'sentry/views/organizationContext';
  39. type ContextMenuProps = {
  40. displayType: MetricDisplayType;
  41. metricsQuery: MetricsQuery;
  42. widgetIndex: number;
  43. };
  44. export function MetricQueryContextMenu({
  45. metricsQuery,
  46. displayType,
  47. widgetIndex,
  48. }: ContextMenuProps) {
  49. const organization = useOrganization();
  50. const router = useRouter();
  51. const {removeWidget, duplicateWidget, widgets} = useMetricsContext();
  52. const createAlert = useMemo(
  53. () => getCreateAlert(organization, metricsQuery),
  54. [metricsQuery, organization]
  55. );
  56. const createDashboardWidget = useCreateDashboardWidget(
  57. organization,
  58. metricsQuery,
  59. displayType
  60. );
  61. // At least one query must remain
  62. const canDelete = widgets.filter(isMetricsQueryWidget).length > 1;
  63. const items = useMemo<MenuItemProps[]>(
  64. () => [
  65. {
  66. leadingItems: [<IconCopy key="icon" />],
  67. key: 'duplicate',
  68. label: t('Duplicate'),
  69. onAction: () => {
  70. trackAnalytics('ddm.widget.duplicate', {
  71. organization,
  72. });
  73. Sentry.metrics.increment('ddm.widget.duplicate');
  74. duplicateWidget(widgetIndex);
  75. },
  76. },
  77. {
  78. leadingItems: [<IconSiren key="icon" />],
  79. key: 'add-alert',
  80. label: t('Create Alert'),
  81. disabled: !createAlert,
  82. onAction: () => {
  83. trackAnalytics('ddm.create-alert', {
  84. organization,
  85. source: 'widget',
  86. });
  87. Sentry.metrics.increment('ddm.widget.alert');
  88. createAlert?.();
  89. },
  90. },
  91. {
  92. leadingItems: [<IconDashboard key="icon" />],
  93. key: 'add-dashboard',
  94. label: (
  95. <Feature
  96. organization={organization}
  97. hookName="feature-disabled:dashboards-edit"
  98. features="dashboards-edit"
  99. >
  100. {({hasFeature}) => (
  101. <AddToDashboardItem disabled={!hasFeature}>
  102. {t('Add to Dashboard')}
  103. </AddToDashboardItem>
  104. )}
  105. </Feature>
  106. ),
  107. disabled: !createDashboardWidget,
  108. onAction: () => {
  109. if (!organization.features.includes('dashboards-edit')) {
  110. return;
  111. }
  112. trackAnalytics('ddm.add-to-dashboard', {
  113. organization,
  114. source: 'widget',
  115. });
  116. Sentry.metrics.increment('ddm.widget.dashboard');
  117. createDashboardWidget?.();
  118. },
  119. },
  120. {
  121. leadingItems: [<IconSettings key="icon" />],
  122. key: 'settings',
  123. label: t('Metric Settings'),
  124. disabled: !isCustomMetric({mri: metricsQuery.mri}),
  125. onAction: () => {
  126. trackAnalytics('ddm.widget.settings', {
  127. organization,
  128. });
  129. Sentry.metrics.increment('ddm.widget.settings');
  130. navigateTo(
  131. `/settings/projects/:projectId/metrics/${encodeURIComponent(
  132. metricsQuery.mri
  133. )}`,
  134. router
  135. );
  136. },
  137. },
  138. {
  139. leadingItems: [<IconClose key="icon" />],
  140. key: 'delete',
  141. label: t('Remove Metric'),
  142. disabled: !canDelete,
  143. onAction: () => {
  144. Sentry.metrics.increment('ddm.widget.delete');
  145. removeWidget(widgetIndex);
  146. },
  147. },
  148. ],
  149. [
  150. createAlert,
  151. createDashboardWidget,
  152. metricsQuery.mri,
  153. canDelete,
  154. organization,
  155. duplicateWidget,
  156. widgetIndex,
  157. router,
  158. removeWidget,
  159. ]
  160. );
  161. if (!hasCustomMetrics(organization)) {
  162. return null;
  163. }
  164. return (
  165. <DropdownMenu
  166. items={items}
  167. triggerProps={{
  168. 'aria-label': t('Widget actions'),
  169. size: 'md',
  170. showChevron: false,
  171. icon: <IconEllipsis direction="down" size="sm" />,
  172. }}
  173. position="bottom-end"
  174. />
  175. );
  176. }
  177. export function getCreateAlert(organization: Organization, metricsQuery: MetricsQuery) {
  178. if (
  179. !metricsQuery.mri ||
  180. !metricsQuery.op ||
  181. isCustomMeasurement(metricsQuery) ||
  182. !organization.access.includes('alerts:write')
  183. ) {
  184. return undefined;
  185. }
  186. return function () {
  187. return openModal(deps => (
  188. <OrganizationContext.Provider value={organization}>
  189. <CreateAlertModal metricsQuery={metricsQuery} {...deps} />
  190. </OrganizationContext.Provider>
  191. ));
  192. };
  193. }
  194. export function useCreateDashboardWidget(
  195. organization: Organization,
  196. metricsQuery: MetricsQuery,
  197. displayType?: MetricDisplayType
  198. ) {
  199. const router = useRouter();
  200. const {selection} = usePageFilters();
  201. return useMemo(() => {
  202. if (!metricsQuery.mri || !metricsQuery.op || isCustomMeasurement(metricsQuery)) {
  203. return undefined;
  204. }
  205. const widgetQuery = getWidgetQuery(metricsQuery);
  206. const urlWidgetQuery = encodeWidgetQuery(widgetQuery);
  207. const widgetAsQueryParams = getWidgetAsQueryParams(
  208. selection,
  209. urlWidgetQuery,
  210. displayType
  211. );
  212. return () =>
  213. openAddToDashboardModal({
  214. organization,
  215. selection,
  216. widget: convertToDashboardWidget([metricsQuery], displayType),
  217. router,
  218. widgetAsQueryParams,
  219. location: router.location,
  220. actions: ['add-and-open-dashboard', 'add-and-stay-on-current-page'],
  221. });
  222. }, [metricsQuery, selection, displayType, organization, router]);
  223. }
  224. const AddToDashboardItem = styled('div')<{disabled: boolean}>`
  225. color: ${p => (p.disabled ? p.theme.disabled : p.theme.textColor)};
  226. `;