pageHeaderActions.tsx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import {useMemo} from 'react';
  2. import * as Sentry from '@sentry/react';
  3. import {navigateTo} from 'sentry/actionCreators/navigation';
  4. import {Button} from 'sentry/components/button';
  5. import ButtonBar from 'sentry/components/buttonBar';
  6. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  7. import {
  8. IconAdd,
  9. IconBookmark,
  10. IconDashboard,
  11. IconEllipsis,
  12. IconSettings,
  13. IconSiren,
  14. } from 'sentry/icons';
  15. import {t} from 'sentry/locale';
  16. import {trackAnalytics} from 'sentry/utils/analytics';
  17. import {isCustomMeasurement} from 'sentry/utils/metrics';
  18. import {MRIToField} from 'sentry/utils/metrics/mri';
  19. import {middleEllipsis} from 'sentry/utils/middleEllipsis';
  20. import useOrganization from 'sentry/utils/useOrganization';
  21. import usePageFilters from 'sentry/utils/usePageFilters';
  22. import useRouter from 'sentry/utils/useRouter';
  23. import {useDDMContext} from 'sentry/views/ddm/context';
  24. import {getCreateAlert} from 'sentry/views/ddm/contextMenu';
  25. import {QuerySymbol} from 'sentry/views/ddm/querySymbol';
  26. import {useCreateDashboard} from 'sentry/views/ddm/useCreateDashboard';
  27. interface Props {
  28. addCustomMetric: (referrer: string) => void;
  29. showCustomMetricButton: boolean;
  30. }
  31. export function PageHeaderActions({showCustomMetricButton, addCustomMetric}: Props) {
  32. const router = useRouter();
  33. const organization = useOrganization();
  34. const {selection} = usePageFilters();
  35. const createDashboard = useCreateDashboard();
  36. const {addWidget, isDefaultQuery, setDefaultQuery, widgets, showQuerySymbols} =
  37. useDDMContext();
  38. const hasEmptyWidget = widgets.length === 0 || widgets.some(widget => !widget.mri);
  39. const items = useMemo(
  40. () => [
  41. {
  42. leadingItems: [<IconAdd isCircled key="icon" />],
  43. key: 'add-query',
  44. label: t('Add Query'),
  45. disabled: hasEmptyWidget,
  46. onAction: () => {
  47. trackAnalytics('ddm.widget.add', {
  48. organization,
  49. });
  50. Sentry.metrics.increment('ddm.widget.add');
  51. addWidget();
  52. },
  53. },
  54. {
  55. leadingItems: [<IconDashboard key="icon" />],
  56. key: 'add-dashboard',
  57. label: t('Add to Dashboard'),
  58. onAction: createDashboard,
  59. },
  60. {
  61. leadingItems: [<IconSettings key="icon" />],
  62. key: 'metrics-settings',
  63. label: t('Metrics Settings'),
  64. onAction: () => navigateTo(`/settings/projects/:projectId/metrics/`, router),
  65. },
  66. ],
  67. [addWidget, createDashboard, hasEmptyWidget, organization, router]
  68. );
  69. const alertItems = useMemo(
  70. () =>
  71. widgets.map((widget, index) => {
  72. const createAlert = getCreateAlert(organization, {
  73. datetime: selection.datetime,
  74. projects: selection.projects,
  75. environments: selection.environments,
  76. query: widget.query,
  77. mri: widget.mri,
  78. groupBy: widget.groupBy,
  79. op: widget.op,
  80. });
  81. return {
  82. leadingItems: showQuerySymbols
  83. ? [<QuerySymbol key="icon" index={index} />]
  84. : [],
  85. key: `add-alert-${index}`,
  86. label: widget.mri
  87. ? middleEllipsis(MRIToField(widget.mri, widget.op!), 60, /\.|-|_/)
  88. : t('Select a metric to create an alert'),
  89. tooltip: isCustomMeasurement({mri: widget.mri})
  90. ? t('Custom measurements cannot be used to create alerts')
  91. : undefined,
  92. disabled: !createAlert,
  93. onAction: createAlert,
  94. };
  95. }),
  96. [
  97. organization,
  98. selection.datetime,
  99. selection.environments,
  100. selection.projects,
  101. showQuerySymbols,
  102. widgets,
  103. ]
  104. );
  105. return (
  106. <ButtonBar gap={1}>
  107. {showCustomMetricButton && (
  108. <Button priority="primary" onClick={() => addCustomMetric('header')} size="sm">
  109. {t('Add Custom Metric')}
  110. </Button>
  111. )}
  112. <Button
  113. size="sm"
  114. icon={<IconBookmark isSolid={isDefaultQuery} />}
  115. onClick={() => setDefaultQuery(isDefaultQuery ? null : router.location.query)}
  116. >
  117. {isDefaultQuery ? t('Remove Default') : t('Save as default')}
  118. </Button>
  119. {alertItems.length === 1 ? (
  120. <Button
  121. size="sm"
  122. icon={<IconSiren />}
  123. disabled={!alertItems[0].onAction}
  124. onClick={alertItems[0].onAction}
  125. >
  126. {t('Create Alert')}
  127. </Button>
  128. ) : (
  129. <DropdownMenu
  130. items={alertItems}
  131. triggerLabel={t('Create Alert')}
  132. triggerProps={{
  133. size: 'sm',
  134. showChevron: false,
  135. icon: <IconSiren direction="down" size="sm" />,
  136. }}
  137. position="bottom-end"
  138. />
  139. )}
  140. <DropdownMenu
  141. items={items}
  142. triggerProps={{
  143. 'aria-label': t('Page actions'),
  144. size: 'sm',
  145. showChevron: false,
  146. icon: <IconEllipsis direction="down" size="xs" />,
  147. }}
  148. position="bottom-end"
  149. />
  150. </ButtonBar>
  151. );
  152. }