pageHeaderActions.tsx 5.1 KB

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