widgetCardContextMenu.tsx 9.4 KB

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