contextMenu.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {InjectedRouter} from 'react-router';
  2. import styled from '@emotion/styled';
  3. import {openAddToDashboardModal} from 'sentry/actionCreators/modal';
  4. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  5. import {IconEllipsis} from 'sentry/icons';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {Organization} from 'sentry/types';
  9. import {MetricDisplayType, MetricsQuery} from 'sentry/utils/metrics';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import useRouter from 'sentry/utils/useRouter';
  12. import {DashboardWidgetSource} from 'sentry/views/dashboards/types';
  13. type ContextMenuProps = {
  14. displayType: MetricDisplayType;
  15. metricsQuery: MetricsQuery;
  16. };
  17. export function MetricWidgetContextMenu({metricsQuery, displayType}: ContextMenuProps) {
  18. const organization = useOrganization();
  19. const router = useRouter();
  20. if (!organization.features.includes('ddm-experimental')) {
  21. return null;
  22. }
  23. return (
  24. <StyledDropdownMenuControl
  25. items={[
  26. {
  27. key: 'add-alert',
  28. label: t('Create Alert'),
  29. disabled: true,
  30. },
  31. {
  32. key: 'add-dashoard',
  33. label: t('Add to Dashboard'),
  34. onAction: () => {
  35. handleAddQueryToDashboard(metricsQuery, organization, router, displayType);
  36. },
  37. },
  38. ]}
  39. triggerProps={{
  40. 'aria-label': t('Widget actions'),
  41. size: 'xs',
  42. borderless: true,
  43. showChevron: false,
  44. icon: <IconEllipsis direction="down" size="sm" />,
  45. }}
  46. position="bottom-end"
  47. />
  48. );
  49. }
  50. function handleAddQueryToDashboard(
  51. {projects, environments, datetime, op, mri, groupBy, query}: MetricsQuery,
  52. organization: Organization,
  53. router: InjectedRouter,
  54. displayType?: MetricDisplayType
  55. ) {
  56. const {start, end, period} = datetime;
  57. // TODO(ddm): make a util that does this
  58. const field = op ? `${op}(${mri})` : mri;
  59. const widgetAsQueryParams = {
  60. ...router.location?.query,
  61. source: DashboardWidgetSource.DDM,
  62. start,
  63. end,
  64. statsPeriod: period,
  65. defaultWidgetQuery: field,
  66. defaultTableColumns: [],
  67. defaultTitle: 'DDM Widget',
  68. displayType,
  69. };
  70. const limit = !groupBy?.length ? 1 : 10;
  71. openAddToDashboardModal({
  72. organization,
  73. selection: {
  74. projects,
  75. environments,
  76. datetime,
  77. },
  78. widget: {
  79. title: 'DDM Widget',
  80. displayType,
  81. widgetType: 'custom-metrics',
  82. limit,
  83. queries: [
  84. {
  85. name: '',
  86. aggregates: [field],
  87. columns: groupBy ?? [],
  88. fields: [field],
  89. conditions: query,
  90. },
  91. ],
  92. },
  93. router,
  94. widgetAsQueryParams,
  95. location: router.location,
  96. });
  97. }
  98. const StyledDropdownMenuControl = styled(DropdownMenu)`
  99. margin: ${space(1)};
  100. `;