metricQueryContextMenu.tsx 7.9 KB

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