webVitalDescription.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import {useTheme} from '@emotion/react';
  2. import styled from '@emotion/styled';
  3. import {CopyToClipboardButton} from 'sentry/components/copyToClipboardButton';
  4. import LoadingIndicator from 'sentry/components/loadingIndicator';
  5. import {COUNTRY_CODE_TO_NAME_MAP} from 'sentry/data/countryCodesMap';
  6. import {IconCheckmark} from 'sentry/icons/iconCheckmark';
  7. import {IconClose} from 'sentry/icons/iconClose';
  8. import {t, tct} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {Tag} from 'sentry/types';
  11. import {WebVital} from 'sentry/utils/fields';
  12. import {Browser} from 'sentry/utils/performance/vitals/constants';
  13. import {Dot} from 'sentry/views/performance/browser/webVitals/components/webVitalMeters';
  14. import {PERFORMANCE_SCORE_COLORS} from 'sentry/views/performance/browser/webVitals/utils/performanceScoreColors';
  15. import {
  16. scoreToStatus,
  17. STATUS_TEXT,
  18. } from 'sentry/views/performance/browser/webVitals/utils/scoreToStatus';
  19. import type {
  20. ProjectScore,
  21. WebVitals,
  22. } from 'sentry/views/performance/browser/webVitals/utils/types';
  23. import {vitalSupportedBrowsers} from 'sentry/views/performance/vitalDetail/utils';
  24. import PerformanceScoreRingWithTooltips from './performanceScoreRingWithTooltips';
  25. type Props = {
  26. webVital: WebVitals;
  27. score?: number;
  28. value?: string;
  29. };
  30. const WEB_VITAL_FULL_NAME_MAP = {
  31. cls: t('Cumulative Layout Shift'),
  32. fcp: t('First Contentful Paint'),
  33. fid: t('First Input Delay'),
  34. inp: t('Interaction to Next Paint'),
  35. lcp: t('Largest Contentful Paint'),
  36. ttfb: t('Time to First Byte'),
  37. };
  38. const VITAL_DESCRIPTIONS: Partial<Record<WebVital, string>> = {
  39. [WebVital.FCP]: t(
  40. 'First Contentful Paint (FCP) measures the amount of time the first content takes to render in the viewport. Like FP, this could also show up in any form from the document object model (DOM), such as images, SVGs, or text blocks.'
  41. ),
  42. [WebVital.CLS]: t(
  43. 'Cumulative Layout Shift (CLS) is the sum of individual layout shift scores for every unexpected element shift during the rendering process. Imagine navigating to an article and trying to click a link before the page finishes loading. Before your cursor even gets there, the link may have shifted down due to an image rendering. Rather than using duration for this Web Vital, the CLS score represents the degree of disruptive and visually unstable shifts.'
  44. ),
  45. [WebVital.FID]: t(
  46. 'First Input Delay (FID) measures the response time when the user tries to interact with the viewport. Actions maybe include clicking a button, link or other custom Javascript controller. It is key in helping the user determine if a page is usable or not.'
  47. ),
  48. [WebVital.LCP]: t(
  49. 'Largest Contentful Paint (LCP) measures the render time for the largest content to appear in the viewport. This may be in any form from the document object model (DOM), such as images, SVGs, or text blocks. It’s the largest pixel area in the viewport, thus most visually defining. LCP helps developers understand how long it takes to see the main content on the page.'
  50. ),
  51. [WebVital.TTFB]: t(
  52. 'Time to First Byte (TTFB) is a foundational metric for measuring connection setup time and web server responsiveness in both the lab and the field. It helps identify when a web server is too slow to respond to requests. In the case of navigation requests—that is, requests for an HTML document—it precedes every other meaningful loading performance metric.'
  53. ),
  54. [WebVital.INP]: t(
  55. "Interaction to Next Paint (INP) is a metric that assesses a page's overall responsiveness to user interactions by observing the latency of all click, tap, and keyboard interactions that occur throughout the lifespan of a user's visit to a page. The final INP value is the longest interaction observed, ignoring outliers."
  56. ),
  57. };
  58. type WebVitalDetailHeaderProps = {
  59. isProjectScoreCalculated: boolean;
  60. projectScore: ProjectScore;
  61. tag: Tag;
  62. value: React.ReactNode;
  63. };
  64. export function WebVitalDetailHeader({score, value, webVital}: Props) {
  65. const theme = useTheme();
  66. const colors = theme.charts.getColorPalette(3);
  67. const dotColor = colors[['lcp', 'fcp', 'fid', 'cls', 'ttfb'].indexOf(webVital)];
  68. const status = score !== undefined ? scoreToStatus(score) : undefined;
  69. return (
  70. <Header>
  71. <span>
  72. <WebVitalName>{`${WEB_VITAL_FULL_NAME_MAP[webVital]} (P75)`}</WebVitalName>
  73. <Value>
  74. <Dot color={dotColor} />
  75. {value ?? ' \u2014 '}
  76. </Value>
  77. </span>
  78. {status && score && (
  79. <ScoreBadge status={status}>
  80. <StatusText>{STATUS_TEXT[status]}</StatusText>
  81. <StatusScore>{score}</StatusScore>
  82. </ScoreBadge>
  83. )}
  84. </Header>
  85. );
  86. }
  87. export function WebVitalTagsDetailHeader({
  88. projectScore,
  89. value,
  90. tag,
  91. isProjectScoreCalculated,
  92. }: WebVitalDetailHeaderProps) {
  93. const theme = useTheme();
  94. const ringSegmentColors = theme.charts.getColorPalette(3);
  95. const ringBackgroundColors = ringSegmentColors.map(color => `${color}50`);
  96. const title =
  97. tag.key === 'geo.country_code' ? COUNTRY_CODE_TO_NAME_MAP[tag.name] : tag.name;
  98. return (
  99. <Header>
  100. <span>
  101. <TitleWrapper>
  102. <WebVitalName>{title}</WebVitalName>
  103. <StyledCopyToClipboardButton
  104. borderless
  105. text={`${tag.key}:${tag.name}`}
  106. size="sm"
  107. iconSize="sm"
  108. />
  109. </TitleWrapper>
  110. <Value>{value}</Value>
  111. </span>
  112. {isProjectScoreCalculated && projectScore ? (
  113. <PerformanceScoreRingWithTooltips
  114. hideWebVitalLabels
  115. projectScore={projectScore}
  116. text={projectScore.totalScore}
  117. width={100}
  118. height={100}
  119. ringBackgroundColors={ringBackgroundColors}
  120. ringSegmentColors={ringSegmentColors}
  121. size={100}
  122. x={0}
  123. y={0}
  124. />
  125. ) : (
  126. <StyledLoadingIndicator size={50} />
  127. )}
  128. </Header>
  129. );
  130. }
  131. export function WebVitalDescription({score, value, webVital}: Props) {
  132. const description: string = VITAL_DESCRIPTIONS[WebVital[webVital.toUpperCase()]];
  133. return (
  134. <div>
  135. <WebVitalDetailHeader score={score} value={value} webVital={webVital} />
  136. <p>{description}</p>
  137. <p>
  138. <b>
  139. {tct(
  140. `At the moment, there is support for [webVital] in the following browsers:`,
  141. {webVital: webVital.toUpperCase()}
  142. )}
  143. </b>
  144. </p>
  145. <SupportedBrowsers>
  146. {Object.values(Browser).map(browser => (
  147. <BrowserItem key={browser}>
  148. {vitalSupportedBrowsers[WebVital[webVital.toUpperCase()]]?.includes(
  149. browser
  150. ) ? (
  151. <IconCheckmark color="successText" size="sm" />
  152. ) : (
  153. <IconClose color="dangerText" size="sm" />
  154. )}
  155. {browser}
  156. </BrowserItem>
  157. ))}
  158. </SupportedBrowsers>
  159. </div>
  160. );
  161. }
  162. const SupportedBrowsers = styled('div')`
  163. display: inline-flex;
  164. gap: ${space(2)};
  165. margin-bottom: ${space(3)};
  166. `;
  167. const BrowserItem = styled('div')`
  168. display: flex;
  169. align-items: center;
  170. gap: ${space(1)};
  171. `;
  172. const Header = styled('span')`
  173. display: flex;
  174. justify-content: space-between;
  175. margin-bottom: ${space(3)};
  176. `;
  177. const Value = styled('h2')`
  178. display: flex;
  179. align-items: center;
  180. font-weight: normal;
  181. margin-bottom: ${space(1)};
  182. `;
  183. const WebVitalName = styled('h4')`
  184. margin-bottom: ${space(1)};
  185. margin-top: 40px;
  186. max-width: 400px;
  187. ${p => p.theme.overflowEllipsis}
  188. `;
  189. const TitleWrapper = styled('div')`
  190. display: flex;
  191. align-items: baseline;
  192. `;
  193. const StyledCopyToClipboardButton = styled(CopyToClipboardButton)`
  194. padding-left: ${space(0.5)};
  195. `;
  196. const StyledLoadingIndicator = styled(LoadingIndicator)`
  197. margin: 20px 65px;
  198. `;
  199. const ScoreBadge = styled('div')<{status: string}>`
  200. display: flex;
  201. justify-content: center;
  202. align-items: center;
  203. flex-direction: column;
  204. color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  205. background-color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  206. border: solid 1px ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  207. padding: ${space(0.5)};
  208. text-align: center;
  209. height: 60px;
  210. width: 60px;
  211. border-radius: 60px;
  212. `;
  213. const StatusText = styled('span')`
  214. padding-top: ${space(0.5)};
  215. font-size: ${p => p.theme.fontSizeSmall};
  216. `;
  217. const StatusScore = styled('span')`
  218. font-weight: bold;
  219. font-size: ${p => p.theme.fontSizeLarge};
  220. `;