pageOverview.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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 moment from 'moment';
  6. import ProjectAvatar from 'sentry/components/avatar/projectAvatar';
  7. import {Breadcrumbs} from 'sentry/components/breadcrumbs';
  8. import {LinkButton} from 'sentry/components/button';
  9. import {AggregateSpans} from 'sentry/components/events/interfaces/spans/aggregateSpans';
  10. import FloatingFeedbackWidget from 'sentry/components/feedback/widget/floatingFeedbackWidget';
  11. import * as Layout from 'sentry/components/layouts/thirds';
  12. import ExternalLink from 'sentry/components/links/externalLink';
  13. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  14. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  15. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  16. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  17. import {TabList, Tabs} from 'sentry/components/tabs';
  18. import {IconChevron, IconClose} from 'sentry/icons';
  19. import {t, tct} from 'sentry/locale';
  20. import ConfigStore from 'sentry/stores/configStore';
  21. import {space} from 'sentry/styles/space';
  22. import {defined} from 'sentry/utils';
  23. import {decodeScalar} from 'sentry/utils/queryString';
  24. import useDismissAlert from 'sentry/utils/useDismissAlert';
  25. import {useLocation} from 'sentry/utils/useLocation';
  26. import useOrganization from 'sentry/utils/useOrganization';
  27. import useProjects from 'sentry/utils/useProjects';
  28. import useRouter from 'sentry/utils/useRouter';
  29. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  30. import {PageOverviewSidebar} from 'sentry/views/performance/browser/webVitals/components/pageOverviewSidebar';
  31. import {
  32. PerformanceScoreBreakdownChart,
  33. SCORE_MIGRATION_TIMESTAMP,
  34. } from 'sentry/views/performance/browser/webVitals/components/performanceScoreBreakdownChart';
  35. import WebVitalMeters from 'sentry/views/performance/browser/webVitals/components/webVitalMeters';
  36. import {PageOverviewWebVitalsDetailPanel} from 'sentry/views/performance/browser/webVitals/pageOverviewWebVitalsDetailPanel';
  37. import {PageSamplePerformanceTable} from 'sentry/views/performance/browser/webVitals/pageSamplePerformanceTable';
  38. import {calculatePerformanceScoreFromTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/calculatePerformanceScore';
  39. import {useProjectRawWebVitalsQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/rawWebVitalsQueries/useProjectRawWebVitalsQuery';
  40. import {calculatePerformanceScoreFromStoredTableDataRow} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/calculatePerformanceScoreFromStored';
  41. import {useProjectWebVitalsScoresQuery} from 'sentry/views/performance/browser/webVitals/utils/queries/storedScoreQueries/useProjectWebVitalsScoresQuery';
  42. import type {WebVitals} from 'sentry/views/performance/browser/webVitals/utils/types';
  43. import {useStoredScoresSetting} from 'sentry/views/performance/browser/webVitals/utils/useStoredScoresSetting';
  44. import {
  45. AlertContent,
  46. DismissButton,
  47. StyledAlert,
  48. } from 'sentry/views/performance/browser/webVitals/webVitalsLandingPage';
  49. import {ModulePageProviders} from 'sentry/views/performance/modulePageProviders';
  50. import {transactionSummaryRouteWithQuery} from '../../transactionSummary/utils';
  51. export enum LandingDisplayField {
  52. OVERVIEW = 'overview',
  53. SPANS = 'spans',
  54. }
  55. const LANDING_DISPLAYS = [
  56. {
  57. label: t('Overview'),
  58. field: LandingDisplayField.OVERVIEW,
  59. },
  60. {
  61. label: t('Aggregate Spans'),
  62. field: LandingDisplayField.SPANS,
  63. },
  64. ];
  65. function getCurrentTabSelection(selectedTab) {
  66. const tab = decodeScalar(selectedTab);
  67. if (tab && Object.values(LandingDisplayField).includes(tab as LandingDisplayField)) {
  68. return tab as LandingDisplayField;
  69. }
  70. return LandingDisplayField.OVERVIEW;
  71. }
  72. export default function PageOverview() {
  73. const organization = useOrganization();
  74. const location = useLocation();
  75. const {projects} = useProjects();
  76. const router = useRouter();
  77. const shouldUseStoredScores = useStoredScoresSetting();
  78. const transaction = location.query.transaction
  79. ? Array.isArray(location.query.transaction)
  80. ? location.query.transaction[0]
  81. : location.query.transaction
  82. : undefined;
  83. const project = useMemo(
  84. () => projects.find(p => p.id === String(location.query.project)),
  85. [projects, location.query.project]
  86. );
  87. const tab = getCurrentTabSelection(location.query.tab);
  88. // TODO: When visiting page overview from a specific webvital detail panel in the landing page,
  89. // we should automatically default this webvital state to the respective webvital so the detail
  90. // panel in this page opens automatically.
  91. const [state, setState] = useState<{webVital: WebVitals | null}>({
  92. webVital: (location.query.webVital as WebVitals) ?? null,
  93. });
  94. const user = ConfigStore.get('user');
  95. const {dismiss, isDismissed} = useDismissAlert({
  96. key: `${organization.slug}-${user.id}:performance-score-migration-message-dismissed`,
  97. });
  98. const query = decodeScalar(location.query.query);
  99. const {data: pageData, isLoading} = useProjectRawWebVitalsQuery({transaction});
  100. const {data: projectScores, isLoading: isProjectScoresLoading} =
  101. useProjectWebVitalsScoresQuery({transaction, enabled: shouldUseStoredScores});
  102. if (transaction === undefined) {
  103. // redirect user to webvitals landing page
  104. window.location.href = normalizeUrl(
  105. `/organizations/${organization.slug}/performance/browser/pageloads/`
  106. );
  107. return null;
  108. }
  109. const transactionSummaryTarget =
  110. project &&
  111. !Array.isArray(location.query.project) && // Only render button to transaction summary when one project is selected.
  112. transaction &&
  113. transactionSummaryRouteWithQuery({
  114. orgSlug: organization.slug,
  115. transaction,
  116. query: {...location.query},
  117. projectID: project.id,
  118. });
  119. const projectScore =
  120. (shouldUseStoredScores && isProjectScoresLoading) || isLoading
  121. ? undefined
  122. : shouldUseStoredScores
  123. ? calculatePerformanceScoreFromStoredTableDataRow(projectScores?.data?.[0])
  124. : calculatePerformanceScoreFromTableDataRow(pageData?.data?.[0]);
  125. const scoreMigrationTimestampString = moment(SCORE_MIGRATION_TIMESTAMP).format(
  126. 'DD MMMM YYYY'
  127. );
  128. return (
  129. <ModulePageProviders
  130. title={[t('Performance'), t('Web Vitals')].join(' — ')}
  131. baseURL="/performance/browser/pageloads"
  132. features="starfish-browser-webvitals"
  133. >
  134. <Tabs
  135. value={tab}
  136. onChange={value => {
  137. browserHistory.push({
  138. ...location,
  139. query: {
  140. ...location.query,
  141. tab: value,
  142. },
  143. });
  144. }}
  145. >
  146. <Layout.Header>
  147. <Layout.HeaderContent>
  148. <Breadcrumbs
  149. crumbs={[
  150. {
  151. label: 'Performance',
  152. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  153. preservePageFilters: true,
  154. },
  155. {
  156. label: 'Web Vitals',
  157. to: normalizeUrl(
  158. `/organizations/${organization.slug}/performance/browser/pageloads/`
  159. ),
  160. preservePageFilters: true,
  161. },
  162. ...(transaction ? [{label: 'Page Overview'}] : []),
  163. ]}
  164. />
  165. <Layout.Title>
  166. {transaction && project && <ProjectAvatar project={project} size={24} />}
  167. {transaction ?? t('Page Loads')}
  168. </Layout.Title>
  169. </Layout.HeaderContent>
  170. <Layout.HeaderActions>
  171. {transactionSummaryTarget && (
  172. <LinkButton to={transactionSummaryTarget} size="sm">
  173. {t('View Transaction Summary')}
  174. </LinkButton>
  175. )}
  176. </Layout.HeaderActions>
  177. <TabList hideBorder>
  178. {LANDING_DISPLAYS.map(({label, field}) => (
  179. <TabList.Item key={field}>{label}</TabList.Item>
  180. ))}
  181. </TabList>
  182. </Layout.Header>
  183. {tab === LandingDisplayField.SPANS ? (
  184. <Layout.Body>
  185. <Layout.Main fullWidth>
  186. {defined(transaction) && <AggregateSpans transaction={transaction} />}
  187. </Layout.Main>
  188. </Layout.Body>
  189. ) : (
  190. <Layout.Body>
  191. <FloatingFeedbackWidget />
  192. <Layout.Main>
  193. <TopMenuContainer>
  194. {transaction && (
  195. <ViewAllPagesButton
  196. to={{
  197. ...location,
  198. pathname: '/performance/browser/pageloads/',
  199. query: {...location.query, transaction: undefined},
  200. }}
  201. >
  202. <IconChevron direction="left" /> {t('View All Pages')}
  203. </ViewAllPagesButton>
  204. )}
  205. <PageFilterBar condensed>
  206. <ProjectPageFilter />
  207. <EnvironmentPageFilter />
  208. <DatePageFilter />
  209. </PageFilterBar>
  210. </TopMenuContainer>
  211. {shouldUseStoredScores && !isDismissed && (
  212. <StyledAlert type="info" showIcon>
  213. <AlertContent>
  214. <span>
  215. {tct(
  216. `We made improvements to how Performance Scores are calculated for your projects. Starting on [scoreMigrationTimestampString], scores are updated to more accurately reflect user experiences. [link:Read more about these improvements].`,
  217. {
  218. scoreMigrationTimestampString,
  219. link: (
  220. <ExternalLink href="https://sentry.engineering/blog/how-we-improved-performance-score-accuracy" />
  221. ),
  222. }
  223. )}
  224. </span>
  225. <DismissButton
  226. priority="link"
  227. icon={<IconClose />}
  228. onClick={dismiss}
  229. aria-label={t('Dismiss Alert')}
  230. title={t('Dismiss Alert')}
  231. />
  232. </AlertContent>
  233. </StyledAlert>
  234. )}
  235. <Flex>
  236. <PerformanceScoreBreakdownChart transaction={transaction} />
  237. </Flex>
  238. <WebVitalMetersContainer>
  239. <WebVitalMeters
  240. projectData={pageData}
  241. projectScore={projectScore}
  242. onClick={webVital => {
  243. router.replace({
  244. pathname: location.pathname,
  245. query: {...location.query, webVital},
  246. });
  247. setState({...state, webVital});
  248. }}
  249. transaction={transaction}
  250. showTooltip={false}
  251. />
  252. </WebVitalMetersContainer>
  253. <PageSamplePerformanceTableContainer>
  254. <PageSamplePerformanceTable
  255. transaction={transaction}
  256. limit={15}
  257. search={query}
  258. />
  259. </PageSamplePerformanceTableContainer>
  260. </Layout.Main>
  261. <Layout.Side>
  262. <PageOverviewSidebar
  263. projectScore={projectScore}
  264. transaction={transaction}
  265. projectScoreIsLoading={isLoading}
  266. />
  267. </Layout.Side>
  268. </Layout.Body>
  269. )}
  270. <PageOverviewWebVitalsDetailPanel
  271. webVital={state.webVital}
  272. onClose={() => {
  273. router.replace({
  274. pathname: router.location.pathname,
  275. query: omit(router.location.query, 'webVital'),
  276. });
  277. setState({...state, webVital: null});
  278. }}
  279. />
  280. </Tabs>
  281. </ModulePageProviders>
  282. );
  283. }
  284. const ViewAllPagesButton = styled(LinkButton)`
  285. margin-right: ${space(1)};
  286. `;
  287. const TopMenuContainer = styled('div')`
  288. margin-bottom: ${space(1)};
  289. display: flex;
  290. `;
  291. const Flex = styled('div')`
  292. display: flex;
  293. flex-direction: row;
  294. justify-content: space-between;
  295. width: 100%;
  296. gap: ${space(1)};
  297. `;
  298. const PageSamplePerformanceTableContainer = styled('div')`
  299. margin-top: ${space(1)};
  300. `;
  301. const WebVitalMetersContainer = styled('div')`
  302. margin: ${space(2)} 0 ${space(4)} 0;
  303. `;