widgetCardContextMenu.tsx 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. showWidgetViewerButton?: boolean;
  44. tableData?: TableDataWithTitle[];
  45. totalIssuesCount?: string;
  46. };
  47. function WidgetCardContextMenu({
  48. organization,
  49. selection,
  50. widget,
  51. widgetLimitReached,
  52. onDelete,
  53. onDuplicate,
  54. onEdit,
  55. showContextMenu,
  56. isPreview,
  57. showWidgetViewerButton,
  58. router,
  59. location,
  60. index,
  61. seriesData,
  62. tableData,
  63. pageLinks,
  64. totalIssuesCount,
  65. seriesResultsType,
  66. }: Props) {
  67. const {isMetricsData} = useDashboardsMEPContext();
  68. if (!showContextMenu) {
  69. return null;
  70. }
  71. const menuOptions: MenuItemProps[] = [];
  72. const disabledKeys: string[] = [];
  73. const openWidgetViewerPath = (id: string | undefined) => {
  74. if (!isWidgetViewerPath(location.pathname)) {
  75. router.push({
  76. pathname: `${location.pathname}${
  77. location.pathname.endsWith('/') ? '' : '/'
  78. }widget/${id}/`,
  79. query: location.query,
  80. });
  81. }
  82. };
  83. if (isPreview) {
  84. return (
  85. <WidgetViewerContext.Consumer>
  86. {({setData}) => (
  87. <MEPConsumer>
  88. {metricSettingContext => (
  89. <ContextWrapper>
  90. {(organization.features.includes('dashboards-mep') ||
  91. organization.features.includes('mep-rollout-flag')) &&
  92. isMetricsData === false &&
  93. metricSettingContext &&
  94. metricSettingContext.metricSettingState !==
  95. MEPState.transactionsOnly && (
  96. <SampledTag
  97. tooltipText={t('This widget is only applicable to indexed events.')}
  98. >
  99. {t('Indexed')}
  100. </SampledTag>
  101. )}
  102. <StyledDropdownMenuControl
  103. items={[
  104. {
  105. key: 'preview',
  106. label: t(
  107. 'This is a preview only. To edit, you must add this dashboard.'
  108. ),
  109. },
  110. ]}
  111. triggerProps={{
  112. 'aria-label': t('Widget actions'),
  113. size: 'xs',
  114. borderless: true,
  115. showChevron: false,
  116. icon: <IconEllipsis direction="down" size="sm" />,
  117. }}
  118. placement="bottom right"
  119. disabledKeys={[...disabledKeys, 'preview']}
  120. />
  121. {showWidgetViewerButton && (
  122. <OpenWidgetViewerButton
  123. aria-label={t('Open Widget Viewer')}
  124. priority="link"
  125. size="zero"
  126. icon={<IconExpand size="xs" />}
  127. onClick={() => {
  128. (seriesData || tableData) &&
  129. setData({
  130. seriesData,
  131. tableData,
  132. pageLinks,
  133. totalIssuesCount,
  134. seriesResultsType,
  135. });
  136. openWidgetViewerPath(index);
  137. }}
  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. trackAdvancedAnalyticsEvent('dashboards_views.open_in_discover.opened', {
  167. organization,
  168. widget_type: widget.displayType,
  169. });
  170. return;
  171. }
  172. trackAdvancedAnalyticsEvent('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 (organization.features.includes('dashboards-edit')) {
  190. menuOptions.push({
  191. key: 'duplicate-widget',
  192. label: t('Duplicate Widget'),
  193. onAction: () => onDuplicate?.(),
  194. });
  195. widgetLimitReached && disabledKeys.push('duplicate-widget');
  196. menuOptions.push({
  197. key: 'edit-widget',
  198. label: t('Edit Widget'),
  199. onAction: () => onEdit?.(),
  200. });
  201. menuOptions.push({
  202. key: 'delete-widget',
  203. label: t('Delete Widget'),
  204. priority: 'danger',
  205. onAction: () => {
  206. openConfirmModal({
  207. message: t('Are you sure you want to delete this widget?'),
  208. priority: 'danger',
  209. onConfirm: () => onDelete?.(),
  210. });
  211. },
  212. });
  213. }
  214. if (!menuOptions.length) {
  215. return null;
  216. }
  217. return (
  218. <WidgetViewerContext.Consumer>
  219. {({setData}) => (
  220. <MEPConsumer>
  221. {metricSettingContext => (
  222. <ContextWrapper>
  223. {(organization.features.includes('dashboards-mep') ||
  224. organization.features.includes('mep-rollout-flag')) &&
  225. isMetricsData === false &&
  226. metricSettingContext &&
  227. metricSettingContext.metricSettingState !== MEPState.transactionsOnly && (
  228. <SampledTag
  229. tooltipText={t('This widget is only applicable to indexed events.')}
  230. >
  231. {t('Indexed')}
  232. </SampledTag>
  233. )}
  234. <StyledDropdownMenuControl
  235. items={menuOptions}
  236. triggerProps={{
  237. 'aria-label': t('Widget actions'),
  238. size: 'xs',
  239. borderless: true,
  240. showChevron: false,
  241. icon: <IconEllipsis direction="down" size="sm" />,
  242. }}
  243. placement="bottom right"
  244. disabledKeys={[...disabledKeys]}
  245. />
  246. {showWidgetViewerButton && (
  247. <OpenWidgetViewerButton
  248. aria-label={t('Open Widget Viewer')}
  249. priority="link"
  250. size="zero"
  251. icon={<IconExpand size="xs" />}
  252. onClick={() => {
  253. setData({
  254. seriesData,
  255. tableData,
  256. pageLinks,
  257. totalIssuesCount,
  258. seriesResultsType,
  259. });
  260. openWidgetViewerPath(widget.id ?? index);
  261. }}
  262. />
  263. )}
  264. </ContextWrapper>
  265. )}
  266. </MEPConsumer>
  267. )}
  268. </WidgetViewerContext.Consumer>
  269. );
  270. }
  271. export default WidgetCardContextMenu;
  272. const ContextWrapper = styled('div')`
  273. display: flex;
  274. align-items: center;
  275. height: ${space(3)};
  276. margin-left: ${space(1)};
  277. `;
  278. const StyledDropdownMenuControl = styled(DropdownMenuControl)`
  279. & > button {
  280. z-index: auto;
  281. }
  282. `;
  283. const OpenWidgetViewerButton = styled(Button)`
  284. padding: ${space(0.75)} ${space(1)};
  285. color: ${p => p.theme.textColor};
  286. &:hover {
  287. color: ${p => p.theme.textColor};
  288. background: ${p => p.theme.surface400};
  289. border-color: transparent;
  290. }
  291. `;
  292. const SampledTag = styled(Tag)`
  293. margin-right: ${space(0.5)};
  294. `;