pageHeaderActions.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 {hasCustomMetrics, hasMetricsNewInputs} 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 metricsNewInputs = hasMetricsNewInputs(organization);
  39. const formulaDependencies = useFormulaDependencies();
  40. const {isDefaultQuery, setDefaultQuery, widgets, showQuerySymbols, isMultiChartMode} =
  41. useMetricsContext();
  42. const createDashboard = useCreateDashboard(
  43. widgets,
  44. formulaDependencies,
  45. isMultiChartMode
  46. );
  47. const handleToggleDefaultQuery = useCallback(() => {
  48. if (isDefaultQuery) {
  49. Sentry.metrics.increment('ddm.remove-default-query');
  50. trackAnalytics('ddm.remove-default-query', {
  51. organization,
  52. });
  53. setDefaultQuery(null);
  54. } else {
  55. Sentry.metrics.increment('ddm.set-default-query');
  56. trackAnalytics('ddm.set-default-query', {
  57. organization,
  58. });
  59. setDefaultQuery(router.location.query);
  60. }
  61. }, [isDefaultQuery, organization, router.location.query, setDefaultQuery]);
  62. const items = useMemo(() => {
  63. const createDashboardItem = {
  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. const settingsItem = {
  91. leadingItems: [<IconSettings key="icon" />],
  92. key: 'Metrics Settings',
  93. label: t('Metrics Settings'),
  94. onAction: () =>
  95. navigateTo(`/settings/${organization.slug}/projects/:projectId/metrics/`, router),
  96. };
  97. if (hasCustomMetrics(organization)) {
  98. return [createDashboardItem, settingsItem];
  99. }
  100. return [settingsItem];
  101. }, [createDashboard, organization, router]);
  102. const alertItems = useMemo(
  103. () =>
  104. widgets
  105. .filter(
  106. (query): query is MetricsQueryWidget =>
  107. query.type === MetricExpressionType.QUERY
  108. )
  109. .map((widget, index) => {
  110. const createAlert = getCreateAlert(organization, widget);
  111. return {
  112. leadingItems: showQuerySymbols
  113. ? [<span key="symbol">{getQuerySymbol(widget.id, metricsNewInputs)}:</span>]
  114. : [],
  115. key: `add-alert-${index}`,
  116. label: widget.mri
  117. ? `${widget.aggregation}(${middleEllipsis(formatMRI(widget.mri), 60, /\.|-|_/)})`
  118. : t('Select a metric to create an alert'),
  119. tooltip: isCustomMeasurement({mri: widget.mri})
  120. ? t('Custom measurements cannot be used to create alerts')
  121. : undefined,
  122. disabled: !createAlert,
  123. onAction: () => {
  124. trackAnalytics('ddm.create-alert', {
  125. organization,
  126. source: 'global',
  127. });
  128. createAlert?.();
  129. },
  130. };
  131. }),
  132. [widgets, showQuerySymbols, metricsNewInputs, organization]
  133. );
  134. return (
  135. <ButtonBar gap={1}>
  136. {showAddMetricButton && hasCustomMetrics(organization) && (
  137. <Button priority="primary" onClick={() => addCustomMetric()} size="sm">
  138. {t('Add Custom Metrics')}
  139. </Button>
  140. )}
  141. <Button
  142. size="sm"
  143. icon={<IconBookmark isSolid={isDefaultQuery} />}
  144. onClick={handleToggleDefaultQuery}
  145. >
  146. {isDefaultQuery ? t('Remove Default') : t('Save as default')}
  147. </Button>
  148. {hasCustomMetrics(organization) && (
  149. <CreateMetricAlertFeature>
  150. {({hasFeature}) =>
  151. alertItems.length === 1 ? (
  152. <Button
  153. size="sm"
  154. icon={<IconSiren />}
  155. disabled={!alertItems[0].onAction || !hasFeature}
  156. onClick={alertItems[0].onAction}
  157. >
  158. {t('Create Alert')}
  159. </Button>
  160. ) : (
  161. <DropdownMenu
  162. items={alertItems}
  163. triggerLabel={t('Create Alert')}
  164. isDisabled={!hasFeature}
  165. triggerProps={{
  166. size: 'sm',
  167. showChevron: false,
  168. icon: <IconSiren direction="down" size="sm" />,
  169. }}
  170. position="bottom-end"
  171. />
  172. )
  173. }
  174. </CreateMetricAlertFeature>
  175. )}
  176. <DropdownMenu
  177. items={items}
  178. triggerProps={{
  179. 'aria-label': t('Page actions'),
  180. size: 'sm',
  181. showChevron: false,
  182. icon: <IconEllipsis direction="down" size="xs" />,
  183. }}
  184. position="bottom-end"
  185. />
  186. </ButtonBar>
  187. );
  188. }
  189. const AddToDashboardItem = styled('div')<{disabled: boolean}>`
  190. color: ${p => (p.disabled ? p.theme.disabled : p.theme.textColor)};
  191. `;