widgetCardContextMenu.tsx 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. import type {InjectedRouter} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {openDashboardWidgetQuerySelectorModal} from 'sentry/actionCreators/modal';
  5. import Tag from 'sentry/components/badge/tag';
  6. import {Button} from 'sentry/components/button';
  7. import {openConfirmModal} from 'sentry/components/confirm';
  8. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  9. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  10. import {isWidgetViewerPath} from 'sentry/components/modals/widgetViewerModal/utils';
  11. import {IconEllipsis, IconExpand} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import {space} from 'sentry/styles/space';
  14. import type {PageFilters} from 'sentry/types/core';
  15. import type {Series} from 'sentry/types/echarts';
  16. import type {Organization} from 'sentry/types/organization';
  17. import {trackAnalytics} from 'sentry/utils/analytics';
  18. import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  19. import type {AggregationOutputType} from 'sentry/utils/discover/fields';
  20. import {
  21. MEPConsumer,
  22. MEPState,
  23. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  24. import {
  25. getWidgetDiscoverUrl,
  26. getWidgetIssueUrl,
  27. getWidgetMetricsUrl,
  28. hasDatasetSelector,
  29. } from 'sentry/views/dashboards/utils';
  30. import type {Widget} from '../types';
  31. import {WidgetType} from '../types';
  32. import {WidgetViewerContext} from '../widgetViewer/widgetViewerContext';
  33. import {useDashboardsMEPContext} from './dashboardsMEPContext';
  34. type Props = {
  35. location: Location;
  36. organization: Organization;
  37. router: InjectedRouter;
  38. selection: PageFilters;
  39. widget: Widget;
  40. widgetLimitReached: boolean;
  41. index?: string;
  42. isPreview?: boolean;
  43. onDelete?: () => void;
  44. onDuplicate?: () => void;
  45. onEdit?: () => void;
  46. pageLinks?: string;
  47. seriesData?: Series[];
  48. seriesResultsType?: Record<string, AggregationOutputType>;
  49. showContextMenu?: boolean;
  50. tableData?: TableDataWithTitle[];
  51. totalIssuesCount?: string;
  52. };
  53. function WidgetCardContextMenu({
  54. organization,
  55. selection,
  56. widget,
  57. widgetLimitReached,
  58. onDelete,
  59. onDuplicate,
  60. onEdit,
  61. showContextMenu,
  62. isPreview,
  63. router,
  64. location,
  65. index,
  66. seriesData,
  67. tableData,
  68. pageLinks,
  69. totalIssuesCount,
  70. seriesResultsType,
  71. }: Props) {
  72. const {isMetricsData} = useDashboardsMEPContext();
  73. if (!showContextMenu) {
  74. return null;
  75. }
  76. const menuOptions: MenuItemProps[] = [];
  77. const disabledKeys: string[] = [];
  78. const openWidgetViewerPath = (id: string | undefined) => {
  79. if (!isWidgetViewerPath(location.pathname)) {
  80. router.push({
  81. pathname: `${location.pathname}${
  82. location.pathname.endsWith('/') ? '' : '/'
  83. }widget/${id}/`,
  84. query: location.query,
  85. });
  86. }
  87. };
  88. if (isPreview) {
  89. return (
  90. <WidgetViewerContext.Consumer>
  91. {({setData}) => (
  92. <MEPConsumer>
  93. {metricSettingContext => (
  94. <ContextWrapper>
  95. {!organization.features.includes('performance-mep-bannerless-ui') &&
  96. isMetricsData === false &&
  97. metricSettingContext &&
  98. metricSettingContext.metricSettingState !==
  99. MEPState.TRANSACTIONS_ONLY && (
  100. <SampledTag
  101. tooltipText={t('This widget is only applicable to indexed events.')}
  102. >
  103. {t('Indexed')}
  104. </SampledTag>
  105. )}
  106. <StyledDropdownMenuControl
  107. items={[
  108. {
  109. key: 'preview',
  110. label: t(
  111. 'This is a preview only. To edit, you must add this dashboard.'
  112. ),
  113. },
  114. ]}
  115. triggerProps={{
  116. 'aria-label': t('Widget actions'),
  117. size: 'xs',
  118. borderless: true,
  119. showChevron: false,
  120. icon: <IconEllipsis direction="down" size="sm" />,
  121. }}
  122. position="bottom-end"
  123. disabledKeys={[...disabledKeys, 'preview']}
  124. />
  125. <Button
  126. aria-label={t('Open Widget Viewer')}
  127. borderless
  128. size="xs"
  129. icon={<IconExpand />}
  130. onClick={() => {
  131. (seriesData || tableData) &&
  132. setData({
  133. seriesData,
  134. tableData,
  135. pageLinks,
  136. totalIssuesCount,
  137. seriesResultsType,
  138. });
  139. openWidgetViewerPath(index);
  140. }}
  141. />
  142. </ContextWrapper>
  143. )}
  144. </MEPConsumer>
  145. )}
  146. </WidgetViewerContext.Consumer>
  147. );
  148. }
  149. if (
  150. organization.features.includes('discover-basic') &&
  151. widget.widgetType &&
  152. [WidgetType.DISCOVER, WidgetType.ERRORS, WidgetType.TRANSACTIONS].includes(
  153. widget.widgetType
  154. )
  155. ) {
  156. const optionDisabled =
  157. hasDatasetSelector(organization) && widget.widgetType === WidgetType.DISCOVER;
  158. // Open Widget in Discover
  159. if (widget.queries.length) {
  160. const discoverPath = getWidgetDiscoverUrl(
  161. widget,
  162. selection,
  163. organization,
  164. 0,
  165. isMetricsData
  166. );
  167. menuOptions.push({
  168. key: 'open-in-discover',
  169. label: t('Open in Discover'),
  170. to: optionDisabled
  171. ? undefined
  172. : widget.queries.length === 1
  173. ? discoverPath
  174. : undefined,
  175. tooltip: t(
  176. 'We are splitting datasets to make them easier to digest. Please confirm the dataset for this widget by clicking Edit Widget.'
  177. ),
  178. tooltipOptions: {disabled: !optionDisabled},
  179. disabled: optionDisabled,
  180. showDetailsInOverlay: true,
  181. onAction: () => {
  182. if (widget.queries.length === 1) {
  183. trackAnalytics('dashboards_views.open_in_discover.opened', {
  184. organization,
  185. widget_type: widget.displayType,
  186. });
  187. return;
  188. }
  189. trackAnalytics('dashboards_views.query_selector.opened', {
  190. organization,
  191. widget_type: widget.displayType,
  192. });
  193. openDashboardWidgetQuerySelectorModal({organization, widget, isMetricsData});
  194. },
  195. });
  196. }
  197. }
  198. if (widget.widgetType === WidgetType.ISSUE) {
  199. const issuesLocation = getWidgetIssueUrl(widget, selection, organization);
  200. menuOptions.push({
  201. key: 'open-in-issues',
  202. label: t('Open in Issues'),
  203. to: issuesLocation,
  204. });
  205. }
  206. if (widget.widgetType === WidgetType.METRICS) {
  207. const metricsLocation = getWidgetMetricsUrl(widget, selection, organization);
  208. menuOptions.push({
  209. key: 'open-in-metrics',
  210. label: t('Open in Metrics'),
  211. to: metricsLocation,
  212. });
  213. }
  214. if (organization.features.includes('dashboards-edit')) {
  215. menuOptions.push({
  216. key: 'duplicate-widget',
  217. label: t('Duplicate Widget'),
  218. onAction: () => onDuplicate?.(),
  219. });
  220. widgetLimitReached && disabledKeys.push('duplicate-widget');
  221. menuOptions.push({
  222. key: 'edit-widget',
  223. label: t('Edit Widget'),
  224. onAction: () => onEdit?.(),
  225. });
  226. menuOptions.push({
  227. key: 'delete-widget',
  228. label: t('Delete Widget'),
  229. priority: 'danger',
  230. onAction: () => {
  231. openConfirmModal({
  232. message: t('Are you sure you want to delete this widget?'),
  233. priority: 'danger',
  234. onConfirm: () => onDelete?.(),
  235. });
  236. },
  237. });
  238. }
  239. if (!menuOptions.length) {
  240. return null;
  241. }
  242. return (
  243. <WidgetViewerContext.Consumer>
  244. {({setData}) => (
  245. <MEPConsumer>
  246. {metricSettingContext => (
  247. <ContextWrapper>
  248. {!organization.features.includes('performance-mep-bannerless-ui') &&
  249. isMetricsData === false &&
  250. metricSettingContext &&
  251. metricSettingContext.metricSettingState !==
  252. MEPState.TRANSACTIONS_ONLY && (
  253. <SampledTag
  254. tooltipText={t('This widget is only applicable to indexed events.')}
  255. >
  256. {t('Indexed')}
  257. </SampledTag>
  258. )}
  259. <StyledDropdownMenuControl
  260. items={menuOptions}
  261. triggerProps={{
  262. 'aria-label': t('Widget actions'),
  263. size: 'xs',
  264. borderless: true,
  265. showChevron: false,
  266. icon: <IconEllipsis direction="down" size="sm" />,
  267. }}
  268. position="bottom-end"
  269. disabledKeys={[...disabledKeys]}
  270. />
  271. <Button
  272. aria-label={t('Open Widget Viewer')}
  273. borderless
  274. size="xs"
  275. icon={<IconExpand />}
  276. onClick={() => {
  277. setData({
  278. seriesData,
  279. tableData,
  280. pageLinks,
  281. totalIssuesCount,
  282. seriesResultsType,
  283. });
  284. openWidgetViewerPath(widget.id ?? index);
  285. }}
  286. />
  287. </ContextWrapper>
  288. )}
  289. </MEPConsumer>
  290. )}
  291. </WidgetViewerContext.Consumer>
  292. );
  293. }
  294. export default WidgetCardContextMenu;
  295. const ContextWrapper = styled('div')`
  296. display: flex;
  297. align-items: center;
  298. height: ${space(3)};
  299. margin-left: ${space(1)};
  300. gap: ${space(0.25)};
  301. `;
  302. const StyledDropdownMenuControl = styled(DropdownMenu)`
  303. display: flex;
  304. & > button {
  305. z-index: auto;
  306. }
  307. `;
  308. const SampledTag = styled(Tag)`
  309. margin-right: ${space(0.5)};
  310. `;