pageHeaderActions.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 {MetricQueryType, type MetricQueryWidgetParams} 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 {useDDMContext} from 'sentry/views/ddm/context';
  25. import {getCreateAlert} from 'sentry/views/ddm/metricQueryContextMenu';
  26. import {QuerySymbol} from 'sentry/views/ddm/querySymbol';
  27. import {useCreateDashboard} from 'sentry/views/ddm/useCreateDashboard';
  28. interface Props {
  29. addCustomMetric: () => void;
  30. showCustomMetricButton: boolean;
  31. }
  32. export function PageHeaderActions({showCustomMetricButton, addCustomMetric}: Props) {
  33. const router = useRouter();
  34. const organization = useOrganization();
  35. const createDashboard = useCreateDashboard();
  36. const {
  37. isDefaultQuery,
  38. setDefaultQuery,
  39. widgets,
  40. showQuerySymbols,
  41. selectedWidgetIndex,
  42. isMultiChartMode,
  43. } = useDDMContext();
  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 MetricQueryWidgetParams =>
  102. query.type === MetricQueryType.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. isSelected={index === selectedWidgetIndex && isMultiChartMode}
  119. />,
  120. ]
  121. : [],
  122. key: `add-alert-${index}`,
  123. label: widget.mri
  124. ? middleEllipsis(MRIToField(widget.mri, widget.op), 60, /\.|-|_/)
  125. : t('Select a metric to create an alert'),
  126. tooltip: isCustomMeasurement({mri: widget.mri})
  127. ? t('Custom measurements cannot be used to create alerts')
  128. : undefined,
  129. disabled: !createAlert,
  130. onAction: () => {
  131. trackAnalytics('ddm.create-alert', {
  132. organization,
  133. source: 'global',
  134. });
  135. createAlert?.();
  136. },
  137. };
  138. }),
  139. [isMultiChartMode, organization, selectedWidgetIndex, showQuerySymbols, widgets]
  140. );
  141. return (
  142. <ButtonBar gap={1}>
  143. {showCustomMetricButton && (
  144. <Button priority="primary" onClick={() => addCustomMetric()} size="sm">
  145. {t('Set Up Custom Metrics')}
  146. </Button>
  147. )}
  148. <Button
  149. size="sm"
  150. icon={<IconBookmark isSolid={isDefaultQuery} />}
  151. onClick={handleToggleDefaultQuery}
  152. >
  153. {isDefaultQuery ? t('Remove Default') : t('Save as default')}
  154. </Button>
  155. {alertItems.length === 1 ? (
  156. <Button
  157. size="sm"
  158. icon={<IconSiren />}
  159. disabled={!alertItems[0].onAction}
  160. onClick={alertItems[0].onAction}
  161. >
  162. {t('Create Alert')}
  163. </Button>
  164. ) : (
  165. <DropdownMenu
  166. items={alertItems}
  167. triggerLabel={t('Create Alert')}
  168. triggerProps={{
  169. size: 'sm',
  170. showChevron: false,
  171. icon: <IconSiren direction="down" size="sm" />,
  172. }}
  173. position="bottom-end"
  174. />
  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. `;