widgetCardContextMenu.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import styled from '@emotion/styled';
  2. import * as qs from 'query-string';
  3. import {openDashboardWidgetQuerySelectorModal} from 'sentry/actionCreators/modal';
  4. import {parseArithmetic} from 'sentry/components/arithmeticInput/parser';
  5. import {openConfirmModal} from 'sentry/components/confirm';
  6. import DropdownMenuControlV2 from 'sentry/components/dropdownMenuControlV2';
  7. import {MenuItemProps} from 'sentry/components/dropdownMenuItemV2';
  8. import {
  9. IconCopy,
  10. IconDelete,
  11. IconEdit,
  12. IconEllipsis,
  13. IconIssues,
  14. IconTelescope,
  15. } from 'sentry/icons';
  16. import {t} from 'sentry/locale';
  17. import space from 'sentry/styles/space';
  18. import {Organization, PageFilters} from 'sentry/types';
  19. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  20. import {getUtcDateString} from 'sentry/utils/dates';
  21. import {isEquation, stripEquationPrefix} from 'sentry/utils/discover/fields';
  22. import {DisplayModes} from 'sentry/utils/discover/types';
  23. import {eventViewFromWidget} from 'sentry/views/dashboardsV2/utils';
  24. import {DisplayType} from 'sentry/views/dashboardsV2/widgetBuilder/utils';
  25. import {Widget, WidgetType} from '../types';
  26. type Props = {
  27. onDelete: () => void;
  28. onDuplicate: () => void;
  29. onEdit: () => void;
  30. organization: Organization;
  31. selection: PageFilters;
  32. widget: Widget;
  33. widgetLimitReached: boolean;
  34. isPreview?: boolean;
  35. showContextMenu?: boolean;
  36. };
  37. function WidgetCardContextMenu({
  38. organization,
  39. selection,
  40. widget,
  41. widgetLimitReached,
  42. onDelete,
  43. onDuplicate,
  44. onEdit,
  45. showContextMenu,
  46. isPreview,
  47. }: Props) {
  48. if (!showContextMenu) {
  49. return null;
  50. }
  51. const menuOptions: MenuItemProps[] = [];
  52. const disabledKeys: string[] = [];
  53. if (isPreview) {
  54. return (
  55. <ContextWrapper>
  56. <DropdownMenuControlV2
  57. items={[
  58. {
  59. key: 'preview',
  60. label: t('This is a preview only. To edit, you must add this dashboard.'),
  61. },
  62. ]}
  63. triggerProps={{
  64. 'aria-label': t('Widget actions'),
  65. size: 'xsmall',
  66. borderless: true,
  67. showChevron: false,
  68. icon: <IconEllipsis direction="down" size="sm" />,
  69. }}
  70. placement="bottom right"
  71. disabledKeys={['preview']}
  72. />
  73. </ContextWrapper>
  74. );
  75. }
  76. if (
  77. organization.features.includes('discover-basic') &&
  78. widget.widgetType === WidgetType.DISCOVER
  79. ) {
  80. // Open Widget in Discover
  81. if (widget.queries.length) {
  82. const eventView = eventViewFromWidget(
  83. widget.title,
  84. widget.queries[0],
  85. selection,
  86. widget.displayType
  87. );
  88. const discoverLocation = eventView.getResultsViewUrlTarget(organization.slug);
  89. // Pull a max of 3 valid Y-Axis from the widget
  90. const yAxisOptions = eventView.getYAxisOptions().map(({value}) => value);
  91. discoverLocation.query.yAxis = [
  92. ...new Set(
  93. widget.queries[0].fields.filter(field => yAxisOptions.includes(field))
  94. ),
  95. ].slice(0, 3);
  96. switch (widget.displayType) {
  97. case DisplayType.WORLD_MAP:
  98. discoverLocation.query.display = DisplayModes.WORLDMAP;
  99. break;
  100. case DisplayType.BAR:
  101. discoverLocation.query.display = DisplayModes.BAR;
  102. break;
  103. case DisplayType.TOP_N:
  104. discoverLocation.query.display = DisplayModes.TOP5;
  105. // Last field is used as the yAxis
  106. discoverLocation.query.yAxis =
  107. widget.queries[0].fields[widget.queries[0].fields.length - 1];
  108. discoverLocation.query.field = widget.queries[0].fields.slice(0, -1);
  109. break;
  110. default:
  111. break;
  112. }
  113. // Gather all fields and functions used in equations and prepend them to discover columns
  114. const termsSet: Set<string> = new Set();
  115. widget.queries[0].fields.forEach(field => {
  116. if (isEquation(field)) {
  117. const parsed = parseArithmetic(stripEquationPrefix(field)).tc;
  118. parsed.fields.forEach(({term}) => termsSet.add(term as string));
  119. parsed.functions.forEach(({term}) => termsSet.add(term as string));
  120. }
  121. });
  122. termsSet.forEach(term => {
  123. const fields = discoverLocation.query.field;
  124. if (Array.isArray(fields) && !fields.includes(term)) {
  125. fields.unshift(term);
  126. }
  127. });
  128. const discoverPath = `${discoverLocation.pathname}?${qs.stringify({
  129. ...discoverLocation.query,
  130. })}`;
  131. menuOptions.push({
  132. key: 'open-in-discover',
  133. label: t('Open in Discover'),
  134. leadingItems: <IconTelescope />,
  135. to: widget.queries.length === 1 ? discoverPath : undefined,
  136. onAction: () => {
  137. if (widget.queries.length === 1) {
  138. trackAdvancedAnalyticsEvent('dashboards_views.open_in_discover.opened', {
  139. organization,
  140. widget_type: widget.displayType,
  141. });
  142. return;
  143. }
  144. trackAdvancedAnalyticsEvent('dashboards_views.query_selector.opened', {
  145. organization,
  146. widget_type: widget.displayType,
  147. });
  148. openDashboardWidgetQuerySelectorModal({organization, widget});
  149. },
  150. });
  151. }
  152. }
  153. if (widget.widgetType === WidgetType.ISSUE) {
  154. const {start, end, utc, period} = selection.datetime;
  155. const datetime =
  156. start && end
  157. ? {start: getUtcDateString(start), end: getUtcDateString(end), utc}
  158. : {statsPeriod: period};
  159. const issuesLocation = `/organizations/${organization.slug}/issues/?${qs.stringify({
  160. query: widget.queries?.[0]?.conditions,
  161. sort: widget.queries?.[0]?.orderby,
  162. ...datetime,
  163. })}`;
  164. menuOptions.push({
  165. key: 'open-in-issues',
  166. label: t('Open in Issues'),
  167. leadingItems: <IconIssues />,
  168. to: issuesLocation,
  169. });
  170. }
  171. if (organization.features.includes('dashboards-edit')) {
  172. menuOptions.push({
  173. key: 'duplicate-widget',
  174. label: t('Duplicate Widget'),
  175. leadingItems: <IconCopy />,
  176. onAction: () => onDuplicate(),
  177. });
  178. widgetLimitReached && disabledKeys.push('duplicate-widget');
  179. menuOptions.push({
  180. key: 'edit-widget',
  181. label: t('Edit Widget'),
  182. leadingItems: <IconEdit />,
  183. onAction: () => onEdit(),
  184. });
  185. menuOptions.push({
  186. key: 'delete-widget',
  187. label: t('Delete Widget'),
  188. leadingItems: <IconDelete />,
  189. onAction: () => {
  190. openConfirmModal({
  191. message: t('Are you sure you want to delete this widget?'),
  192. priority: 'danger',
  193. onConfirm: () => onDelete(),
  194. });
  195. },
  196. });
  197. }
  198. if (!menuOptions.length) {
  199. return null;
  200. }
  201. return (
  202. <ContextWrapper>
  203. <DropdownMenuControlV2
  204. items={menuOptions}
  205. triggerProps={{
  206. 'aria-label': t('Widget actions'),
  207. size: 'xsmall',
  208. borderless: true,
  209. showChevron: false,
  210. icon: <IconEllipsis direction="down" size="sm" />,
  211. }}
  212. placement="bottom right"
  213. disabledKeys={disabledKeys}
  214. />
  215. </ContextWrapper>
  216. );
  217. }
  218. export default WidgetCardContextMenu;
  219. const ContextWrapper = styled('div')`
  220. display: flex;
  221. align-items: center;
  222. height: ${space(3)};
  223. margin-left: ${space(1)};
  224. `;