metricQueryContextMenu.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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';
  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. type MetricDisplayType,
  30. MetricQueryType,
  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 {useDDMContext} from 'sentry/views/ddm/context';
  37. import {CreateAlertModal} from 'sentry/views/ddm/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} = useDDMContext();
  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 =
  63. widgets.filter(widget => widget.type === MetricQueryType.QUERY).length > 1;
  64. const items = useMemo<MenuItemProps[]>(
  65. () => [
  66. {
  67. leadingItems: [<IconCopy key="icon" />],
  68. key: 'duplicate',
  69. label: t('Duplicate'),
  70. onAction: () => {
  71. trackAnalytics('ddm.widget.duplicate', {
  72. organization,
  73. });
  74. Sentry.metrics.increment('ddm.widget.duplicate');
  75. duplicateWidget(widgetIndex);
  76. },
  77. },
  78. {
  79. leadingItems: [<IconSiren key="icon" />],
  80. key: 'add-alert',
  81. label: t('Create Alert'),
  82. disabled: !createAlert,
  83. onAction: () => {
  84. trackAnalytics('ddm.create-alert', {
  85. organization,
  86. source: 'widget',
  87. });
  88. Sentry.metrics.increment('ddm.widget.alert');
  89. createAlert?.();
  90. },
  91. },
  92. {
  93. leadingItems: [<IconDashboard key="icon" />],
  94. key: 'add-dashboard',
  95. label: (
  96. <Feature
  97. organization={organization}
  98. hookName="feature-disabled:dashboards-edit"
  99. features="dashboards-edit"
  100. >
  101. {({hasFeature}) => (
  102. <AddToDashboardItem disabled={!hasFeature}>
  103. {t('Add to Dashboard')}
  104. </AddToDashboardItem>
  105. )}
  106. </Feature>
  107. ),
  108. disabled: !createDashboardWidget,
  109. onAction: () => {
  110. if (!organization.features.includes('dashboards-edit')) {
  111. return;
  112. }
  113. trackAnalytics('ddm.add-to-dashboard', {
  114. organization,
  115. source: 'widget',
  116. });
  117. Sentry.metrics.increment('ddm.widget.dashboard');
  118. createDashboardWidget?.();
  119. },
  120. },
  121. {
  122. leadingItems: [<IconSettings key="icon" />],
  123. key: 'settings',
  124. label: t('Metric Settings'),
  125. disabled: !isCustomMetric({mri: metricsQuery.mri}),
  126. onAction: () => {
  127. trackAnalytics('ddm.widget.settings', {
  128. organization,
  129. });
  130. Sentry.metrics.increment('ddm.widget.settings');
  131. navigateTo(
  132. `/settings/projects/:projectId/metrics/${encodeURIComponent(
  133. metricsQuery.mri
  134. )}`,
  135. router
  136. );
  137. },
  138. },
  139. {
  140. leadingItems: [<IconClose key="icon" />],
  141. key: 'delete',
  142. label: t('Remove Query'),
  143. disabled: !canDelete,
  144. onAction: () => {
  145. Sentry.metrics.increment('ddm.widget.delete');
  146. removeWidget(widgetIndex);
  147. },
  148. },
  149. ],
  150. [
  151. createAlert,
  152. createDashboardWidget,
  153. metricsQuery.mri,
  154. canDelete,
  155. organization,
  156. duplicateWidget,
  157. widgetIndex,
  158. router,
  159. removeWidget,
  160. ]
  161. );
  162. if (!hasCustomMetrics(organization)) {
  163. return null;
  164. }
  165. return (
  166. <DropdownMenu
  167. items={items}
  168. triggerProps={{
  169. 'aria-label': t('Widget actions'),
  170. size: 'md',
  171. showChevron: false,
  172. icon: <IconEllipsis direction="down" size="sm" />,
  173. }}
  174. position="bottom-end"
  175. />
  176. );
  177. }
  178. export function getCreateAlert(organization: Organization, metricsQuery: MetricsQuery) {
  179. if (
  180. !metricsQuery.mri ||
  181. !metricsQuery.op ||
  182. isCustomMeasurement(metricsQuery) ||
  183. !organization.access.includes('alerts:write')
  184. ) {
  185. return undefined;
  186. }
  187. return function () {
  188. return openModal(deps => (
  189. <OrganizationContext.Provider value={organization}>
  190. <CreateAlertModal metricsQuery={metricsQuery} {...deps} />
  191. </OrganizationContext.Provider>
  192. ));
  193. };
  194. }
  195. export function useCreateDashboardWidget(
  196. organization: Organization,
  197. metricsQuery: MetricsQuery,
  198. displayType?: MetricDisplayType
  199. ) {
  200. const router = useRouter();
  201. const {selection} = usePageFilters();
  202. return useMemo(() => {
  203. if (!metricsQuery.mri || !metricsQuery.op || isCustomMeasurement(metricsQuery)) {
  204. return undefined;
  205. }
  206. const widgetQuery = getWidgetQuery(metricsQuery);
  207. const urlWidgetQuery = encodeWidgetQuery(widgetQuery);
  208. const widgetAsQueryParams = getWidgetAsQueryParams(
  209. selection,
  210. urlWidgetQuery,
  211. displayType
  212. );
  213. return () =>
  214. openAddToDashboardModal({
  215. organization,
  216. selection,
  217. widget: convertToDashboardWidget([metricsQuery], displayType),
  218. router,
  219. widgetAsQueryParams,
  220. location: router.location,
  221. actions: ['add-and-open-dashboard', 'add-and-stay-on-current-page'],
  222. });
  223. }, [metricsQuery, selection, displayType, organization, router]);
  224. }
  225. const AddToDashboardItem = styled('div')<{disabled: boolean}>`
  226. color: ${p => (p.disabled ? p.theme.disabled : p.theme.textColor)};
  227. `;