pageOverview.tsx 11 KB

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