widgetCardContextMenu.tsx 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 Feature from 'sentry/components/acl/feature';
  6. import Button from 'sentry/components/button';
  7. import {openConfirmModal} from 'sentry/components/confirm';
  8. import DropdownMenuControl from 'sentry/components/dropdownMenuControl';
  9. import {MenuItemProps} from 'sentry/components/dropdownMenuItem';
  10. import {isWidgetViewerPath} from 'sentry/components/modals/widgetViewerModal/utils';
  11. import Tag from 'sentry/components/tag';
  12. import Tooltip from 'sentry/components/tooltip';
  13. import {IconEllipsis, IconExpand} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import space from 'sentry/styles/space';
  16. import {Organization, PageFilters} from 'sentry/types';
  17. import {Series} from 'sentry/types/echarts';
  18. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  19. import {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  20. import {AggregationOutputType} from 'sentry/utils/discover/fields';
  21. import {
  22. getWidgetDiscoverUrl,
  23. getWidgetIssueUrl,
  24. isCustomMeasurementWidget,
  25. } from 'sentry/views/dashboardsV2/utils';
  26. import {Widget, WidgetType} from '../types';
  27. import {WidgetViewerContext} from '../widgetViewer/widgetViewerContext';
  28. import {useDashboardsMEPContext} from './dashboardsMEPContext';
  29. type Props = {
  30. location: Location;
  31. organization: Organization;
  32. router: InjectedRouter;
  33. selection: PageFilters;
  34. widget: Widget;
  35. widgetLimitReached: boolean;
  36. index?: string;
  37. isPreview?: boolean;
  38. onDelete?: () => void;
  39. onDuplicate?: () => void;
  40. onEdit?: () => void;
  41. pageLinks?: string;
  42. seriesData?: Series[];
  43. seriesResultsType?: Record<string, AggregationOutputType>;
  44. showContextMenu?: boolean;
  45. showWidgetViewerButton?: boolean;
  46. tableData?: TableDataWithTitle[];
  47. totalIssuesCount?: string;
  48. };
  49. function WidgetCardContextMenu({
  50. organization,
  51. selection,
  52. widget,
  53. widgetLimitReached,
  54. onDelete,
  55. onDuplicate,
  56. onEdit,
  57. showContextMenu,
  58. isPreview,
  59. showWidgetViewerButton,
  60. router,
  61. location,
  62. index,
  63. seriesData,
  64. tableData,
  65. pageLinks,
  66. totalIssuesCount,
  67. seriesResultsType,
  68. }: Props) {
  69. const {isMetricsData} = useDashboardsMEPContext();
  70. if (!showContextMenu) {
  71. return null;
  72. }
  73. const menuOptions: MenuItemProps[] = [];
  74. const usingCustomMeasurements = isCustomMeasurementWidget(widget);
  75. const disabledKeys: string[] = usingCustomMeasurements ? ['open-in-discover'] : [];
  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. <ContextWrapper>
  91. <Feature organization={organization} features={['dashboards-mep']}>
  92. {isMetricsData === false && (
  93. <SampledTag
  94. tooltipText={t('This widget is only applicable to indexed events.')}
  95. >
  96. {t('Indexed')}
  97. </SampledTag>
  98. )}
  99. </Feature>
  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. placement="bottom right"
  117. disabledKeys={[...disabledKeys, 'preview']}
  118. />
  119. {showWidgetViewerButton && (
  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. )}
  138. </ContextWrapper>
  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: usingCustomMeasurements ? (
  159. <Tooltip
  160. skipWrapper
  161. title={t(
  162. 'Widget using custom performance metrics cannot be opened in Discover.'
  163. )}
  164. >
  165. {t('Open in Discover')}
  166. </Tooltip>
  167. ) : (
  168. t('Open in Discover')
  169. ),
  170. to:
  171. !usingCustomMeasurements && widget.queries.length === 1
  172. ? discoverPath
  173. : undefined,
  174. onAction: () => {
  175. if (!usingCustomMeasurements) {
  176. if (widget.queries.length === 1) {
  177. trackAdvancedAnalyticsEvent('dashboards_views.open_in_discover.opened', {
  178. organization,
  179. widget_type: widget.displayType,
  180. });
  181. return;
  182. }
  183. trackAdvancedAnalyticsEvent('dashboards_views.query_selector.opened', {
  184. organization,
  185. widget_type: widget.displayType,
  186. });
  187. openDashboardWidgetQuerySelectorModal({organization, widget, isMetricsData});
  188. }
  189. },
  190. });
  191. }
  192. }
  193. if (widget.widgetType === WidgetType.ISSUE) {
  194. const issuesLocation = getWidgetIssueUrl(widget, selection, organization);
  195. menuOptions.push({
  196. key: 'open-in-issues',
  197. label: t('Open in Issues'),
  198. to: issuesLocation,
  199. });
  200. }
  201. if (organization.features.includes('dashboards-edit')) {
  202. menuOptions.push({
  203. key: 'duplicate-widget',
  204. label: t('Duplicate Widget'),
  205. onAction: () => onDuplicate?.(),
  206. });
  207. widgetLimitReached && disabledKeys.push('duplicate-widget');
  208. menuOptions.push({
  209. key: 'edit-widget',
  210. label: t('Edit Widget'),
  211. onAction: () => onEdit?.(),
  212. });
  213. menuOptions.push({
  214. key: 'delete-widget',
  215. label: t('Delete Widget'),
  216. priority: 'danger',
  217. onAction: () => {
  218. openConfirmModal({
  219. message: t('Are you sure you want to delete this widget?'),
  220. priority: 'danger',
  221. onConfirm: () => onDelete?.(),
  222. });
  223. },
  224. });
  225. }
  226. if (!menuOptions.length) {
  227. return null;
  228. }
  229. return (
  230. <WidgetViewerContext.Consumer>
  231. {({setData}) => (
  232. <ContextWrapper>
  233. <Feature organization={organization} features={['dashboards-mep']}>
  234. {isMetricsData === false && (
  235. <SampledTag
  236. tooltipText={t('This widget is only applicable to indexed events.')}
  237. >
  238. {t('Indexed')}
  239. </SampledTag>
  240. )}
  241. </Feature>
  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. placement="bottom right"
  252. disabledKeys={[...disabledKeys]}
  253. />
  254. {showWidgetViewerButton && (
  255. <OpenWidgetViewerButton
  256. aria-label={t('Open Widget Viewer')}
  257. priority="link"
  258. size="zero"
  259. icon={<IconExpand size="xs" />}
  260. onClick={() => {
  261. setData({
  262. seriesData,
  263. tableData,
  264. pageLinks,
  265. totalIssuesCount,
  266. seriesResultsType,
  267. });
  268. openWidgetViewerPath(widget.id ?? index);
  269. }}
  270. />
  271. )}
  272. </ContextWrapper>
  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. `;
  284. const StyledDropdownMenuControl = styled(DropdownMenuControl)`
  285. & > button {
  286. z-index: auto;
  287. }
  288. `;
  289. const OpenWidgetViewerButton = styled(Button)`
  290. padding: ${space(0.75)} ${space(1)};
  291. color: ${p => p.theme.textColor};
  292. &:hover {
  293. color: ${p => p.theme.textColor};
  294. background: ${p => p.theme.surface400};
  295. border-color: transparent;
  296. }
  297. `;
  298. const SampledTag = styled(Tag)`
  299. margin-right: ${space(0.5)};
  300. `;