pageHeaderActions.tsx 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 {
  22. hasCustomMetricsExtractionRules,
  23. hasMetricsNewInputs,
  24. } from 'sentry/utils/metrics/features';
  25. import {formatMRI} from 'sentry/utils/metrics/mri';
  26. import {MetricExpressionType, type MetricsQueryWidget} from 'sentry/utils/metrics/types';
  27. import {middleEllipsis} from 'sentry/utils/string/middleEllipsis';
  28. import useOrganization from 'sentry/utils/useOrganization';
  29. import useRouter from 'sentry/utils/useRouter';
  30. import {useMetricsContext} from 'sentry/views/metrics/context';
  31. import {getCreateAlert} from 'sentry/views/metrics/metricQueryContextMenu';
  32. import {useCreateDashboard} from 'sentry/views/metrics/useCreateDashboard';
  33. import {useFormulaDependencies} from 'sentry/views/metrics/utils/useFormulaDependencies';
  34. import {openExtractionRuleCreateModal} from 'sentry/views/settings/projectMetrics/metricsExtractionRuleCreateModal';
  35. interface Props {
  36. addCustomMetric: () => void;
  37. showAddMetricButton: boolean;
  38. }
  39. export function PageHeaderActions({showAddMetricButton, addCustomMetric}: Props) {
  40. const router = useRouter();
  41. const organization = useOrganization();
  42. const metricsNewInputs = hasMetricsNewInputs(organization);
  43. const formulaDependencies = useFormulaDependencies();
  44. const {isDefaultQuery, setDefaultQuery, widgets, showQuerySymbols, isMultiChartMode} =
  45. useMetricsContext();
  46. const createDashboard = useCreateDashboard(
  47. widgets,
  48. formulaDependencies,
  49. isMultiChartMode
  50. );
  51. const handleToggleDefaultQuery = useCallback(() => {
  52. if (isDefaultQuery) {
  53. Sentry.metrics.increment('ddm.remove-default-query');
  54. trackAnalytics('ddm.remove-default-query', {
  55. organization,
  56. });
  57. setDefaultQuery(null);
  58. } else {
  59. Sentry.metrics.increment('ddm.set-default-query');
  60. trackAnalytics('ddm.set-default-query', {
  61. organization,
  62. });
  63. setDefaultQuery(router.location.query);
  64. }
  65. }, [isDefaultQuery, organization, router.location.query, setDefaultQuery]);
  66. const items = useMemo(
  67. () => [
  68. {
  69. leadingItems: [<IconDashboard key="icon" />],
  70. key: 'add-dashboard',
  71. label: (
  72. <Feature
  73. organization={organization}
  74. hookName="feature-disabled:dashboards-edit"
  75. features="dashboards-edit"
  76. >
  77. {({hasFeature}) => (
  78. <AddToDashboardItem disabled={!hasFeature}>
  79. {t('Add to Dashboard')}
  80. </AddToDashboardItem>
  81. )}
  82. </Feature>
  83. ),
  84. onAction: () => {
  85. if (!organization.features.includes('dashboards-edit')) {
  86. return;
  87. }
  88. trackAnalytics('ddm.add-to-dashboard', {
  89. organization,
  90. source: 'global',
  91. });
  92. createDashboard();
  93. },
  94. },
  95. {
  96. leadingItems: [<IconSettings key="icon" />],
  97. key: 'Metrics Settings',
  98. label: t('Metrics Settings'),
  99. onAction: () => navigateTo(`/settings/projects/:projectId/metrics/`, router),
  100. },
  101. ],
  102. [createDashboard, organization, router]
  103. );
  104. const alertItems = useMemo(
  105. () =>
  106. widgets
  107. .filter(
  108. (query): query is MetricsQueryWidget =>
  109. query.type === MetricExpressionType.QUERY
  110. )
  111. .map((widget, index) => {
  112. const createAlert = getCreateAlert(organization, widget);
  113. return {
  114. leadingItems: showQuerySymbols
  115. ? [<span key="symbol">{getQuerySymbol(widget.id, metricsNewInputs)}:</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. [widgets, showQuerySymbols, metricsNewInputs, organization]
  135. );
  136. return (
  137. <ButtonBar gap={1}>
  138. {showAddMetricButton &&
  139. (hasCustomMetricsExtractionRules(organization) ? (
  140. <Button
  141. priority="primary"
  142. onClick={() =>
  143. openExtractionRuleCreateModal({
  144. organization,
  145. source: 'ddm.header.create-metric',
  146. })
  147. }
  148. size="sm"
  149. >
  150. {t('Create 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. `;