widgetCardContextMenu.tsx 9.1 KB

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