widgetCardContextMenu.tsx 9.0 KB

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