webVitalsLandingPage.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import {Fragment, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import omit from 'lodash/omit';
  4. import Breadcrumbs from 'sentry/components/breadcrumbs';
  5. import FeedbackWidget from 'sentry/components/feedback/widget/feedbackWidget';
  6. import * as Layout from 'sentry/components/layouts/thirds';
  7. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  8. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  9. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  10. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import {useLocation} from 'sentry/utils/useLocation';
  14. import useOrganization from 'sentry/utils/useOrganization';
  15. import useRouter from 'sentry/utils/useRouter';
  16. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  17. import WebVitalMeters from 'sentry/views/performance/browser/webVitals/components/webVitalMeters';
  18. import {PagePerformanceTable} from 'sentry/views/performance/browser/webVitals/pagePerformanceTable';
  19. import {PerformanceScoreChart} from 'sentry/views/performance/browser/webVitals/performanceScoreChart';
  20. import {USE_STORED_SCORES} from 'sentry/views/performance/browser/webVitals/settings';
  21. import {calculatePerformanceScoreFromTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/calculatePerformanceScore';
  22. import {useProjectRawWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsQuery';
  23. import {calculatePerformanceScoreFromStoredTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/calculatePerformanceScoreFromStored';
  24. import {useProjectWebVitalsScoresQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/useProjectWebVitalsScoresQuery';
  25. import {WebVitals} from 'sentry/views/performance/browser/webVitals/utils/types';
  26. import {useOnboardingProject} from 'sentry/views/performance/browser/webVitals/utils/useOnboardingProject';
  27. import {WebVitalsDetailPanel} from 'sentry/views/performance/browser/webVitals/webVitalsDetailPanel';
  28. import {ModulePageProviders} from 'sentry/views/performance/database/modulePageProviders';
  29. import Onboarding from 'sentry/views/performance/onboarding';
  30. export default function WebVitalsLandingPage() {
  31. const organization = useOrganization();
  32. const location = useLocation();
  33. const onboardingProject = useOnboardingProject();
  34. const router = useRouter();
  35. const [state, setState] = useState<{webVital: WebVitals | null}>({
  36. webVital: (location.query.webVital as WebVitals) ?? null,
  37. });
  38. const {data: projectData, isLoading} = useProjectRawWebVitalsQuery({});
  39. const {data: projectScores, isLoading: isProjectScoresLoading} =
  40. useProjectWebVitalsScoresQuery({enabled: USE_STORED_SCORES});
  41. const noTransactions = !isLoading && !projectData?.data?.[0]?.['count()'];
  42. const projectScore =
  43. (USE_STORED_SCORES && isProjectScoresLoading) || isLoading || noTransactions
  44. ? undefined
  45. : USE_STORED_SCORES
  46. ? calculatePerformanceScoreFromStoredTableDataRow(projectScores?.data?.[0])
  47. : calculatePerformanceScoreFromTableDataRow(projectData?.data?.[0]);
  48. return (
  49. <ModulePageProviders title={[t('Performance'), t('Web Vitals')].join(' — ')}>
  50. <Layout.Header>
  51. <Layout.HeaderContent>
  52. <Breadcrumbs
  53. crumbs={[
  54. {
  55. label: 'Performance',
  56. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  57. preservePageFilters: true,
  58. },
  59. {
  60. label: 'Web Vitals',
  61. },
  62. ]}
  63. />
  64. <Layout.Title>{t('Web Vitals')}</Layout.Title>
  65. </Layout.HeaderContent>
  66. </Layout.Header>
  67. <Layout.Body>
  68. <FeedbackWidget />
  69. <Layout.Main fullWidth>
  70. <TopMenuContainer>
  71. <PageFilterBar condensed>
  72. <ProjectPageFilter />
  73. <EnvironmentPageFilter />
  74. <DatePageFilter />
  75. </PageFilterBar>
  76. </TopMenuContainer>
  77. {onboardingProject && (
  78. <OnboardingContainer>
  79. <Onboarding organization={organization} project={onboardingProject} />
  80. </OnboardingContainer>
  81. )}
  82. {!onboardingProject && (
  83. <Fragment>
  84. <PerformanceScoreChartContainer>
  85. <PerformanceScoreChart
  86. projectScore={projectScore}
  87. isProjectScoreLoading={
  88. isLoading || (USE_STORED_SCORES && isProjectScoresLoading)
  89. }
  90. webVital={state.webVital}
  91. />
  92. </PerformanceScoreChartContainer>
  93. <WebVitalMeters
  94. projectData={projectData}
  95. projectScore={projectScore}
  96. onClick={webVital => setState({...state, webVital})}
  97. />
  98. <PagePerformanceTable />
  99. </Fragment>
  100. )}
  101. </Layout.Main>
  102. </Layout.Body>
  103. <WebVitalsDetailPanel
  104. webVital={state.webVital}
  105. onClose={() => {
  106. router.replace({
  107. pathname: router.location.pathname,
  108. query: omit(router.location.query, 'webVital'),
  109. });
  110. setState({...state, webVital: null});
  111. }}
  112. />
  113. </ModulePageProviders>
  114. );
  115. }
  116. const TopMenuContainer = styled('div')`
  117. display: flex;
  118. `;
  119. const PerformanceScoreChartContainer = styled('div')`
  120. margin-bottom: ${space(1)};
  121. `;
  122. const OnboardingContainer = styled('div')`
  123. margin-top: ${space(2)};
  124. `;