widgetCardContextMenu.tsx 8.5 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 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 {IconEllipsis, IconExpand} from 'sentry/icons';
  13. import {t} from 'sentry/locale';
  14. import space from 'sentry/styles/space';
  15. import {Organization, PageFilters} from 'sentry/types';
  16. import {Series} from 'sentry/types/echarts';
  17. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  18. import {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  19. import {AggregationOutputType} from 'sentry/utils/discover/fields';
  20. import {getWidgetDiscoverUrl, getWidgetIssueUrl} from 'sentry/views/dashboardsV2/utils';
  21. import {Widget, WidgetType} from '../types';
  22. import {WidgetViewerContext} from '../widgetViewer/widgetViewerContext';
  23. import {useDashboardsMEPContext} from './dashboardsMEPContext';
  24. type Props = {
  25. location: Location;
  26. organization: Organization;
  27. router: InjectedRouter;
  28. selection: PageFilters;
  29. widget: Widget;
  30. widgetLimitReached: boolean;
  31. index?: string;
  32. isPreview?: boolean;
  33. onDelete?: () => void;
  34. onDuplicate?: () => void;
  35. onEdit?: () => void;
  36. pageLinks?: string;
  37. seriesData?: Series[];
  38. seriesResultsType?: Record<string, AggregationOutputType>;
  39. showContextMenu?: boolean;
  40. showWidgetViewerButton?: boolean;
  41. tableData?: TableDataWithTitle[];
  42. totalIssuesCount?: string;
  43. };
  44. function WidgetCardContextMenu({
  45. organization,
  46. selection,
  47. widget,
  48. widgetLimitReached,
  49. onDelete,
  50. onDuplicate,
  51. onEdit,
  52. showContextMenu,
  53. isPreview,
  54. showWidgetViewerButton,
  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. <ContextWrapper>
  85. <Feature organization={organization} features={['dashboards-mep']}>
  86. {isMetricsData === false && (
  87. <SampledTag
  88. tooltipText={t('This widget is only applicable to indexed events.')}
  89. >
  90. {t('Indexed')}
  91. </SampledTag>
  92. )}
  93. </Feature>
  94. <StyledDropdownMenuControl
  95. items={[
  96. {
  97. key: 'preview',
  98. label: t(
  99. 'This is a preview only. To edit, you must add this dashboard.'
  100. ),
  101. },
  102. ]}
  103. triggerProps={{
  104. 'aria-label': t('Widget actions'),
  105. size: 'xs',
  106. borderless: true,
  107. showChevron: false,
  108. icon: <IconEllipsis direction="down" size="sm" />,
  109. }}
  110. placement="bottom right"
  111. disabledKeys={[...disabledKeys, 'preview']}
  112. />
  113. {showWidgetViewerButton && (
  114. <OpenWidgetViewerButton
  115. aria-label={t('Open Widget Viewer')}
  116. priority="link"
  117. size="zero"
  118. icon={<IconExpand size="xs" />}
  119. onClick={() => {
  120. (seriesData || tableData) &&
  121. setData({
  122. seriesData,
  123. tableData,
  124. pageLinks,
  125. totalIssuesCount,
  126. seriesResultsType,
  127. });
  128. openWidgetViewerPath(index);
  129. }}
  130. />
  131. )}
  132. </ContextWrapper>
  133. )}
  134. </WidgetViewerContext.Consumer>
  135. );
  136. }
  137. if (
  138. organization.features.includes('discover-basic') &&
  139. widget.widgetType === WidgetType.DISCOVER
  140. ) {
  141. // Open Widget in Discover
  142. if (widget.queries.length) {
  143. const discoverPath = getWidgetDiscoverUrl(
  144. widget,
  145. selection,
  146. organization,
  147. 0,
  148. isMetricsData
  149. );
  150. menuOptions.push({
  151. key: 'open-in-discover',
  152. label: t('Open in Discover'),
  153. to: widget.queries.length === 1 ? discoverPath : undefined,
  154. onAction: () => {
  155. if (widget.queries.length === 1) {
  156. trackAdvancedAnalyticsEvent('dashboards_views.open_in_discover.opened', {
  157. organization,
  158. widget_type: widget.displayType,
  159. });
  160. return;
  161. }
  162. trackAdvancedAnalyticsEvent('dashboards_views.query_selector.opened', {
  163. organization,
  164. widget_type: widget.displayType,
  165. });
  166. openDashboardWidgetQuerySelectorModal({organization, widget, isMetricsData});
  167. },
  168. });
  169. }
  170. }
  171. if (widget.widgetType === WidgetType.ISSUE) {
  172. const issuesLocation = getWidgetIssueUrl(widget, selection, organization);
  173. menuOptions.push({
  174. key: 'open-in-issues',
  175. label: t('Open in Issues'),
  176. to: issuesLocation,
  177. });
  178. }
  179. if (organization.features.includes('dashboards-edit')) {
  180. menuOptions.push({
  181. key: 'duplicate-widget',
  182. label: t('Duplicate Widget'),
  183. onAction: () => onDuplicate?.(),
  184. });
  185. widgetLimitReached && disabledKeys.push('duplicate-widget');
  186. menuOptions.push({
  187. key: 'edit-widget',
  188. label: t('Edit Widget'),
  189. onAction: () => onEdit?.(),
  190. });
  191. menuOptions.push({
  192. key: 'delete-widget',
  193. label: t('Delete Widget'),
  194. priority: 'danger',
  195. onAction: () => {
  196. openConfirmModal({
  197. message: t('Are you sure you want to delete this widget?'),
  198. priority: 'danger',
  199. onConfirm: () => onDelete?.(),
  200. });
  201. },
  202. });
  203. }
  204. if (!menuOptions.length) {
  205. return null;
  206. }
  207. return (
  208. <WidgetViewerContext.Consumer>
  209. {({setData}) => (
  210. <ContextWrapper>
  211. <Feature organization={organization} features={['dashboards-mep']}>
  212. {isMetricsData === false && (
  213. <SampledTag
  214. tooltipText={t('This widget is only applicable to indexed events.')}
  215. >
  216. {t('Indexed')}
  217. </SampledTag>
  218. )}
  219. </Feature>
  220. <StyledDropdownMenuControl
  221. items={menuOptions}
  222. triggerProps={{
  223. 'aria-label': t('Widget actions'),
  224. size: 'xs',
  225. borderless: true,
  226. showChevron: false,
  227. icon: <IconEllipsis direction="down" size="sm" />,
  228. }}
  229. placement="bottom right"
  230. disabledKeys={[...disabledKeys]}
  231. />
  232. {showWidgetViewerButton && (
  233. <OpenWidgetViewerButton
  234. aria-label={t('Open Widget Viewer')}
  235. priority="link"
  236. size="zero"
  237. icon={<IconExpand size="xs" />}
  238. onClick={() => {
  239. setData({
  240. seriesData,
  241. tableData,
  242. pageLinks,
  243. totalIssuesCount,
  244. seriesResultsType,
  245. });
  246. openWidgetViewerPath(widget.id ?? index);
  247. }}
  248. />
  249. )}
  250. </ContextWrapper>
  251. )}
  252. </WidgetViewerContext.Consumer>
  253. );
  254. }
  255. export default WidgetCardContextMenu;
  256. const ContextWrapper = styled('div')`
  257. display: flex;
  258. align-items: center;
  259. height: ${space(3)};
  260. margin-left: ${space(1)};
  261. `;
  262. const StyledDropdownMenuControl = styled(DropdownMenuControl)`
  263. & > button {
  264. z-index: auto;
  265. }
  266. `;
  267. const OpenWidgetViewerButton = styled(Button)`
  268. padding: ${space(0.75)} ${space(1)};
  269. color: ${p => p.theme.textColor};
  270. &:hover {
  271. color: ${p => p.theme.textColor};
  272. background: ${p => p.theme.surface400};
  273. border-color: transparent;
  274. }
  275. `;
  276. const SampledTag = styled(Tag)`
  277. margin-right: ${space(0.5)};
  278. `;