widgetCardContextMenu.tsx 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. isMetricsData === false &&
  89. metricSettingContext &&
  90. metricSettingContext.metricSettingState !==
  91. MEPState.TRANSACTIONS_ONLY && (
  92. <SampledTag
  93. tooltipText={t('This widget is only applicable to indexed events.')}
  94. >
  95. {t('Indexed')}
  96. </SampledTag>
  97. )}
  98. <StyledDropdownMenuControl
  99. items={[
  100. {
  101. key: 'preview',
  102. label: t(
  103. 'This is a preview only. To edit, you must add this dashboard.'
  104. ),
  105. },
  106. ]}
  107. triggerProps={{
  108. 'aria-label': t('Widget actions'),
  109. size: 'xs',
  110. borderless: true,
  111. showChevron: false,
  112. icon: <IconEllipsis direction="down" size="sm" />,
  113. }}
  114. position="bottom-end"
  115. disabledKeys={[...disabledKeys, 'preview']}
  116. />
  117. <Button
  118. aria-label={t('Open Widget Viewer')}
  119. borderless
  120. size="xs"
  121. icon={<IconExpand size="xs" />}
  122. onClick={() => {
  123. (seriesData || tableData) &&
  124. setData({
  125. seriesData,
  126. tableData,
  127. pageLinks,
  128. totalIssuesCount,
  129. seriesResultsType,
  130. });
  131. openWidgetViewerPath(index);
  132. }}
  133. />
  134. </ContextWrapper>
  135. )}
  136. </MEPConsumer>
  137. )}
  138. </WidgetViewerContext.Consumer>
  139. );
  140. }
  141. if (
  142. organization.features.includes('discover-basic') &&
  143. widget.widgetType === WidgetType.DISCOVER
  144. ) {
  145. // Open Widget in Discover
  146. if (widget.queries.length) {
  147. const discoverPath = getWidgetDiscoverUrl(
  148. widget,
  149. selection,
  150. organization,
  151. 0,
  152. isMetricsData
  153. );
  154. menuOptions.push({
  155. key: 'open-in-discover',
  156. label: t('Open in Discover'),
  157. to: widget.queries.length === 1 ? discoverPath : undefined,
  158. onAction: () => {
  159. if (widget.queries.length === 1) {
  160. trackAnalytics('dashboards_views.open_in_discover.opened', {
  161. organization,
  162. widget_type: widget.displayType,
  163. });
  164. return;
  165. }
  166. trackAnalytics('dashboards_views.query_selector.opened', {
  167. organization,
  168. widget_type: widget.displayType,
  169. });
  170. openDashboardWidgetQuerySelectorModal({organization, widget, isMetricsData});
  171. },
  172. });
  173. }
  174. }
  175. if (widget.widgetType === WidgetType.ISSUE) {
  176. const issuesLocation = getWidgetIssueUrl(widget, selection, organization);
  177. menuOptions.push({
  178. key: 'open-in-issues',
  179. label: t('Open in Issues'),
  180. to: issuesLocation,
  181. });
  182. }
  183. if (organization.features.includes('dashboards-edit')) {
  184. menuOptions.push({
  185. key: 'duplicate-widget',
  186. label: t('Duplicate Widget'),
  187. onAction: () => onDuplicate?.(),
  188. });
  189. widgetLimitReached && disabledKeys.push('duplicate-widget');
  190. menuOptions.push({
  191. key: 'edit-widget',
  192. label: t('Edit Widget'),
  193. onAction: () => onEdit?.(),
  194. });
  195. menuOptions.push({
  196. key: 'delete-widget',
  197. label: t('Delete Widget'),
  198. priority: 'danger',
  199. onAction: () => {
  200. openConfirmModal({
  201. message: t('Are you sure you want to delete this widget?'),
  202. priority: 'danger',
  203. onConfirm: () => onDelete?.(),
  204. });
  205. },
  206. });
  207. }
  208. if (!menuOptions.length) {
  209. return null;
  210. }
  211. return (
  212. <WidgetViewerContext.Consumer>
  213. {({setData}) => (
  214. <MEPConsumer>
  215. {metricSettingContext => (
  216. <ContextWrapper>
  217. {!organization.features.includes('performance-mep-bannerless-ui') &&
  218. isMetricsData === false &&
  219. metricSettingContext &&
  220. metricSettingContext.metricSettingState !==
  221. MEPState.TRANSACTIONS_ONLY && (
  222. <SampledTag
  223. tooltipText={t('This widget is only applicable to indexed events.')}
  224. >
  225. {t('Indexed')}
  226. </SampledTag>
  227. )}
  228. <StyledDropdownMenuControl
  229. items={menuOptions}
  230. triggerProps={{
  231. 'aria-label': t('Widget actions'),
  232. size: 'xs',
  233. borderless: true,
  234. showChevron: false,
  235. icon: <IconEllipsis direction="down" size="sm" />,
  236. }}
  237. position="bottom-end"
  238. disabledKeys={[...disabledKeys]}
  239. />
  240. <Button
  241. aria-label={t('Open Widget Viewer')}
  242. borderless
  243. size="xs"
  244. icon={<IconExpand size="xs" />}
  245. onClick={() => {
  246. setData({
  247. seriesData,
  248. tableData,
  249. pageLinks,
  250. totalIssuesCount,
  251. seriesResultsType,
  252. });
  253. openWidgetViewerPath(widget.id ?? index);
  254. }}
  255. />
  256. </ContextWrapper>
  257. )}
  258. </MEPConsumer>
  259. )}
  260. </WidgetViewerContext.Consumer>
  261. );
  262. }
  263. export default WidgetCardContextMenu;
  264. const ContextWrapper = styled('div')`
  265. display: flex;
  266. align-items: center;
  267. height: ${space(3)};
  268. margin-left: ${space(1)};
  269. gap: ${space(0.25)};
  270. `;
  271. const StyledDropdownMenuControl = styled(DropdownMenu)`
  272. display: flex;
  273. & > button {
  274. z-index: auto;
  275. }
  276. `;
  277. const SampledTag = styled(Tag)`
  278. margin-right: ${space(0.5)};
  279. `;