webVitalDescription.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 {
  15. ORDER,
  16. ORDER_WITH_INP,
  17. } from 'sentry/views/performance/browser/webVitals/performanceScoreChart';
  18. import {PERFORMANCE_SCORE_COLORS} from 'sentry/views/performance/browser/webVitals/utils/performanceScoreColors';
  19. import {
  20. scoreToStatus,
  21. STATUS_TEXT,
  22. } from 'sentry/views/performance/browser/webVitals/utils/scoreToStatus';
  23. import type {
  24. ProjectScore,
  25. WebVitals,
  26. } from 'sentry/views/performance/browser/webVitals/utils/types';
  27. import {useReplaceFidWithInpSetting} from 'sentry/views/performance/browser/webVitals/utils/useReplaceFidWithInpSetting';
  28. import {vitalSupportedBrowsers} from 'sentry/views/performance/vitalDetail/utils';
  29. import PerformanceScoreRingWithTooltips from './performanceScoreRingWithTooltips';
  30. type Props = {
  31. webVital: WebVitals;
  32. score?: number;
  33. value?: string;
  34. };
  35. const WEB_VITAL_FULL_NAME_MAP = {
  36. cls: t('Cumulative Layout Shift'),
  37. fcp: t('First Contentful Paint'),
  38. fid: t('First Input Delay'),
  39. inp: t('Interaction to Next Paint'),
  40. lcp: t('Largest Contentful Paint'),
  41. ttfb: t('Time to First Byte'),
  42. };
  43. const VITAL_DESCRIPTIONS: Partial<Record<WebVital, string>> = {
  44. [WebVital.FCP]: t(
  45. '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.'
  46. ),
  47. [WebVital.CLS]: t(
  48. '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.'
  49. ),
  50. [WebVital.FID]: t(
  51. '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.'
  52. ),
  53. [WebVital.LCP]: t(
  54. '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.'
  55. ),
  56. [WebVital.TTFB]: t(
  57. '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.'
  58. ),
  59. [WebVital.INP]: t(
  60. "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."
  61. ),
  62. };
  63. type WebVitalDetailHeaderProps = {
  64. isProjectScoreCalculated: boolean;
  65. projectScore: ProjectScore;
  66. tag: Tag;
  67. value: React.ReactNode;
  68. };
  69. export function WebVitalDetailHeader({score, value, webVital}: Props) {
  70. const shouldReplaceFidWithInp = useReplaceFidWithInpSetting();
  71. const theme = useTheme();
  72. const colors = theme.charts.getColorPalette(3);
  73. const dotColor =
  74. colors[(shouldReplaceFidWithInp ? ORDER_WITH_INP : ORDER).indexOf(webVital)];
  75. const status = score !== undefined ? scoreToStatus(score) : undefined;
  76. return (
  77. <Header>
  78. <span>
  79. <WebVitalName>{`${WEB_VITAL_FULL_NAME_MAP[webVital]} (P75)`}</WebVitalName>
  80. <Value>
  81. <Dot color={dotColor} />
  82. {value ?? ' \u2014 '}
  83. </Value>
  84. </span>
  85. {status && score && (
  86. <ScoreBadge status={status}>
  87. <StatusText>{STATUS_TEXT[status]}</StatusText>
  88. <StatusScore>{score}</StatusScore>
  89. </ScoreBadge>
  90. )}
  91. </Header>
  92. );
  93. }
  94. export function WebVitalTagsDetailHeader({
  95. projectScore,
  96. value,
  97. tag,
  98. isProjectScoreCalculated,
  99. }: WebVitalDetailHeaderProps) {
  100. const theme = useTheme();
  101. const ringSegmentColors = theme.charts.getColorPalette(3);
  102. const ringBackgroundColors = ringSegmentColors.map(color => `${color}50`);
  103. const title =
  104. tag.key === 'geo.country_code' ? COUNTRY_CODE_TO_NAME_MAP[tag.name] : tag.name;
  105. return (
  106. <Header>
  107. <span>
  108. <TitleWrapper>
  109. <WebVitalName>{title}</WebVitalName>
  110. <StyledCopyToClipboardButton
  111. borderless
  112. text={`${tag.key}:${tag.name}`}
  113. size="sm"
  114. iconSize="sm"
  115. />
  116. </TitleWrapper>
  117. <Value>{value}</Value>
  118. </span>
  119. {isProjectScoreCalculated && projectScore ? (
  120. <PerformanceScoreRingWithTooltips
  121. hideWebVitalLabels
  122. projectScore={projectScore}
  123. text={projectScore.totalScore}
  124. width={100}
  125. height={100}
  126. ringBackgroundColors={ringBackgroundColors}
  127. ringSegmentColors={ringSegmentColors}
  128. size={100}
  129. x={0}
  130. y={0}
  131. />
  132. ) : (
  133. <StyledLoadingIndicator size={50} />
  134. )}
  135. </Header>
  136. );
  137. }
  138. export function WebVitalDescription({score, value, webVital}: Props) {
  139. const description: string = VITAL_DESCRIPTIONS[WebVital[webVital.toUpperCase()]];
  140. return (
  141. <div>
  142. <WebVitalDetailHeader score={score} value={value} webVital={webVital} />
  143. <p>{description}</p>
  144. <p>
  145. <b>
  146. {tct(
  147. `At the moment, there is support for [webVital] in the following browsers:`,
  148. {webVital: webVital.toUpperCase()}
  149. )}
  150. </b>
  151. </p>
  152. <SupportedBrowsers>
  153. {Object.values(Browser).map(browser => (
  154. <BrowserItem key={browser}>
  155. {vitalSupportedBrowsers[WebVital[webVital.toUpperCase()]]?.includes(
  156. browser
  157. ) ? (
  158. <IconCheckmark color="successText" size="sm" />
  159. ) : (
  160. <IconClose color="dangerText" size="sm" />
  161. )}
  162. {browser}
  163. </BrowserItem>
  164. ))}
  165. </SupportedBrowsers>
  166. </div>
  167. );
  168. }
  169. const SupportedBrowsers = styled('div')`
  170. display: inline-flex;
  171. gap: ${space(2)};
  172. margin-bottom: ${space(3)};
  173. `;
  174. const BrowserItem = styled('div')`
  175. display: flex;
  176. align-items: center;
  177. gap: ${space(1)};
  178. `;
  179. const Header = styled('span')`
  180. display: flex;
  181. justify-content: space-between;
  182. margin-bottom: ${space(3)};
  183. `;
  184. const Value = styled('h2')`
  185. display: flex;
  186. align-items: center;
  187. font-weight: normal;
  188. margin-bottom: ${space(1)};
  189. `;
  190. const WebVitalName = styled('h4')`
  191. margin-bottom: ${space(1)};
  192. margin-top: 40px;
  193. max-width: 400px;
  194. ${p => p.theme.overflowEllipsis}
  195. `;
  196. const TitleWrapper = styled('div')`
  197. display: flex;
  198. align-items: baseline;
  199. `;
  200. const StyledCopyToClipboardButton = styled(CopyToClipboardButton)`
  201. padding-left: ${space(0.5)};
  202. `;
  203. const StyledLoadingIndicator = styled(LoadingIndicator)`
  204. margin: 20px 65px;
  205. `;
  206. const ScoreBadge = styled('div')<{status: string}>`
  207. display: flex;
  208. justify-content: center;
  209. align-items: center;
  210. flex-direction: column;
  211. color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  212. background-color: ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].light]};
  213. border: solid 1px ${p => p.theme[PERFORMANCE_SCORE_COLORS[p.status].normal]};
  214. padding: ${space(0.5)};
  215. text-align: center;
  216. height: 60px;
  217. width: 60px;
  218. border-radius: 60px;
  219. `;
  220. const StatusText = styled('span')`
  221. padding-top: ${space(0.5)};
  222. font-size: ${p => p.theme.fontSizeSmall};
  223. `;
  224. const StatusScore = styled('span')`
  225. font-weight: bold;
  226. font-size: ${p => p.theme.fontSizeLarge};
  227. `;