Skip to content

Commit fb681ba

Browse files
committed
added null checks for checkDiff in history tree
1 parent a01d644 commit fb681ba

File tree

2 files changed

+50
-54
lines changed

2 files changed

+50
-54
lines changed

src/app/components/StateRoute/History.tsx

Lines changed: 43 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,87 +46,79 @@ function History(props: Record<string, unknown>): JSX.Element {
4646
const innerHeight: number = totalHeight - margin.top - margin.bottom - 60;
4747

4848
function findDiff(index) {
49-
// determines the difference between our current index and the index-1 snapshot and produces an html string
5049
const statelessCleaning = (obj: {
51-
//'statelessCleaning' functions in the same way as the 'statelessCleaning' function in Diff.tsx
5250
name?: string;
5351
componentData?: object;
5452
state?: string | any;
5553
stateSnaphot?: object;
5654
children?: any[];
5755
}) => {
58-
const newObj = { ...obj }; // duplicate our input object into a new object
56+
if (!obj) return {}; // Add null check
5957

60-
if (newObj.name === 'nameless') {
61-
// if our new object's name is nameless
62-
delete newObj.name; // delete the name property
63-
}
64-
if (newObj.componentData) {
65-
// if our new object has a componentData property
66-
delete newObj.componentData; // delete the componentData property
67-
}
68-
if (newObj.state === 'stateless') {
69-
// if if our new object's state is stateless
70-
delete newObj.state; // delete the state property
71-
}
58+
const newObj = { ...obj };
59+
if (newObj.name === 'nameless') delete newObj.name;
60+
if (newObj.componentData) delete newObj.componentData;
61+
if (newObj.state === 'stateless') delete newObj.state;
7262
if (newObj.stateSnaphot) {
73-
// if our new object has a stateSnaphot propertys
74-
newObj.stateSnaphot = statelessCleaning(obj.stateSnaphot); // run statelessCleaning on the stateSnapshot
63+
newObj.stateSnaphot = statelessCleaning(obj.stateSnaphot);
7564
}
76-
7765
if (newObj.children) {
78-
// if our new object has a children property
7966
newObj.children = [];
80-
if (obj.children.length > 0) {
81-
// and if our input object's children property is non-empty, go through each children object from our input object and determine, if the object being iterated on either has a stateless state or has a children array with a non-zero amount of objects. Objects that fulfill the above that need to be cleaned through statelessCleaning. Those that are cleaned through this process are then pushed to the new object's children array.
67+
// Add null check for children array
68+
if (Array.isArray(obj.children) && obj.children.length > 0) {
8269
obj.children.forEach((element: { state?: object | string; children?: [] }) => {
83-
if (element.state !== 'stateless' || element.children.length > 0) {
70+
// Add null check for element
71+
if (
72+
element &&
73+
((element.state && element.state !== 'stateless') ||
74+
(element.children && element.children.length > 0))
75+
) {
8476
const clean = statelessCleaning(element);
8577
newObj.children.push(clean);
8678
}
8779
});
8880
}
8981
}
90-
return newObj; // return the cleaned state snapshot(s)
82+
return newObj;
9183
};
9284

9385
function findStateChangeObj(delta, changedState = []) {
94-
// function determines whether delta has resulted in a changedState. Function would return an empty array if there were no changes to state and an array of objects that changed state.
95-
if (!delta.children && !delta.state) {
96-
// if delta doesn't have a children property or a state property
97-
return changedState; // returns an empty array
98-
}
99-
86+
if (!delta) return changedState; // Add null check
87+
if (!delta.children && !delta.state) return changedState;
10088
if (delta.state && delta.state[0] !== 'stateless') {
101-
// ignore stateless delta objects
102-
changedState.push(delta.state); // and push stateful delta objects to changedState
103-
}
104-
105-
if (!delta.children) {
106-
// if the delta doesn't have any children
107-
return changedState; // return the changedState array with any and all stateful delta objects.
89+
changedState.push(delta.state);
10890
}
109-
91+
if (!delta.children) return changedState;
11092
Object.keys(delta.children).forEach((child) => {
111-
// but if the delta object did have children, we iterate through each child object
112-
// if (isNaN(child) === false) {
113-
changedState.push(...findStateChangeObj(delta.children[child])); // recursively call this function on each child object. Push every 'stateful' child into the changedState array.
114-
// }
93+
if (delta.children[child]) {
94+
// Add null check
95+
changedState.push(...findStateChangeObj(delta.children[child]));
96+
}
11597
});
98+
return changedState;
99+
}
100+
101+
if (index === 0) return 'Initial State';
116102

117-
return changedState; // return the changedState array with any and all stateful delta objects.
103+
// Add null checks for snapshots
104+
if (!snapshots || !snapshots[index] || !snapshots[index - 1]) {
105+
return 'No state changes';
118106
}
119107

120-
if (index === 0) return ''; // Return empty string for initial state
108+
try {
109+
const delta = diff(
110+
statelessCleaning(snapshots[index - 1]),
111+
statelessCleaning(snapshots[index]),
112+
);
113+
114+
if (!delta) return 'No state changes';
121115

122-
const delta = diff(
123-
// 'diff' function from 'jsondiffpatch' returns the difference in state between the (snapshot that occurred before the indexed snapshot) and the (indexed snapshot).
124-
statelessCleaning(snapshots[index - 1]),
125-
statelessCleaning(snapshots[index]),
126-
);
127-
const changedState = findStateChangeObj(delta); // determines if delta had any stateful changes
128-
const html = formatters.html.format(changedState[0]); // formats the difference into html string
129-
return html; // return html string
116+
const changedState = findStateChangeObj(delta);
117+
return changedState.length > 0 ? formatters.html.format(changedState[0]) : 'No state changes';
118+
} catch (error) {
119+
console.error('Error in findDiff:', error);
120+
return 'Error comparing states';
121+
}
130122
}
131123

132124
/**

src/app/components/StateRoute/PerformanceVisx/BarGraph.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,24 @@ const BarGraph = (props: BarGraphProps): JSX.Element => {
138138
>
139139
<option>All Routes</option>
140140
{allRoutes.map((route) => (
141-
<option className='routes'>{route}</option>
141+
<option key={route} className='routes'>
142+
{route}
143+
</option>
142144
))}
143145
</select>
144146
</form>
145147
<form className='routesForm' id='routes-formcontrol'>
146148
<label id='routes-dropdown'>Snapshot: </label>
147149
<select
148-
labelid='demo-simple-select-label'
150+
labelId='demo-simple-select-label'
149151
id='snapshot-select'
150152
onChange={(e) => setSnapshot(e.target.value)}
151153
>
152154
<option value='All Snapshots'>All Snapshots</option>
153155
{filteredSnapshots.map((route) => (
154-
<option className='routes'>{route.snapshotId}</option>
156+
<option key={route.snapshotId} className='routes'>
157+
{route.snapshotId}
158+
</option>
155159
))}
156160
</select>
157161
</form>

0 commit comments

Comments
 (0)