pageOverview.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import React, {useMemo, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import omit from 'lodash/omit';
  4. import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
  5. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  6. import {LinkButton} from 'sentry/components/button';
  7. import ButtonBar from 'sentry/components/buttonBar';
  8. import {AggregateSpans} from 'sentry/components/events/interfaces/spans/aggregateSpans';
  9. import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
  10. import * as Layout from 'sentry/components/layouts/thirds';
  11. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  12. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  13. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  14. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  15. import {TabList, Tabs} from 'sentry/components/tabs';
  16. import {IconChevron} from 'sentry/icons';
  17. import {t} from 'sentry/locale';
  18. import {space} from 'sentry/styles/space';
  19. import {defined} from 'sentry/utils';
  20. import {trackAnalytics} from 'sentry/utils/analytics';
  21. import {browserHistory} from 'sentry/utils/browserHistory';
  22. import {decodeScalar} from 'sentry/utils/queryString';
  23. import {useLocation} from 'sentry/utils/useLocation';
  24. import useOrganization from 'sentry/utils/useOrganization';
  25. import useProjects from 'sentry/utils/useProjects';
  26. import useRouter from 'sentry/utils/useRouter';
  27. import {PageOverviewSidebar} from 'sentry/views/performance/browser/webVitals/components/pageOverviewSidebar';
  28. import {PerformanceScoreBreakdownChart} from 'sentry/views/performance/browser/webVitals/components/performanceScoreBreakdownChart';
  29. import WebVitalMeters from 'sentry/views/performance/browser/webVitals/components/webVitalMeters';
  30. import {PageOverviewWebVitalsDetailPanel} from 'sentry/views/performance/browser/webVitals/pageOverviewWebVitalsDetailPanel';
  31. import {PageSamplePerformanceTable} from 'sentry/views/performance/browser/webVitals/pageSamplePerformanceTable';
  32. import {useProjectRawWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsQuery';
  33. import {calculatePerformanceScoreFromStoredTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/calculatePerformanceScoreFromStored';
  34. import {useProjectWebVitalsScoresQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/useProjectWebVitalsScoresQuery';
  35. import type {WebVitals} from 'sentry/views/performance/browser/webVitals/utils/types';
  36. import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
  37. import {useModuleBreadcrumbs} from 'sentry/views/performance/utils/useModuleBreadcrumbs';
  38. import {useModuleURL} from 'sentry/views/performance/utils/useModuleURL';
  39. import {transactionSummaryRouteWithQuery} from '../../transactionSummary/utils';
  40. export enum LandingDisplayField {
  41. OVERVIEW = 'overview',
  42. SPANS = 'spans',
  43. }
  44. const LANDING_DISPLAYS = [
  45. {
  46. label: t('Overview'),
  47. field: LandingDisplayField.OVERVIEW,
  48. },
  49. {
  50. label: t('Aggregate Spans'),
  51. field: LandingDisplayField.SPANS,
  52. },
  53. ];
  54. function getCurrentTabSelection(selectedTab) {
  55. const tab = decodeScalar(selectedTab);
  56. if (tab && Object.values(LandingDisplayField).includes(tab as LandingDisplayField)) {
  57. return tab as LandingDisplayField;
  58. }
  59. return LandingDisplayField.OVERVIEW;
  60. }
  61. export function PageOverview() {
  62. const moduleURL = useModuleURL('vital');
  63. const organization = useOrganization();
  64. const location = useLocation();
  65. const {projects} = useProjects();
  66. const router = useRouter();
  67. const transaction = location.query.transaction
  68. ? Array.isArray(location.query.transaction)
  69. ? location.query.transaction[0]
  70. : location.query.transaction
  71. : undefined;
  72. const project = useMemo(
  73. () => projects.find(p => p.id === String(location.query.project)),
  74. [projects, location.query.project]
  75. );
  76. const tab = getCurrentTabSelection(location.query.tab);
  77. // TODO: When visiting page overview from a specific webvital detail panel in the landing page,
  78. // we should automatically default this webvital state to the respective webvital so the detail
  79. // panel in this page opens automatically.
  80. const [state, setState] = useState<{webVital: WebVitals | null}>({
  81. webVital: (location.query.webVital as WebVitals) ?? null,
  82. });
  83. const crumbs = useModuleBreadcrumbs('vital');
  84. const query = decodeScalar(location.query.query);
  85. const {data: pageData, isLoading} = useProjectRawWebVitalsQuery({transaction});
  86. const {data: projectScores, isLoading: isProjectScoresLoading} =
  87. useProjectWebVitalsScoresQuery({transaction});
  88. if (transaction === undefined) {
  89. // redirect user to webvitals landing page
  90. window.location.href = moduleURL;
  91. return null;
  92. }
  93. const transactionSummaryTarget =
  94. project &&
  95. !Array.isArray(location.query.project) && // Only render button to transaction summary when one project is selected.
  96. transaction &&
  97. transactionSummaryRouteWithQuery({
  98. orgSlug: organization.slug,
  99. transaction,
  100. query: {...location.query},
  101. projectID: project.id,
  102. });
  103. const projectScore =
  104. isProjectScoresLoading || isLoading
  105. ? undefined
  106. : calculatePerformanceScoreFromStoredTableDataRow(projectScores?.data?.[0]);
  107. return (
  108. <React.Fragment>
  109. <Tabs
  110. value={tab}
  111. onChange={value => {
  112. trackAnalytics('insight.vital.overview.toggle_tab', {
  113. organization,
  114. tab: value,
  115. });
  116. browserHistory.push({
  117. ...location,
  118. query: {
  119. ...location.query,
  120. tab: value,
  121. },
  122. });
  123. }}
  124. >
  125. <Layout.Header>
  126. <Layout.HeaderContent>
  127. <Breadcrumbs
  128. crumbs={[...crumbs, ...(transaction ? [{label: 'Page Overview'}] : [])]}
  129. />
  130. <Layout.Title>
  131. {transaction && project && <ProjectAvatar project={project} size={24} />}
  132. {transaction ?? t('Page Loads')}
  133. </Layout.Title>
  134. </Layout.HeaderContent>
  135. <Layout.HeaderActions>
  136. <ButtonBar gap={1}>
  137. <FeedbackWidgetButton />
  138. {transactionSummaryTarget && (
  139. <LinkButton
  140. to={transactionSummaryTarget}
  141. onClick={() => {
  142. trackAnalytics('insight.vital.overview.open_transaction_summary', {
  143. organization,
  144. });
  145. }}
  146. size="sm"
  147. >
  148. {t('View Transaction Summary')}
  149. </LinkButton>
  150. )}
  151. </ButtonBar>
  152. </Layout.HeaderActions>
  153. <TabList hideBorder>
  154. {LANDING_DISPLAYS.map(({label, field}) => (
  155. <TabList.Item key={field}>{label}</TabList.Item>
  156. ))}
  157. </TabList>
  158. </Layout.Header>
  159. {tab === LandingDisplayField.SPANS ? (
  160. <Layout.Body>
  161. <Layout.Main fullWidth>
  162. {defined(transaction) && <AggregateSpans transaction={transaction} />}
  163. </Layout.Main>
  164. </Layout.Body>
  165. ) : (
  166. <Layout.Body>
  167. <Layout.Main>
  168. <TopMenuContainer>
  169. {transaction && (
  170. <ViewAllPagesButton
  171. to={{
  172. ...location,
  173. pathname: '/performance/browser/pageloads/',
  174. query: {...location.query, transaction: undefined},
  175. }}
  176. >
  177. <IconChevron direction="left" /> {t('View All Pages')}
  178. </ViewAllPagesButton>
  179. )}
  180. <PageFilterBar condensed>
  181. <ProjectPageFilter />
  182. <EnvironmentPageFilter />
  183. <DatePageFilter />
  184. </PageFilterBar>
  185. </TopMenuContainer>
  186. <Flex>
  187. <PerformanceScoreBreakdownChart transaction={transaction} />
  188. </Flex>
  189. <WebVitalMetersContainer>
  190. <WebVitalMeters
  191. projectData={pageData}
  192. projectScore={projectScore}
  193. onClick={webVital => {
  194. router.replace({
  195. pathname: location.pathname,
  196. query: {...location.query, webVital},
  197. });
  198. setState({...state, webVital});
  199. }}
  200. transaction={transaction}
  201. showTooltip={false}
  202. />
  203. </WebVitalMetersContainer>
  204. <PageSamplePerformanceTableContainer>
  205. <PageSamplePerformanceTable
  206. transaction={transaction}
  207. limit={15}
  208. search={query}
  209. />
  210. </PageSamplePerformanceTableContainer>
  211. </Layout.Main>
  212. <Layout.Side>
  213. <PageOverviewSidebar
  214. projectScore={projectScore}
  215. transaction={transaction}
  216. projectScoreIsLoading={isLoading}
  217. />
  218. </Layout.Side>
  219. </Layout.Body>
  220. )}
  221. <PageOverviewWebVitalsDetailPanel
  222. webVital={state.webVital}
  223. onClose={() => {
  224. router.replace({
  225. pathname: router.location.pathname,
  226. query: omit(router.location.query, 'webVital'),
  227. });
  228. setState({...state, webVital: null});
  229. }}
  230. />
  231. </Tabs>
  232. </React.Fragment>
  233. );
  234. }
  235. function PageWithProviders() {
  236. return (
  237. <ModulePageProviders moduleName="vital" features="insights-initial-modules">
  238. <PageOverview />
  239. </ModulePageProviders>
  240. );
  241. }
  242. export default PageWithProviders;
  243. const ViewAllPagesButton = styled(LinkButton)`
  244. margin-right: ${space(1)};
  245. `;
  246. const TopMenuContainer = styled('div')`
  247. margin-bottom: ${space(1)};
  248. display: flex;
  249. `;
  250. const Flex = styled('div')`
  251. display: flex;
  252. flex-direction: row;
  253. justify-content: space-between;
  254. width: 100%;
  255. gap: ${space(1)};
  256. `;
  257. const PageSamplePerformanceTableContainer = styled('div')`
  258. margin-top: ${space(1)};
  259. `;
  260. const WebVitalMetersContainer = styled('div')`
  261. margin: ${space(2)} 0 ${space(4)} 0;
  262. `;