pageHeaderActions.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. showCustomMetricButton: boolean;
  34. }
  35. export function PageHeaderActions({showCustomMetricButton, 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. op: widget.op,
  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.op}(${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. {showCustomMetricButton &&
  139. (hasCustomMetricsExtractionRules(organization) ? (
  140. <Button
  141. priority="primary"
  142. onClick={() => navigateTo(`/settings/projects/:projectId/metrics/`, router)}
  143. size="sm"
  144. >
  145. {t('Add New Metric')}
  146. </Button>
  147. ) : (
  148. <Button priority="primary" onClick={() => addCustomMetric()} size="sm">
  149. {t('Add 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. <CreateMetricAlertFeature>
  160. {({hasFeature}) =>
  161. alertItems.length === 1 ? (
  162. <Button
  163. size="sm"
  164. icon={<IconSiren />}
  165. disabled={!alertItems[0].onAction || !hasFeature}
  166. onClick={alertItems[0].onAction}
  167. >
  168. {t('Create Alert')}
  169. </Button>
  170. ) : (
  171. <DropdownMenu
  172. items={alertItems}
  173. triggerLabel={t('Create Alert')}
  174. isDisabled={!hasFeature}
  175. triggerProps={{
  176. size: 'sm',
  177. showChevron: false,
  178. icon: <IconSiren direction="down" size="sm" />,
  179. }}
  180. position="bottom-end"
  181. />
  182. )
  183. }
  184. </CreateMetricAlertFeature>
  185. <DropdownMenu
  186. items={items}
  187. triggerProps={{
  188. 'aria-label': t('Page actions'),
  189. size: 'sm',
  190. showChevron: false,
  191. icon: <IconEllipsis direction="down" size="xs" />,
  192. }}
  193. position="bottom-end"
  194. />
  195. </ButtonBar>
  196. );
  197. }
  198. const AddToDashboardItem = styled('div')<{disabled: boolean}>`
  199. color: ${p => (p.disabled ? p.theme.disabled : p.theme.textColor)};
  200. `;