pageHeaderActions.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import {useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import * as Sentry from '@sentry/react';
  4. import {navigateTo} from 'sentry/actionCreators/navigation';
  5. import Feature from 'sentry/components/acl/feature';
  6. import {Button} from 'sentry/components/button';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import {DropdownMenu} from 'sentry/components/dropdownMenu';
  9. import {CreateMetricAlertFeature} from 'sentry/components/metrics/createMetricAlertFeature';
  10. import {getQuerySymbol} from 'sentry/components/metrics/querySymbol';
  11. import {
  12. IconBookmark,
  13. IconDashboard,
  14. IconEllipsis,
  15. IconSettings,
  16. IconSiren,
  17. } from 'sentry/icons';
  18. import {t} from 'sentry/locale';
  19. import {trackAnalytics} from 'sentry/utils/analytics';
  20. import {isCustomMeasurement} from 'sentry/utils/metrics';
  21. import {hasCustomMetricsExtractionRules} from 'sentry/utils/metrics/features';
  22. import {formatMRI} from 'sentry/utils/metrics/mri';
  23. import {MetricExpressionType, type MetricsQueryWidget} from 'sentry/utils/metrics/types';
  24. import {middleEllipsis} from 'sentry/utils/string/middleEllipsis';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import useRouter from 'sentry/utils/useRouter';
  27. import {useMetricsContext} from 'sentry/views/metrics/context';
  28. import {getCreateAlert} from 'sentry/views/metrics/metricQueryContextMenu';
  29. import {useCreateDashboard} from 'sentry/views/metrics/useCreateDashboard';
  30. import {useFormulaDependencies} from 'sentry/views/metrics/utils/useFormulaDependencies';
  31. interface Props {
  32. addCustomMetric: () => void;
  33. showAddMetricButton: boolean;
  34. }
  35. export function PageHeaderActions({showAddMetricButton, addCustomMetric}: Props) {
  36. const router = useRouter();
  37. const organization = useOrganization();
  38. const formulaDependencies = useFormulaDependencies();
  39. const {isDefaultQuery, setDefaultQuery, widgets, showQuerySymbols, isMultiChartMode} =
  40. useMetricsContext();
  41. const createDashboard = useCreateDashboard(
  42. widgets,
  43. formulaDependencies,
  44. isMultiChartMode
  45. );
  46. const handleToggleDefaultQuery = useCallback(() => {
  47. if (isDefaultQuery) {
  48. Sentry.metrics.increment('ddm.remove-default-query');
  49. trackAnalytics('ddm.remove-default-query', {
  50. organization,
  51. });
  52. setDefaultQuery(null);
  53. } else {
  54. Sentry.metrics.increment('ddm.set-default-query');
  55. trackAnalytics('ddm.set-default-query', {
  56. organization,
  57. });
  58. setDefaultQuery(router.location.query);
  59. }
  60. }, [isDefaultQuery, organization, router.location.query, setDefaultQuery]);
  61. const items = useMemo(
  62. () => [
  63. {
  64. leadingItems: [<IconDashboard key="icon" />],
  65. key: 'add-dashboard',
  66. label: (
  67. <Feature
  68. organization={organization}
  69. hookName="feature-disabled:dashboards-edit"
  70. features="dashboards-edit"
  71. >
  72. {({hasFeature}) => (
  73. <AddToDashboardItem disabled={!hasFeature}>
  74. {t('Add to Dashboard')}
  75. </AddToDashboardItem>
  76. )}
  77. </Feature>
  78. ),
  79. onAction: () => {
  80. if (!organization.features.includes('dashboards-edit')) {
  81. return;
  82. }
  83. trackAnalytics('ddm.add-to-dashboard', {
  84. organization,
  85. source: 'global',
  86. });
  87. createDashboard();
  88. },
  89. },
  90. {
  91. leadingItems: [<IconSettings key="icon" />],
  92. key: 'metrics-settings',
  93. label: t('Metrics Settings'),
  94. onAction: () => navigateTo(`/settings/projects/:projectId/metrics/`, router),
  95. },
  96. ],
  97. [createDashboard, organization, router]
  98. );
  99. const alertItems = useMemo(
  100. () =>
  101. widgets
  102. .filter(
  103. (query): query is MetricsQueryWidget =>
  104. query.type === MetricExpressionType.QUERY
  105. )
  106. .map((widget, index) => {
  107. const createAlert = getCreateAlert(organization, {
  108. query: widget.query,
  109. mri: widget.mri,
  110. groupBy: widget.groupBy,
  111. aggregation: widget.aggregation,
  112. });
  113. return {
  114. leadingItems: showQuerySymbols
  115. ? [<span key="symbol">{getQuerySymbol(widget.id)}:</span>]
  116. : [],
  117. key: `add-alert-${index}`,
  118. label: widget.mri
  119. ? `${widget.aggregation}(${middleEllipsis(formatMRI(widget.mri), 60, /\.|-|_/)})`
  120. : t('Select a metric to create an alert'),
  121. tooltip: isCustomMeasurement({mri: widget.mri})
  122. ? t('Custom measurements cannot be used to create alerts')
  123. : undefined,
  124. disabled: !createAlert,
  125. onAction: () => {
  126. trackAnalytics('ddm.create-alert', {
  127. organization,
  128. source: 'global',
  129. });
  130. createAlert?.();
  131. },
  132. };
  133. }),
  134. [organization, showQuerySymbols, widgets]
  135. );
  136. return (
  137. <ButtonBar gap={1}>
  138. {showAddMetricButton &&
  139. (hasCustomMetricsExtractionRules(organization) ? (
  140. <Button
  141. priority="primary"
  142. onClick={() =>
  143. navigateTo(
  144. `/settings/projects/:projectId/metrics/configure-metric/`,
  145. router
  146. )
  147. }
  148. size="sm"
  149. >
  150. {t('Add New Metric')}
  151. </Button>
  152. ) : (
  153. <Button priority="primary" onClick={() => addCustomMetric()} size="sm">
  154. {t('Add Custom Metrics')}
  155. </Button>
  156. ))}
  157. <Button
  158. size="sm"
  159. icon={<IconBookmark isSolid={isDefaultQuery} />}
  160. onClick={handleToggleDefaultQuery}
  161. >
  162. {isDefaultQuery ? t('Remove Default') : t('Save as default')}
  163. </Button>
  164. <CreateMetricAlertFeature>
  165. {({hasFeature}) =>
  166. alertItems.length === 1 ? (
  167. <Button
  168. size="sm"
  169. icon={<IconSiren />}
  170. disabled={!alertItems[0].onAction || !hasFeature}
  171. onClick={alertItems[0].onAction}
  172. >
  173. {t('Create Alert')}
  174. </Button>
  175. ) : (
  176. <DropdownMenu
  177. items={alertItems}
  178. triggerLabel={t('Create Alert')}
  179. isDisabled={!hasFeature}
  180. triggerProps={{
  181. size: 'sm',
  182. showChevron: false,
  183. icon: <IconSiren direction="down" size="sm" />,
  184. }}
  185. position="bottom-end"
  186. />
  187. )
  188. }
  189. </CreateMetricAlertFeature>
  190. <DropdownMenu
  191. items={items}
  192. triggerProps={{
  193. 'aria-label': t('Page actions'),
  194. size: 'sm',
  195. showChevron: false,
  196. icon: <IconEllipsis direction="down" size="xs" />,
  197. }}
  198. position="bottom-end"
  199. />
  200. </ButtonBar>
  201. );
  202. }
  203. const AddToDashboardItem = styled('div')<{disabled: boolean}>`
  204. color: ${p => (p.disabled ? p.theme.disabled : p.theme.textColor)};
  205. `;