Skip to content

Commit 62d8b7a

Browse files
committed
imported FCP to front-end
1 parent d89dacb commit 62d8b7a

File tree

8 files changed

+123
-9
lines changed

8 files changed

+123
-9
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
"react-json-tree": "^0.11.2",
158158
"react-router-dom": "^5.2.0",
159159
"react-select": "^3.1.0",
160-
"recoil": "0.0.10"
160+
"recoil": "0.0.10",
161+
"web-vitals": "^1.1.0"
161162
}
162163
}

src/app/components/StateRoute.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { useStoreContext } from '../store';
2020
import PerformanceVisx from './PerformanceVisx';
2121
import Legend from './AtomsRelationshipLegend';
2222
import AtomsRelationship from './AtomsRelationship';
23+
import Vitals from './Vitals';
2324

2425
const History = require('./History').default;
2526
const ErrorHandler = require('./ErrorHandler').default;
@@ -37,14 +38,16 @@ export interface StateRouteProps {
3738
children?: any[];
3839
atomsComponents?: any;
3940
atomSelectors?: any;
41+
4042
};
4143
hierarchy: any;
4244
snapshots: [];
4345
viewIndex: number;
46+
FCP: any;
4447
}
4548

4649
const StateRoute = (props: StateRouteProps) => {
47-
const { snapshot, hierarchy, snapshots, viewIndex } = props;
50+
const { snapshot, hierarchy, snapshots, viewIndex, FCP } = props;
4851
const [{ tabs, currentTab }, dispatch] = useStoreContext();
4952
const { hierarchy, sliderIndex, viewIndex } = tabs[currentTab];
5053
const isRecoil = snapshot.atomsComponents ? true : false;
@@ -111,6 +114,10 @@ const StateRoute = (props: StateRouteProps) => {
111114
return <Tree snapshot={snapshot} />;
112115
}
113116
return <div className="noState">{NO_STATE_MSG}</div>;
117+
118+
};
119+
const renderVitals = () => {
120+
return <Vitals FCP={FCP}/>;
114121
};
115122

116123
const renderPerfView = () => {
@@ -155,6 +162,13 @@ const StateRoute = (props: StateRouteProps) => {
155162
>
156163
History
157164
</NavLink>
165+
<NavLink
166+
className="router-link"
167+
activeClassName="is-active"
168+
to="/vitals"
169+
>
170+
Vitals
171+
</NavLink>
158172
<NavLink
159173
className="router-link"
160174
activeClassName="is-active"
@@ -176,6 +190,7 @@ const StateRoute = (props: StateRouteProps) => {
176190
<Route path="/performance" render={renderPerfView} />
177191
<Route path="/history" render={renderHistory} />
178192
<Route path="/relationship" render={renderAtomsRelationship} />
193+
<Route path="/vitals" render={renderVitals} />
179194
<Route path="/tree" render={renderTree} />
180195
<Route path="/" render={renderComponentMap} />
181196
</Switch>

src/app/components/Vitals.tsx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import React, { useState } from 'react';
2+
3+
4+
const Vitals = ({FCP}) => {
5+
const [data, dataSet] = React.useState({});
6+
const METRICS = ["TTFB", "LCP", "FID", "FCP", "CLS"];
7+
8+
React.useEffect(() => {
9+
chrome.runtime.sendMessage({
10+
type: "performance:metric:request",
11+
}, (d) => {
12+
dataSet(d);
13+
})
14+
}, [])
15+
16+
17+
return (
18+
<div>
19+
<h1>FCP: {FCP.toFixed(3)}</h1>
20+
</div>
21+
)
22+
}
23+
24+
export default Vitals;

src/app/containers/ActionContainer.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,13 @@ function ActionContainer(props) {
115115
);
116116

117117
if (!timeTravel) {
118-
console.log("should be false:", timeTravel)
119118
return (
120119
//returns an empty div when timeTravel is false
121120

122121
<div></div>
123122
)
124123
}
125124
else {
126-
console.log("Should be true:", timeTravel);
127125
// this is not logging; the prop is not being udpdated or the component is not being re-rendered.
128126
return (
129127
<div className="action-container">

src/app/containers/MainContainer.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ function MainContainer(): any {
155155
</div>
156156
);
157157
}
158-
const { viewIndex, sliderIndex, snapshots, hierarchy } = tabs[currentTab];
158+
const { viewIndex, sliderIndex, snapshots, hierarchy, FCP } = tabs[currentTab];
159159
// if viewIndex is -1, then use the sliderIndex instead
160+
console.log('FCP in MainContainer >>>', FCP);
161+
162+
160163
const snapshotView =
161164
viewIndex === -1 ? snapshots[sliderIndex] : snapshots[viewIndex];
162165
// cleaning hierarchy and snapshotView from stateless data
@@ -205,6 +208,7 @@ function MainContainer(): any {
205208
{snapshots.length ? (
206209
<StateContainer
207210
toggleActionContainer={toggleActionContainer}
211+
FCP={FCP}
208212
viewIndex={viewIndex}
209213
snapshot={snapshotDisplay}
210214
hierarchy={hierarchyDisplay}

src/app/containers/StateContainer.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ interface StateContainerProps {
2424
}
2525
>;
2626
toggleActionContainer?: any;
27+
FCP: any;
2728
AtomsRelationship?: any[];
2829
hierarchy: Record<string, unknown>;
2930
snapshots: [];
@@ -33,7 +34,7 @@ interface StateContainerProps {
3334

3435
// eslint-disable-next-line react/prop-types
3536
const StateContainer = (props: StateContainerProps): JSX.Element => {
36-
const { snapshot, hierarchy, snapshots, viewIndex, toggleActionContainer } = props;
37+
const { snapshot, hierarchy, snapshots, viewIndex, toggleActionContainer, FCP } = props;
3738
const [Text, setText] = useState('State');
3839

3940
return (
@@ -74,6 +75,7 @@ const StateContainer = (props: StateContainerProps): JSX.Element => {
7475
setText('State');
7576
return (
7677
<StateRoute
78+
FCP={FCP}
7779
viewIndex={viewIndex}
7880
snapshot={snapshot}
7981
hierarchy={hierarchy}

src/extension/background.js

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const reloaded = {};
66
const firstSnapshotReceived = {};
77
// there will be the same number of objects in here as there are reactime tabs open for each user application being worked on
88
const tabsObj = {};
9+
let data = {}; //for Performance Metrics 8.0
910

1011
function createTabObj(title) {
1112
// update tabsObj
@@ -32,6 +33,8 @@ function createTabObj(title) {
3233
paused: false,
3334
empty: false,
3435
},
36+
// Holds FCP metric
37+
FCP: null,
3538
};
3639
}
3740

@@ -175,17 +178,26 @@ chrome.runtime.onConnect.addListener((port) => {
175178
});
176179

177180
// background.js listening for a message from contentScript.js
178-
chrome.runtime.onMessage.addListener((request, sender) => {
181+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
179182
// IGNORE THE AUTOMATIC MESSAGE SENT BY CHROME WHEN CONTENT SCRIPT IS FIRST LOADED
180183
if (request.type === 'SIGN_CONNECT') {
181184
return true;
182185
}
186+
187+
188+
console.log('Request in Listener >>>', request)
189+
console.log('Sender in Listener >>>', sender)
183190
const tabTitle = sender.tab.title;
184191
const tabId = sender.tab.id;
185-
const { action, index, name } = request;
192+
const { action, index, name, value } = request;
186193
let isReactTimeTravel = false;
187194

188-
195+
if (request.value) {
196+
data = {
197+
name, value
198+
};
199+
console.log("data >>>", data)
200+
}
189201
// Filter out tabs that don't have reactime, tabs that dont use react?
190202
if (
191203
action === 'tabReload' ||
@@ -203,6 +215,29 @@ chrome.runtime.onMessage.addListener((request, sender) => {
203215
tabsObj[tabId] = createTabObj(tabTitle);
204216
}
205217

218+
//Performance Metrics 8.0
219+
220+
// if (request.type === "performance:metric") {
221+
// console.log('performance:metric received');
222+
223+
// const tab = sender.tab.url.toString();
224+
// data[tab] = data[tab] || {};
225+
// const name = request.name;
226+
// data[tab][name] = data[tab][name] || {
227+
// values: [],
228+
// average: 0,
229+
// };
230+
// data[tab][name].values.push(request.value);
231+
// data[tab][name].average =
232+
// data[tab][name].values.reduce((a, v) => a + v, 0) / data[tab][name].values.length;
233+
// }
234+
// if (request.type === "performance:metric:request") {
235+
// console.log('performance:metric:request received')
236+
// console.log('p:m:r', data)
237+
// sendResponse(data);
238+
// }
239+
240+
206241
const { persist, empty } = tabsObj[tabId].mode;
207242

208243
switch (action) {
@@ -236,7 +271,10 @@ chrome.runtime.onMessage.addListener((request, sender) => {
236271
tabsObj[tabId].snapshots = tabsObj[tabId].initialSnapshot;
237272
// resets hierarchy to page initial state recorded when emptied
238273
tabsObj[tabId].hierarchy = tabsObj[tabId].initialHierarchy;
274+
275+
239276
} else {
277+
240278
// triggered with new tab opened
241279
// resets snapshots to page initial state
242280
tabsObj[tabId].snapshots.splice(1);
@@ -276,8 +314,13 @@ chrome.runtime.onMessage.addListener((request, sender) => {
276314
if (!firstSnapshotReceived[tabId]) {
277315
firstSnapshotReceived[tabId] = true;
278316
reloaded[tabId] = false;
317+
318+
if (data.value) tabsObj[tabId].FCP = data.value;
279319

280320
tabsObj[tabId].snapshots.push(request.payload);
321+
322+
// FCP check
323+
console.log('tabsObj >>>', tabsObj);
281324

282325
sendToHierarchy(
283326
tabsObj[tabId],

src/extension/contentScript.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { getTTFB, getLCP, getFID, getFCP, getCLS } from "web-vitals";
2+
3+
14

25
let firstMessage = true;
36

@@ -35,4 +38,28 @@ chrome.runtime.onMessage.addListener(request => {
3538
return true; // attempt to fix port closing console error
3639
});
3740

41+
//Performance Metrics 8.0
42+
43+
const metrics = {};
44+
const gatherMetrics = ({ name, value }) => {
45+
46+
console.log('inside Content Script gather metrics', name, value)
47+
metrics[name] = value;
48+
49+
chrome.runtime.sendMessage({
50+
type: "performance:metric",
51+
name,
52+
value,
53+
});
54+
55+
const metricsHTML = Object.keys(metrics)
56+
}
57+
58+
// getTTFB(gatherMetrics);
59+
// getLCP(gatherMetrics);
60+
// getFID(gatherMetrics);
61+
getFCP(gatherMetrics);
62+
// getCLS(gatherMetrics);
63+
64+
3865
chrome.runtime.sendMessage({ action: 'injectScript' });

0 commit comments

Comments
 (0)