Skip to content

Commit 6cdb5a0

Browse files
authored
Merge pull request #36 from oslabs-beta/ctstamper/web-metrics
added CLS web metric
2 parents 9f548bf + 210e39d commit 6cdb5a0

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/app/components/StateRoute/StateRoute.tsx

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { changeView, changeSlider } from '../../RTKslices';
1414
import { useSelector } from 'react-redux';
1515
import PerformanceVisx from './PerformanceVisx/PerformanceVisx';
1616
import WebMetrics from '../WebMetrics';
17-
import { StateRouteProps } from '../../FrontendTypes'
17+
import { StateRouteProps } from '../../FrontendTypes';
1818

1919
/*
2020
Loads the appropriate StateRoute view and renders the Map, Performance, History, Webmetrics, and Tree navbar buttons after clicking on the 'State' button located near the top rightmost corner.
@@ -24,21 +24,21 @@ const History = require('./History').default;
2424
const NO_STATE_MSG = 'No state change detected. Trigger an event to change state'; // message to be returned if there has been no state change detected in our hooked/target app
2525

2626
const StateRoute = (props: StateRouteProps) => {
27-
const {
27+
const {
2828
snapshot, // from 'tabs[currentTab]' object in 'MainContainer'
2929
hierarchy, // from 'tabs[currentTab]' object in 'MainContainer'
3030
snapshots, // from 'tabs[currentTab].snapshotDisplay' object in 'MainContainer'
3131
viewIndex, // from 'tabs[currentTab]' object in 'MainContainer'
3232
webMetrics, // from 'tabs[currentTab]' object in 'MainContainer'
33-
currLocation // from 'tabs[currentTab]' object in 'MainContainer'
33+
currLocation, // from 'tabs[currentTab]' object in 'MainContainer'
3434
} = props;
3535

36-
3736
const { tabs, currentTab } = useSelector((state: any) => state.main);
3837
const { hierarchy, sliderIndex, viewIndex } = tabs[currentTab];
3938

4039
const renderComponentMap = () => {
41-
if (hierarchy) { // if hierarchy was initialized/created render the Map
40+
if (hierarchy) {
41+
// if hierarchy was initialized/created render the Map
4242
return (
4343
<ParentSize className='componentMapContainer'>
4444
{({ width, height }) => (
@@ -55,13 +55,14 @@ const StateRoute = (props: StateRouteProps) => {
5555
return <div className='noState'>{NO_STATE_MSG}</div>; // otherwise, inform the user that there has been no state change in the target/hooked application.
5656
};
5757

58-
const renderHistory:JSX.Element = () => {
59-
if (hierarchy) { // if hierarchy was initialized/created render the history
58+
const renderHistory: JSX.Element = () => {
59+
if (hierarchy) {
60+
// if hierarchy was initialized/created render the history
6061
return (
6162
<ParentSize>
6263
{({ width, height }) => (
6364
<History
64-
width={width}
65+
width={width}
6566
height={height}
6667
hierarchy={hierarchy}
6768
// Commented out dispatch that was prop drilled as conversion to RTK might invalidate need for prop drilling to access dispatch
@@ -79,7 +80,8 @@ const StateRoute = (props: StateRouteProps) => {
7980
};
8081

8182
const renderTree = () => {
82-
if (hierarchy) { // if a hierarchy has been created/initialized, render the appropriate tree based on the active snapshot
83+
if (hierarchy) {
84+
// if a hierarchy has been created/initialized, render the appropriate tree based on the active snapshot
8385
return <Tree snapshot={snapshot} snapshots={snapshots} currLocation={currLocation} />;
8486
}
8587
return <div className='noState'>{NO_STATE_MSG}</div>; // otherwise, inform the user that there has been no state change in the target/hooked application.
@@ -89,8 +91,9 @@ const StateRoute = (props: StateRouteProps) => {
8991
let FIDColor: String;
9092
let FCPColor: String;
9193
let TTFBColor: String;
94+
let CLSColor: String;
9295

93-
// adjust the strings that represent colors of the webmetrics performance bar for 'Largest Contentful Paint (LCP)', 'First Input Delay (FID)', 'First Contentfuly Paint (FCP)', and 'Time to First Byte (TTFB)' based on webMetrics outputs.
96+
// adjust the strings that represent colors of the webmetrics performance bar for 'Largest Contentful Paint (LCP)', 'First Input Delay (FID)', 'First Contentfuly Paint (FCP)', and 'Time to First Byte (TTFB)' based on webMetrics outputs.
9497
if (webMetrics.LCP <= 2000) LCPColor = '#0bce6b';
9598
if (webMetrics.LCP > 2000 && webMetrics.LCP < 4000) LCPColor = '#E56543';
9699
if (webMetrics.LCP > 4000) LCPColor = '#fc2000';
@@ -102,6 +105,10 @@ const StateRoute = (props: StateRouteProps) => {
102105
if (webMetrics.FCP > 1100) FCPColor = '#fc2000';
103106
if (webMetrics.TTFB <= 600) TTFBColor = '#0bce6b';
104107
if (webMetrics.TTFB > 600) TTFBColor = '#fc2000';
108+
if (webMetrics.CLS <= 0.1) TTFBColor = '#0bce6b';
109+
if (webMetrics.CLS > 0.1 && webMetrics.CLS <= 0.25) TTFBColor = '#E56543';
110+
if (webMetrics.CLS > 0.25) TTFBColor = '#fc2000';
111+
console.log('WEBMETRICS YOOO', webMetrics);
105112

106113
return (
107114
<div className='web-metrics-container'>
@@ -139,12 +146,21 @@ const StateRoute = (props: StateRouteProps) => {
139146
name='Time to First Byte'
140147
description='Measures the time it takes for a browser to receive the first byte of page content. The benchmark is 600 ms.'
141148
/>
149+
<WebMetrics
150+
color={CLSColor}
151+
series={webMetrics.CLS * 10}
152+
formatted={(val) => (Number.isNaN(val) ? 'CLS Score: N/A' : `CLS Score: ${val / 10}`)}
153+
label='Cumulative Layout Shift'
154+
name='Cumulative Layout Shift'
155+
description='Calculates the shifting of elements while the page is being downloaded and rendered.'
156+
/>
142157
</div>
143158
);
144159
};
145160

146161
const renderPerfView = () => {
147-
if (hierarchy) { // if hierarchy was initialized/created render the PerformanceVisx
162+
if (hierarchy) {
163+
// if hierarchy was initialized/created render the PerformanceVisx
148164
return (
149165
<ParentSize>
150166
{({ width, height }) => (

src/backend/models/routes.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ export class Routes {
108108
this.current += delta;
109109
// If the difference is not 0, navigate to the target route using window.history.go() method
110110
if (delta !== 0) {
111-
console.log('OS: ', navigator.platform);
112111
window.history.go(delta);
113112
// Return true to indicate that the navigation was successful
114113
return true;

0 commit comments

Comments
 (0)