widgetCardContextMenu.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. import type React from 'react';
  2. import styled from '@emotion/styled';
  3. import type {Location} from 'history';
  4. import {openDashboardWidgetQuerySelectorModal} from 'sentry/actionCreators/modal';
  5. import Tag from 'sentry/components/badge/tag';
  6. import {Button} from 'sentry/components/button';
  7. import {openConfirmModal} from 'sentry/components/confirm';
  8. import type {MenuItemProps} from 'sentry/components/dropdownMenu';
  9. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  10. import {isWidgetViewerPath} from 'sentry/components/modals/widgetViewerModal/utils';
  11. import {Tooltip} from 'sentry/components/tooltip';
  12. import {IconEllipsis, IconExpand, IconInfo} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {PageFilters} from 'sentry/types/core';
  16. import type {Series} from 'sentry/types/echarts';
  17. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  18. import type {Organization} from 'sentry/types/organization';
  19. import {trackAnalytics} from 'sentry/utils/analytics';
  20. import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  21. import type {AggregationOutputType} from 'sentry/utils/discover/fields';
  22. import {
  23. MEPState,
  24. useMEPSettingContext,
  25. } from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  26. import useOrganization from 'sentry/utils/useOrganization';
  27. import {
  28. getWidgetDiscoverUrl,
  29. getWidgetIssueUrl,
  30. getWidgetMetricsUrl,
  31. hasDatasetSelector,
  32. isUsingPerformanceScore,
  33. performanceScoreTooltip,
  34. } from 'sentry/views/dashboards/utils';
  35. import {getWidgetExploreUrl} from 'sentry/views/dashboards/utils/getWidgetExploreUrl';
  36. import type {Widget} from '../types';
  37. import {WidgetType} from '../types';
  38. import {WidgetViewerContext} from '../widgetViewer/widgetViewerContext';
  39. import {useDashboardsMEPContext} from './dashboardsMEPContext';
  40. type Props = {
  41. location: Location;
  42. organization: Organization;
  43. router: InjectedRouter;
  44. selection: PageFilters;
  45. widget: Widget;
  46. widgetLimitReached: boolean;
  47. description?: string;
  48. hasEditAccess?: boolean;
  49. index?: string;
  50. isPreview?: boolean;
  51. onDelete?: () => void;
  52. onDuplicate?: () => void;
  53. onEdit?: () => void;
  54. pageLinks?: string;
  55. seriesData?: Series[];
  56. seriesResultsType?: Record<string, AggregationOutputType>;
  57. showContextMenu?: boolean;
  58. tableData?: TableDataWithTitle[];
  59. title?: string | React.ReactNode;
  60. totalIssuesCount?: string;
  61. };
  62. export const useIndexedEventsWarning = (): string | null => {
  63. const {isMetricsData} = useDashboardsMEPContext();
  64. const organization = useOrganization();
  65. const metricSettingContext = useMEPSettingContext();
  66. return !organization.features.includes('performance-mep-bannerless-ui') &&
  67. isMetricsData === false &&
  68. metricSettingContext &&
  69. metricSettingContext.metricSettingState !== MEPState.TRANSACTIONS_ONLY
  70. ? t('Indexed')
  71. : null;
  72. };
  73. function WidgetCardContextMenu({
  74. organization,
  75. selection,
  76. widget,
  77. widgetLimitReached,
  78. hasEditAccess,
  79. onDelete,
  80. onDuplicate,
  81. onEdit,
  82. showContextMenu,
  83. isPreview,
  84. router,
  85. location,
  86. index,
  87. seriesData,
  88. tableData,
  89. pageLinks,
  90. totalIssuesCount,
  91. seriesResultsType,
  92. description,
  93. title,
  94. }: Props) {
  95. const indexedEventsWarning = useIndexedEventsWarning();
  96. const {isMetricsData} = useDashboardsMEPContext();
  97. if (!showContextMenu) {
  98. return null;
  99. }
  100. const openWidgetViewerPath = (id: string | undefined) => {
  101. if (!isWidgetViewerPath(location.pathname)) {
  102. router.push({
  103. pathname: `${location.pathname}${
  104. location.pathname.endsWith('/') ? '' : '/'
  105. }widget/${id}/`,
  106. query: location.query,
  107. });
  108. }
  109. };
  110. if (isPreview) {
  111. return (
  112. <WidgetViewerContext.Consumer>
  113. {({setData}) => (
  114. <ContextWrapper>
  115. {indexedEventsWarning ? (
  116. <SampledTag tooltipText={indexedEventsWarning}>{t('Indexed')}</SampledTag>
  117. ) : null}
  118. {title && (
  119. <Tooltip
  120. title={
  121. <span>
  122. <WidgetTooltipTitle>{title}</WidgetTooltipTitle>
  123. {description && (
  124. <WidgetTooltipDescription>{description}</WidgetTooltipDescription>
  125. )}
  126. </span>
  127. }
  128. containerDisplayMode="grid"
  129. isHoverable
  130. >
  131. <WidgetTooltipButton
  132. aria-label={t('Widget description')}
  133. borderless
  134. size="xs"
  135. icon={<IconInfo />}
  136. />
  137. </Tooltip>
  138. )}
  139. <StyledDropdownMenuControl
  140. items={[
  141. {
  142. key: 'preview',
  143. label: t(
  144. 'This is a preview only. To edit, you must add this dashboard.'
  145. ),
  146. disabled: true,
  147. },
  148. ]}
  149. triggerProps={{
  150. 'aria-label': t('Widget actions'),
  151. size: 'xs',
  152. borderless: true,
  153. showChevron: false,
  154. icon: <IconEllipsis direction="down" size="sm" />,
  155. }}
  156. position="bottom-end"
  157. />
  158. <Button
  159. aria-label={t('Open Widget Viewer')}
  160. borderless
  161. size="xs"
  162. icon={<IconExpand />}
  163. onClick={() => {
  164. (seriesData || tableData) &&
  165. setData({
  166. seriesData,
  167. tableData,
  168. pageLinks,
  169. totalIssuesCount,
  170. seriesResultsType,
  171. });
  172. openWidgetViewerPath(index);
  173. }}
  174. />
  175. </ContextWrapper>
  176. )}
  177. </WidgetViewerContext.Consumer>
  178. );
  179. }
  180. const menuOptions = getMenuOptions(
  181. organization,
  182. selection,
  183. widget,
  184. Boolean(isMetricsData),
  185. widgetLimitReached,
  186. hasEditAccess,
  187. onDelete,
  188. onDuplicate,
  189. onEdit
  190. );
  191. if (!menuOptions.length) {
  192. return null;
  193. }
  194. return (
  195. <WidgetViewerContext.Consumer>
  196. {({setData}) => (
  197. <ContextWrapper>
  198. {indexedEventsWarning ? (
  199. <SampledTag tooltipText={indexedEventsWarning}>{t('Indexed')}</SampledTag>
  200. ) : null}
  201. {title && (
  202. <Tooltip
  203. title={
  204. <span>
  205. <WidgetTooltipTitle>{title}</WidgetTooltipTitle>
  206. {description && (
  207. <WidgetTooltipDescription>{description}</WidgetTooltipDescription>
  208. )}
  209. </span>
  210. }
  211. containerDisplayMode="grid"
  212. isHoverable
  213. >
  214. <WidgetTooltipButton
  215. aria-label={t('Widget description')}
  216. borderless
  217. size="xs"
  218. icon={<IconInfo />}
  219. />
  220. </Tooltip>
  221. )}
  222. <StyledDropdownMenuControl
  223. items={menuOptions}
  224. triggerProps={{
  225. 'aria-label': t('Widget actions'),
  226. size: 'xs',
  227. borderless: true,
  228. showChevron: false,
  229. icon: <IconEllipsis direction="down" size="sm" />,
  230. }}
  231. position="bottom-end"
  232. />
  233. <Button
  234. aria-label={t('Open Widget Viewer')}
  235. borderless
  236. size="xs"
  237. icon={<IconExpand />}
  238. onClick={() => {
  239. setData({
  240. seriesData,
  241. tableData,
  242. pageLinks,
  243. totalIssuesCount,
  244. seriesResultsType,
  245. });
  246. openWidgetViewerPath(widget.id ?? index);
  247. }}
  248. />
  249. </ContextWrapper>
  250. )}
  251. </WidgetViewerContext.Consumer>
  252. );
  253. }
  254. export function getMenuOptions(
  255. organization: Organization,
  256. selection: PageFilters,
  257. widget: Widget,
  258. isMetricsData: boolean,
  259. widgetLimitReached: boolean,
  260. hasEditAccess: boolean = true,
  261. onDelete?: () => void,
  262. onDuplicate?: () => void,
  263. onEdit?: () => void
  264. ) {
  265. const menuOptions: MenuItemProps[] = [];
  266. if (
  267. organization.features.includes('discover-basic') &&
  268. widget.widgetType &&
  269. [WidgetType.DISCOVER, WidgetType.ERRORS, WidgetType.TRANSACTIONS].includes(
  270. widget.widgetType
  271. )
  272. ) {
  273. const optionDisabled =
  274. (hasDatasetSelector(organization) && widget.widgetType === WidgetType.DISCOVER) ||
  275. isUsingPerformanceScore(widget);
  276. // Open Widget in Discover
  277. if (widget.queries.length) {
  278. const discoverPath = getWidgetDiscoverUrl(
  279. widget,
  280. selection,
  281. organization,
  282. 0,
  283. isMetricsData
  284. );
  285. menuOptions.push({
  286. key: 'open-in-discover',
  287. label: t('Open in Discover'),
  288. to: optionDisabled
  289. ? undefined
  290. : widget.queries.length === 1
  291. ? discoverPath
  292. : undefined,
  293. tooltip: isUsingPerformanceScore(widget)
  294. ? performanceScoreTooltip
  295. : t(
  296. 'We are splitting datasets to make them easier to digest. Please confirm the dataset for this widget by clicking Edit Widget.'
  297. ),
  298. tooltipOptions: {disabled: !optionDisabled},
  299. disabled: optionDisabled,
  300. showDetailsInOverlay: true,
  301. onAction: () => {
  302. if (widget.queries.length === 1) {
  303. trackAnalytics('dashboards_views.open_in_discover.opened', {
  304. organization,
  305. widget_type: widget.displayType,
  306. });
  307. return;
  308. }
  309. trackAnalytics('dashboards_views.query_selector.opened', {
  310. organization,
  311. widget_type: widget.displayType,
  312. });
  313. openDashboardWidgetQuerySelectorModal({organization, widget, isMetricsData});
  314. },
  315. });
  316. }
  317. }
  318. if (widget.widgetType === WidgetType.SPANS) {
  319. menuOptions.push({
  320. key: 'open-in-explore',
  321. label: t('Open in Explore'),
  322. to: getWidgetExploreUrl(widget, selection, organization),
  323. });
  324. }
  325. if (widget.widgetType === WidgetType.ISSUE) {
  326. const issuesLocation = getWidgetIssueUrl(widget, selection, organization);
  327. menuOptions.push({
  328. key: 'open-in-issues',
  329. label: t('Open in Issues'),
  330. to: issuesLocation,
  331. });
  332. }
  333. if (widget.widgetType === WidgetType.METRICS) {
  334. const metricsLocation = getWidgetMetricsUrl(widget, selection, organization);
  335. menuOptions.push({
  336. key: 'open-in-metrics',
  337. label: t('Open in Metrics'),
  338. to: metricsLocation,
  339. });
  340. }
  341. if (organization.features.includes('dashboards-edit')) {
  342. menuOptions.push({
  343. key: 'duplicate-widget',
  344. label: t('Duplicate Widget'),
  345. onAction: () => onDuplicate?.(),
  346. disabled: widgetLimitReached || !hasEditAccess,
  347. });
  348. menuOptions.push({
  349. key: 'edit-widget',
  350. label: t('Edit Widget'),
  351. onAction: () => onEdit?.(),
  352. disabled: !hasEditAccess,
  353. });
  354. menuOptions.push({
  355. key: 'delete-widget',
  356. label: t('Delete Widget'),
  357. priority: 'danger',
  358. onAction: () => {
  359. openConfirmModal({
  360. message: t('Are you sure you want to delete this widget?'),
  361. priority: 'danger',
  362. onConfirm: () => onDelete?.(),
  363. });
  364. },
  365. disabled: !hasEditAccess,
  366. });
  367. }
  368. return menuOptions;
  369. }
  370. export default WidgetCardContextMenu;
  371. const ContextWrapper = styled('div')`
  372. display: flex;
  373. align-items: center;
  374. height: ${space(3)};
  375. margin-left: ${space(1)};
  376. gap: ${space(0.25)};
  377. `;
  378. const StyledDropdownMenuControl = styled(DropdownMenu)`
  379. display: flex;
  380. & > button {
  381. z-index: auto;
  382. }
  383. `;
  384. const SampledTag = styled(Tag)`
  385. margin-right: ${space(0.5)};
  386. `;
  387. const WidgetTooltipTitle = styled('div')`
  388. font-weight: bold;
  389. font-size: ${p => p.theme.fontSizeMedium};
  390. text-align: left;
  391. `;
  392. const WidgetTooltipDescription = styled('div')`
  393. margin-top: ${space(0.5)};
  394. font-size: ${p => p.theme.fontSizeSmall};
  395. text-align: left;
  396. `;
  397. // We're using a button here to preserve tab accessibility
  398. const WidgetTooltipButton = styled(Button)`
  399. pointer-events: none;
  400. `;