Skip to content

Commit 336d5f7

Browse files
pseudocode added to Diff, DiffRoute, History, StateRoute, StateContainer, and store.
1 parent 5e11d6f commit 336d5f7

File tree

7 files changed

+106
-74
lines changed

7 files changed

+106
-74
lines changed

src/app/components/Diff.tsx

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,62 @@ import { useStoreContext } from '../store';
55
import { DiffProps, StatelessCleaning } from '../FrontendTypes';
66

77
/**
8-
* Displays tree showing specific two versions of tree
8+
* Displays tree showing two specific versions of tree:
99
* one with specific state changes, the other the whole tree
1010
* @param props props from maincontainer
1111
* @returns a diff tree or a string stating no state changes have happened
1212
*/
13+
1314
function Diff(props: DiffProps): JSX.Element {
14-
const { snapshot, show } = props;
15-
const [mainState] = useStoreContext();
16-
const { currentTab, tabs } = mainState; // k/v pairs of mainstate store object being created
15+
const {
16+
snapshot, // snapshot from 'tabs[currentTab]' object in 'MainContainer'
17+
show // boolean that is dependent on the 'Route' path
18+
} = props;
19+
const [mainState] = useStoreContext(); // useStoreContext() returns our global state object (which was initialized as 'initialState' in 'App.tsx')
20+
const { currentTab, tabs } = mainState; // 'currentTab' (type: number) and 'tabs' (type: object) are destructured from 'mainState'
1721
const { snapshots, viewIndex, sliderIndex } = tabs[currentTab];
18-
let previous: unknown;
1922

20-
// previous follows viewIndex or sliderIndex
21-
if (viewIndex !== -1) {
22-
// if tab isnt selected, view index is set to -1
23-
previous = snapshots[viewIndex - 1];
23+
let previous: unknown// = (viewIndex !== -1) ? snapshots[viewIndex - 1] : previous = snapshots[sliderIndex - 1]
24+
25+
if (viewIndex !== -1) { // snapshots should not have any property < 0. A viewIndex of '-1' means that we had a snapshot index that occurred before the initial snapshot of the application state... which is impossible. '-1' therefore means reset to the last/most recent snapshot.
26+
previous = snapshots[viewIndex - 1]; // set previous to the snapshot that is before the one we are currently viewing
2427
} else {
25-
previous = snapshots[sliderIndex - 1];
28+
previous = snapshots[sliderIndex - 1]; // if viewIndex was an impossible index, we will get our snapshots index using 'sliderIndex.' sliderIndex should have already been reset to the latest snapshot index. Previous is then set to the snapshot that occurred immediately before our most recent snapshot.
2629
}
30+
31+
/*
32+
State snapshot objects have the following structure:
33+
{
34+
children: array of objects
35+
componentData: object
36+
isExpanded: Boolean
37+
name: string
38+
route: object
39+
state: string
40+
}
2741
28-
// cleaning preview from stateless data
42+
// cleaning preview from stateless data
43+
*/
2944
const statelessCleaning = (obj: StatelessCleaning) => {
30-
const newObj = { ...obj };
31-
if (newObj.name === 'nameless') {
32-
delete newObj.name;
45+
const newObj = { ...obj }; // duplicate our input object into a new object
46+
47+
if (newObj.name === 'nameless') { // if our new object's name is nameless
48+
delete newObj.name; // delete the name property
3349
}
34-
if (newObj.componentData) {
35-
delete newObj.componentData;
50+
if (newObj.componentData) { // if our new object has a componentData property
51+
delete newObj.componentData; // delete the componentData property
3652
}
37-
if (newObj.state === 'stateless') {
38-
delete newObj.state;
53+
if (newObj.state === 'stateless') { // if if our new object's state is stateless
54+
delete newObj.state; // delete the state property
3955
}
40-
if (newObj.stateSnaphot) {
41-
newObj.stateSnaphot = statelessCleaning(obj.stateSnaphot);
56+
57+
if (newObj.stateSnaphot) { // if our new object has a stateSnaphot property
58+
newObj.stateSnaphot = statelessCleaning(obj.stateSnaphot); // run statelessCleaning on the stateSnapshot
4259
}
43-
if (newObj.children) {
60+
61+
if (newObj.children) { // if our new object has a children property
4462
newObj.children = [];
45-
if (obj.children.length > 0) {
63+
if (obj.children.length > 0) { // and if our input object's children property is non-empty, go through each children object and determine objects that need to be cleaned through statelessCleaning. Those that are cleaned through this process are then pushed to the new object's children array.
4664
obj.children.forEach(
4765
(element: { state?: Record<string, unknown> | string; children?: [] }) => {
4866
if (element.state !== 'stateless' || element.children.length > 0) {
@@ -53,14 +71,15 @@ function Diff(props: DiffProps): JSX.Element {
5371
);
5472
}
5573
}
56-
return newObj;
74+
return newObj; // return the cleaned state snapshot(s)
5775
};
5876

59-
// displays stateful data
60-
const previousDisplay: StatelessCleaning = statelessCleaning(previous);
77+
const previousDisplay: StatelessCleaning = statelessCleaning(previous); // displays stateful data
78+
6179
// diff function returns a comparison of two objects, one has an updated change
6280
// just displays stateful data
6381
const delta: StatelessCleaning = diff(previousDisplay, snapshot);
82+
6483
// returns html in string
6584
// just displays stateful data
6685
const html: StatelessCleaning = formatters.html.format(delta, previousDisplay);

src/app/components/DiffRoute.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { MemoryRouter as Router, Route, NavLink, Switch } from 'react-router-dom
33
import Diff from './Diff';
44
import { DiffRouteProps } from '../FrontendTypes';
55

6+
/*
7+
Loads the appropriate DiffRoute view and renders the 'Tree' and 'Raw' navbar buttons after clicking on the 'Diff' button located near the top rightmost corner.
8+
*/
9+
10+
// 'DiffRoute' only passed in prop is 'snapshot' from 'tabs[currentTab]' object in 'MainContainer'
611
const DiffRoute = (props: DiffRouteProps): JSX.Element => (
712
<Router>
8-
<div className='navbar'>
13+
<div className='navbar'>
914
<NavLink className='router-link' activeClassName='is-active' exact to='/'>
1015
Tree
1116
</NavLink>

src/app/components/StateRoute/History.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ function History(props: Record<string, unknown>): JSX.Element {
2323
width: totalWidth,
2424
height: totalHeight,
2525
margin = defaultMargin,
26-
hierarchy,
27-
dispatch,
28-
currLocation,
29-
snapshots,
26+
hierarchy, // from 'tabs[currentTab]' object in 'MainContainer'
27+
dispatch, // from useStoreContext in 'StateRoute'
28+
currLocation, // from 'tabs[currentTab]' object in 'MainContainer'
29+
snapshots, // from 'tabs[currentTab].snapshotDisplay' object in 'MainContainer'
3030
} = props;
3131
const [, dispatch] = useStoreContext();
3232

src/app/components/StateRoute/StateRoute.tsx

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,27 @@ import PerformanceVisx from './PerformanceVisx/PerformanceVisx';
1616
import WebMetrics from '../WebMetrics';
1717
import { StateRouteProps } from '../../FrontendTypes'
1818

19-
const History = require('./History').default;
20-
21-
const NO_STATE_MSG = 'No state change detected. Trigger an event to change state';
19+
/*
20+
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.
21+
*/
2222

23+
const History = require('./History').default;
24+
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
2325

2426
const StateRoute = (props: StateRouteProps) => {
25-
const { snapshot, hierarchy, snapshots, viewIndex, webMetrics, currLocation } = props;
27+
const {
28+
snapshot, // from 'tabs[currentTab]' object in 'MainContainer'
29+
hierarchy, // from 'tabs[currentTab]' object in 'MainContainer'
30+
snapshots, // from 'tabs[currentTab].snapshotDisplay' object in 'MainContainer'
31+
viewIndex, // from 'tabs[currentTab]' object in 'MainContainer'
32+
webMetrics, // from 'tabs[currentTab]' object in 'MainContainer'
33+
currLocation // from 'tabs[currentTab]' object in 'MainContainer'
34+
} = props;
2635
const [{ tabs, currentTab }, dispatch] = useStoreContext();
2736
const { hierarchy, sliderIndex, viewIndex } = tabs[currentTab];
2837

29-
// Map
3038
const renderComponentMap = () => {
31-
if (hierarchy) {
39+
if (hierarchy) { // if hierarchy was initialized/created render the Map
3240
return (
3341
<ParentSize className='componentMapContainer'>
3442
{({ width, height }) => (
@@ -42,16 +50,11 @@ const StateRoute = (props: StateRouteProps) => {
4250
</ParentSize>
4351
);
4452
}
45-
return <div className='noState'>{NO_STATE_MSG}</div>;
53+
return <div className='noState'>{NO_STATE_MSG}</div>; // otherwise, inform the user that there has been no state change in the target/hooked application.
4654
};
4755

48-
// the hierarchy gets set upon the first click on the page
49-
// when the page is refreshed we may not have a hierarchy, so we need to check if hierarchy was initialized
50-
// if true, we invoke the D3 render chart with hierarchy
51-
// by invoking History component, and passing in all the props required to render D3 elements and perform timeJump from clicking of node
52-
// otherwise we send an alert to the user that no state was found.
5356
const renderHistory:JSX.Element = () => {
54-
if (hierarchy) {
57+
if (hierarchy) { // if hierarchy was initialized/created render the history
5558
return (
5659
<ParentSize>
5760
{({ width, height }) => (
@@ -69,24 +72,22 @@ const StateRoute = (props: StateRouteProps) => {
6972
</ParentSize>
7073
);
7174
}
72-
return <div className='noState'>{NO_STATE_MSG}</div>;
75+
return <div className='noState'>{NO_STATE_MSG}</div>; // otherwise, inform the user that there has been no state change in the target/hooked application.
7376
};
7477

75-
// the hierarchy gets set on the first click in the page
76-
// when the page is refreshed we may not have a hierarchy, so we need to check if hierarchy was initialized
77-
// if true invoke render Tree with snapshot
7878
const renderTree = () => {
79-
if (hierarchy) {
79+
if (hierarchy) { // if a hierarchy has been created/initialized, render the appropriate tree based on the active snapshot
8080
return <Tree snapshot={snapshot} snapshots={snapshots} currLocation={currLocation} />;
8181
}
82-
return <div className='noState'>{NO_STATE_MSG}</div>;
82+
return <div className='noState'>{NO_STATE_MSG}</div>; // otherwise, inform the user that there has been no state change in the target/hooked application.
8383
};
8484
const renderWebMetrics = () => {
8585
let LCPColor: String;
8686
let FIDColor: String;
8787
let FCPColor: String;
8888
let TTFBColor: String;
8989

90+
// 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.
9091
if (webMetrics.LCP <= 2000) LCPColor = '#0bce6b';
9192
if (webMetrics.LCP > 2000 && webMetrics.LCP < 4000) LCPColor = '#E56543';
9293
if (webMetrics.LCP > 4000) LCPColor = '#fc2000';
@@ -140,7 +141,7 @@ const StateRoute = (props: StateRouteProps) => {
140141
};
141142

142143
const renderPerfView = () => {
143-
if (hierarchy) {
144+
if (hierarchy) { // if hierarchy was initialized/created render the PerformanceVisx
144145
return (
145146
<ParentSize>
146147
{({ width, height }) => (
@@ -156,7 +157,7 @@ const StateRoute = (props: StateRouteProps) => {
156157
</ParentSize>
157158
);
158159
}
159-
return <div className='noState'>{NO_STATE_MSG}</div>;
160+
return <div className='noState'>{NO_STATE_MSG}</div>; // otherwise, inform the user that there has been no state change in the target/hooked application.
160161
};
161162

162163
return (

src/app/containers/StateContainer.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import { StateContainerProps } from '../FrontendTypes';
1111
// eslint-disable-next-line react/prop-types
1212
const StateContainer = (props: StateContainerProps): JSX.Element => {
1313
const {
14-
snapshot, // from 'tabs[currentTab]' object from 'MainContainer'
15-
hierarchy, // from 'tabs[currentTab]' object from 'MainContainer'
16-
snapshots, // from 'tabs[currentTab].snapshotDisplay' object from 'MainContainer'
17-
viewIndex, // from 'tabs[currentTab]' object from 'MainContainer'
18-
webMetrics, // from 'tabs[currentTab]' object from 'MainContainer'
19-
currLocation // from 'tabs[currentTab]' object from 'MainContainer'
14+
snapshot, // from 'tabs[currentTab]' object in 'MainContainer'
15+
hierarchy, // from 'tabs[currentTab]' object in 'MainContainer'
16+
snapshots, // from 'tabs[currentTab].snapshotDisplay' object in 'MainContainer'
17+
viewIndex, // from 'tabs[currentTab]' object in 'MainContainer'
18+
webMetrics, // from 'tabs[currentTab]' object in 'MainContainer'
19+
currLocation // from 'tabs[currentTab]' object in 'MainContainer'
2020
} = props;
2121

2222
return (

0 commit comments

Comments
 (0)