Skip to content

Commit 8ba57c7

Browse files
(updated) traverse function to check for component types; tooltip now displays corresponding component types for react apps
1 parent e4c4e34 commit 8ba57c7

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/app/components/PerformanceVisx.tsx

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ const getPerfMetrics = (snapshots, snapshotsIds) => {
5050
}, [])
5151
}
5252

53-
// traverse the snapshot for all components and their rendering time
54-
const traverse = (snapshot, perfSnapshot) => {
53+
// traverse can either return all component rendering times or type of state component depending if 2nd argument is passed
54+
const traverse = (snapshot, data = {}) => {
5555
if (!snapshot.children[0]) return
5656
for (let i = 0; i < snapshot.children.length; i++){
57-
// perfSnapshot.state = snapshot.children[i].state
58-
if (snapshot.children[i].componentData.actualDuration){
59-
const renderTime = Number(Number.parseFloat(snapshot.children[i].componentData.actualDuration).toPrecision(5))
60-
perfSnapshot[snapshot.children[i].name+-[i+1]] = renderTime
57+
const componentName = snapshot.children[i].name+-[i+1]
58+
if (!data.snapshotId){
59+
if(snapshot.children[i].state !== 'stateless') data[componentName] = 'STATEFUL'
60+
else data[componentName] = snapshot.children[i].state;
61+
}
62+
else if (snapshot.children[i].componentData.actualDuration){
63+
const renderTime = Number(Number.parseFloat(snapshot.children[i].componentData.actualDuration).toPrecision(5));
64+
data[componentName] = renderTime;
6165
}
62-
traverse(snapshot.children[i], perfSnapshot)
66+
traverse(snapshot.children[i], data)
6367
}
64-
return perfSnapshot
68+
return data
6569
}
6670

6771
const getSnapshotIds = (obj, snapshotIds = []) => {
@@ -92,7 +96,7 @@ export default function PerformanceVisx({
9296
tooltipLeft,
9397
tooltipTop,
9498
tooltipData,
95-
hideTooltip,
99+
hideTooltip,ƒ
96100
showTooltip
97101
} = useTooltip<TooltipData>();
98102

@@ -101,8 +105,12 @@ let tooltipTimeout: number;
101105
// filter and structure incoming data for VISX
102106
const data = getPerfMetrics(snapshots, getSnapshotIds(hierarchy))
103107
const keys = Object.keys(data[0]).filter((d) => d !== "snapshotId") as CityName[];
108+
const allComponentStates = traverse(snapshots[0])
109+
110+
console.log(keys)
104111
console.log('data', data)
105112
console.log('snapshots', snapshots)
113+
console.log(allComponentStates)
106114

107115
// create array of total render times for each snapshot
108116
const totalRenderArr = data.reduce((totalRender, curSnapshot) => {
@@ -280,6 +288,9 @@ console.log('height', height)
280288
<div style={{ color: colorScale(tooltipData.key) }}>
281289
<strong>{tooltipData.key}</strong>
282290
</div>
291+
292+
<div>{allComponentStates[tooltipData.key]}</div>
293+
283294
<div>
284295
{formatRenderTime(tooltipData.bar.data[tooltipData.key])}
285296
</div>

0 commit comments

Comments
 (0)