layout.tsx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {Fragment, memo, useCallback} from 'react';
  2. import {useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import * as Sentry from '@sentry/react';
  5. import emptyStateImg from 'sentry-images/spot/custom-metrics-empty-state.svg';
  6. import Alert from 'sentry/components/alert';
  7. import GuideAnchor from 'sentry/components/assistant/guideAnchor';
  8. import FeatureBadge from 'sentry/components/badge/featureBadge';
  9. import Banner from 'sentry/components/banner';
  10. import {Button} from 'sentry/components/button';
  11. import ButtonBar from 'sentry/components/buttonBar';
  12. import FloatingFeedbackWidget from 'sentry/components/feedback/widget/floatingFeedbackWidget';
  13. import * as Layout from 'sentry/components/layouts/thirds';
  14. import LoadingIndicator from 'sentry/components/loadingIndicator';
  15. import OnboardingPanel from 'sentry/components/onboardingPanel';
  16. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  17. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  18. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  19. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  20. import {PageHeadingQuestionTooltip} from 'sentry/components/pageHeadingQuestionTooltip';
  21. import {t} from 'sentry/locale';
  22. import {space} from 'sentry/styles/space';
  23. import {trackAnalytics} from 'sentry/utils/analytics';
  24. import {METRICS_DOCS_URL} from 'sentry/utils/metrics/constants';
  25. import {canSeeMetricsPage} from 'sentry/utils/metrics/features';
  26. import useDismissAlert from 'sentry/utils/useDismissAlert';
  27. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  28. import useMedia from 'sentry/utils/useMedia';
  29. import useOrganization from 'sentry/utils/useOrganization';
  30. import usePageFilters from 'sentry/utils/usePageFilters';
  31. import BackgroundSpace from 'sentry/views/discover/backgroundSpace';
  32. import {
  33. MetricsOnboardingPanelPrimaryAction,
  34. MetricsSubscriptionAlert,
  35. } from 'sentry/views/metrics/billing';
  36. import {useMetricsContext} from 'sentry/views/metrics/context';
  37. import {useMetricsOnboardingSidebar} from 'sentry/views/metrics/ddmOnboarding/useMetricsOnboardingSidebar';
  38. import {IntervalSelect} from 'sentry/views/metrics/intervalSelect';
  39. import {PageHeaderActions} from 'sentry/views/metrics/pageHeaderActions';
  40. import {Queries} from 'sentry/views/metrics/queries';
  41. import {MetricScratchpad} from 'sentry/views/metrics/scratchpad';
  42. import {WidgetDetails} from 'sentry/views/metrics/widgetDetails';
  43. export const MetricsLayout = memo(() => {
  44. const organization = useOrganization();
  45. const pageFilters = usePageFilters();
  46. const selectedProjects = pageFilters.selection.projects.join();
  47. const {hasCustomMetrics, hasPerformanceMetrics, isHasMetricsLoading} =
  48. useMetricsContext();
  49. const {activateSidebar} = useMetricsOnboardingSidebar();
  50. const {dismiss: emptyStateDismiss, isDismissed: isEmptyStateDismissed} =
  51. useDismissAlert({
  52. key: `${organization.id}:${selectedProjects}:metrics-empty-state-dismissed`,
  53. });
  54. const theme = useTheme();
  55. const isSmallBanner = useMedia(`(max-width: ${theme.breakpoints.medium})`);
  56. const [isBannerDismissed] = useLocalStorageState('metrics-banner-dismissed', false);
  57. const addCustomMetric = useCallback(
  58. (referrer: 'header' | 'onboarding_panel' | 'banner') => {
  59. Sentry.metrics.increment('ddm.add_custom_metric', 1, {
  60. tags: {
  61. referrer,
  62. },
  63. });
  64. trackAnalytics('ddm.open-onboarding', {
  65. organization,
  66. source: referrer,
  67. });
  68. activateSidebar();
  69. },
  70. [activateSidebar, organization]
  71. );
  72. const viewPerformanceMetrics = useCallback(() => {
  73. Sentry.metrics.increment('ddm.view_performance_metrics', 1);
  74. trackAnalytics('ddm.view_performance_metrics', {
  75. organization,
  76. });
  77. emptyStateDismiss();
  78. }, [emptyStateDismiss, organization]);
  79. if (!canSeeMetricsPage(organization)) {
  80. return (
  81. <Layout.Page withPadding>
  82. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  83. </Layout.Page>
  84. );
  85. }
  86. return (
  87. <Fragment>
  88. <MetricsSubscriptionAlert organization={organization} />
  89. <Layout.Header>
  90. <Layout.HeaderContent>
  91. <Layout.Title>
  92. {t('Metrics')}
  93. <PageHeadingQuestionTooltip
  94. docsUrl={METRICS_DOCS_URL}
  95. title={t(
  96. 'Metrics help you track and visualize the data points you care about, making it easier to monitor your application health and identify issues.'
  97. )}
  98. />
  99. <FeatureBadge type="beta" />
  100. </Layout.Title>
  101. </Layout.HeaderContent>
  102. <Layout.HeaderActions>
  103. <PageHeaderActions
  104. showCustomMetricButton={
  105. hasCustomMetrics || (isEmptyStateDismissed && isBannerDismissed)
  106. }
  107. addCustomMetric={() => addCustomMetric('header')}
  108. />
  109. </Layout.HeaderActions>
  110. </Layout.Header>
  111. <Layout.Body>
  112. <FloatingFeedbackWidget />
  113. <Layout.Main fullWidth>
  114. {isEmptyStateDismissed && !hasCustomMetrics && (
  115. <Banner
  116. title={t('Custom Metrics')}
  117. subtitle={t(
  118. "Track your system's behaviour and profit from the same powerful features as you do with errors, like alerting and dashboards."
  119. )}
  120. backgroundComponent={<BackgroundSpace />}
  121. dismissKey="metrics"
  122. >
  123. <Button
  124. size={isSmallBanner ? 'xs' : undefined}
  125. translucentBorder
  126. onClick={() => addCustomMetric('banner')}
  127. >
  128. {t('Set Up')}
  129. </Button>
  130. </Banner>
  131. )}
  132. <FilterContainer>
  133. <PageFilterBar condensed>
  134. <ProjectPageFilter />
  135. <EnvironmentPageFilter />
  136. <DatePageFilter />
  137. </PageFilterBar>
  138. <IntervalSelect />
  139. </FilterContainer>
  140. {isHasMetricsLoading ? (
  141. <LoadingIndicator />
  142. ) : hasCustomMetrics || isEmptyStateDismissed ? (
  143. <Fragment>
  144. <GuideAnchor target="metrics_onboarding" />
  145. <Queries />
  146. <MetricScratchpad />
  147. <WidgetDetails />
  148. </Fragment>
  149. ) : (
  150. <OnboardingPanel image={<EmptyStateImage src={emptyStateImg} />}>
  151. <h3>{t('Track and solve what matters')}</h3>
  152. <p>
  153. {t(
  154. 'Create custom metrics to track and visualize the data points you care about over time, like processing time, checkout conversion rate, or user signups. See correlated trace exemplars and metrics if used together with Performance Monitoring.'
  155. )}
  156. </p>
  157. <MetricsOnboardingPanelPrimaryAction organization={organization}>
  158. <ButtonList gap={1}>
  159. <Button
  160. priority="primary"
  161. onClick={() => addCustomMetric('onboarding_panel')}
  162. >
  163. {t('Set Up Custom Metric')}
  164. </Button>
  165. <Button href={METRICS_DOCS_URL} external>
  166. {t('Read Docs')}
  167. </Button>
  168. {hasPerformanceMetrics && (
  169. <Button onClick={viewPerformanceMetrics}>
  170. {t('View Performance Metrics')}
  171. </Button>
  172. )}
  173. </ButtonList>
  174. </MetricsOnboardingPanelPrimaryAction>
  175. </OnboardingPanel>
  176. )}
  177. </Layout.Main>
  178. </Layout.Body>
  179. </Fragment>
  180. );
  181. });
  182. const FilterContainer = styled('div')`
  183. margin-bottom: ${space(2)};
  184. display: flex;
  185. justify-content: flex-start;
  186. flex-wrap: wrap;
  187. gap: ${space(1)};
  188. `;
  189. const EmptyStateImage = styled('img')`
  190. @media (min-width: ${p => p.theme.breakpoints.small}) {
  191. user-select: none;
  192. position: absolute;
  193. top: 0;
  194. bottom: 0;
  195. width: 220px;
  196. margin-top: auto;
  197. margin-bottom: auto;
  198. transform: translateX(-50%);
  199. left: 50%;
  200. }
  201. @media (min-width: ${p => p.theme.breakpoints.large}) {
  202. transform: translateX(-60%);
  203. width: 280px;
  204. }
  205. @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
  206. transform: translateX(-75%);
  207. width: 320px;
  208. }
  209. `;
  210. const ButtonList = styled(ButtonBar)`
  211. grid-template-columns: repeat(auto-fit, minmax(130px, max-content));
  212. `;