pageHeaderActions.tsx 6.2 KB

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