pageHeaderActions.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import {useCallback, 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: () => 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 {
  37. addWidget,
  38. isDefaultQuery,
  39. setDefaultQuery,
  40. widgets,
  41. showQuerySymbols,
  42. selectedWidgetIndex,
  43. } = useDDMContext();
  44. const hasEmptyWidget = widgets.length === 0 || widgets.some(widget => !widget.mri);
  45. const handleToggleDefaultQuery = useCallback(() => {
  46. if (isDefaultQuery) {
  47. Sentry.metrics.increment('ddm.remove-default-query');
  48. trackAnalytics('ddm.remove-default-query', {
  49. organization,
  50. });
  51. setDefaultQuery(null);
  52. } else {
  53. Sentry.metrics.increment('ddm.set-default-query');
  54. trackAnalytics('ddm.set-default-query', {
  55. organization,
  56. });
  57. setDefaultQuery(router.location.query);
  58. }
  59. }, [isDefaultQuery, organization, router.location.query, setDefaultQuery]);
  60. const items = useMemo(
  61. () => [
  62. {
  63. leadingItems: [<IconAdd isCircled key="icon" />],
  64. key: 'add-query',
  65. label: t('Add Query'),
  66. disabled: hasEmptyWidget,
  67. onAction: () => {
  68. trackAnalytics('ddm.widget.add', {
  69. organization,
  70. });
  71. Sentry.metrics.increment('ddm.widget.add');
  72. addWidget();
  73. },
  74. },
  75. {
  76. leadingItems: [<IconDashboard key="icon" />],
  77. key: 'add-dashboard',
  78. label: t('Add to Dashboard'),
  79. onAction: () => {
  80. trackAnalytics('ddm.add-to-dashboard', {
  81. organization,
  82. source: 'global',
  83. });
  84. createDashboard();
  85. },
  86. },
  87. {
  88. leadingItems: [<IconSettings key="icon" />],
  89. key: 'metrics-settings',
  90. label: t('Metrics Settings'),
  91. onAction: () => navigateTo(`/settings/projects/:projectId/metrics/`, router),
  92. },
  93. ],
  94. [addWidget, createDashboard, hasEmptyWidget, organization, router]
  95. );
  96. const alertItems = useMemo(
  97. () =>
  98. widgets.map((widget, index) => {
  99. const createAlert = getCreateAlert(organization, {
  100. datetime: selection.datetime,
  101. projects: selection.projects,
  102. environments: selection.environments,
  103. query: widget.query,
  104. mri: widget.mri,
  105. groupBy: widget.groupBy,
  106. op: widget.op,
  107. });
  108. return {
  109. leadingItems: showQuerySymbols
  110. ? [
  111. <QuerySymbol
  112. key="icon"
  113. index={index}
  114. isSelected={index === selectedWidgetIndex}
  115. />,
  116. ]
  117. : [],
  118. key: `add-alert-${index}`,
  119. label: widget.mri
  120. ? middleEllipsis(MRIToField(widget.mri, widget.op!), 60, /\.|-|_/)
  121. : t('Select a metric to create an alert'),
  122. tooltip: isCustomMeasurement({mri: widget.mri})
  123. ? t('Custom measurements cannot be used to create alerts')
  124. : undefined,
  125. disabled: !createAlert,
  126. onAction: () => {
  127. trackAnalytics('ddm.create-alert', {
  128. organization,
  129. source: 'global',
  130. });
  131. createAlert?.();
  132. },
  133. };
  134. }),
  135. [
  136. organization,
  137. selectedWidgetIndex,
  138. selection.datetime,
  139. selection.environments,
  140. selection.projects,
  141. showQuerySymbols,
  142. widgets,
  143. ]
  144. );
  145. return (
  146. <ButtonBar gap={1}>
  147. {showCustomMetricButton && (
  148. <Button priority="primary" onClick={() => addCustomMetric()} size="sm">
  149. {t('Set Up Custom Metrics')}
  150. </Button>
  151. )}
  152. <Button
  153. size="sm"
  154. icon={<IconBookmark isSolid={isDefaultQuery} />}
  155. onClick={handleToggleDefaultQuery}
  156. >
  157. {isDefaultQuery ? t('Remove Default') : t('Save as default')}
  158. </Button>
  159. {alertItems.length === 1 ? (
  160. <Button
  161. size="sm"
  162. icon={<IconSiren />}
  163. disabled={!alertItems[0].onAction}
  164. onClick={alertItems[0].onAction}
  165. >
  166. {t('Create Alert')}
  167. </Button>
  168. ) : (
  169. <DropdownMenu
  170. items={alertItems}
  171. triggerLabel={t('Create Alert')}
  172. triggerProps={{
  173. size: 'sm',
  174. showChevron: false,
  175. icon: <IconSiren direction="down" size="sm" />,
  176. }}
  177. position="bottom-end"
  178. />
  179. )}
  180. <DropdownMenu
  181. items={items}
  182. triggerProps={{
  183. 'aria-label': t('Page actions'),
  184. size: 'sm',
  185. showChevron: false,
  186. icon: <IconEllipsis direction="down" size="xs" />,
  187. }}
  188. position="bottom-end"
  189. />
  190. </ButtonBar>
  191. );
  192. }