Skip to content

Commit f55d56e

Browse files
committed
added typescript, refactoring with engineering best practices
1 parent bfc2923 commit f55d56e

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const tooltipStyles = {
6868
fontFamily: 'Roboto',
6969
};
7070

71-
const BarGraph = (props: BarGraphProps): unknown => {
71+
const BarGraph = (props: BarGraphProps): JSX.Element => {
7272
const [{ tabs, currentTab }, dispatch] = useStoreContext();
7373
const {
7474
width,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ const tooltipStyles = {
7171
fontFamily: 'Roboto',
7272
};
7373

74-
const BarGraphComparison = (props: BarGraphComparisonProps): unknown => {
74+
const BarGraphComparison = (props: BarGraphComparisonProps): JSX.Element => {
7575
const [{ tabs, currentTab }, dispatch] = useStoreContext();
7676
const {
7777
width, height, data, comparison, setSeries, series, setAction,

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

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
/* eslint-disable no-param-reassign */
12
/* eslint-disable guard-for-in */
23
/* eslint-disable no-restricted-syntax */
3-
// @ts-nocheck
44
import React, { useState, useEffect } from 'react';
55
import {
66
MemoryRouter as Router,
@@ -15,7 +15,7 @@ import BarGraphComparison from './BarGraphComparison';
1515
import BarGraphComparisonActions from './BarGraphComparisonActions';
1616
import { useStoreContext } from '../../../store';
1717
import { setCurrentTabInApp } from '../../../actions/actions';
18-
18+
import { Bar } from '@visx/shape';
1919

2020
interface BarStackProps {
2121
width: number;
@@ -82,8 +82,10 @@ const collectNodes = (snaps, componentName) => {
8282
return finalResults;
8383
};
8484

85+
type currNum = number
86+
8587
/* DATA HANDLING HELPER FUNCTIONS */
86-
const traverse = (snapshot, data, snapshots, currTotalRender = 0) => {
88+
const traverse = (snapshot, data, snapshots, currTotalRender: currNum = 0): void => {
8789
if (!snapshot.children[0]) return;
8890

8991
// loop through snapshots
@@ -95,7 +97,7 @@ const traverse = (snapshot, data, snapshots, currTotalRender = 0) => {
9597
Number.parseFloat(child.componentData.actualDuration).toPrecision(5),
9698
);
9799
// sums render time for all children
98-
currTotalRender += renderTime;
100+
const childrenRenderTime = currTotalRender + renderTime;
99101
// components as keys and set the value to their rendering time
100102
data.barStack[data.barStack.length - 1][componentName] = renderTime;
101103

@@ -120,11 +122,10 @@ const traverse = (snapshot, data, snapshots, currTotalRender = 0) => {
120122
// Get rtid for the hovering feature
121123
data.componentData[componentName].rtid = child.rtid;
122124
data.componentData[componentName].information = collectNodes(snapshots, child.name);
123-
traverse(snapshot.children[idx], data, snapshots, currTotalRender);
125+
traverse(snapshot.children[idx], data, snapshots, childrenRenderTime);
124126
});
125127
// reassigns total render time to max render time
126128
data.maxTotalRender = Math.max(currTotalRender, data.maxTotalRender);
127-
return data;
128129
};
129130

130131
// Retrieve snapshot series data from Chrome's local storage.
@@ -145,9 +146,17 @@ const getSnapshotIds = (obj, snapshotIds = []): string[] => {
145146
return snapshotIds;
146147
};
147148

149+
type BarStackProp = Record <string, unknown>
150+
151+
interface PerfData {
152+
barStack: BarStackProp[],
153+
componentData?: Record<string, unknown>,
154+
maxTotalRender: number,
155+
}
156+
148157
// Returns array of snapshot objs each with components and corresponding render times.
149-
const getPerfMetrics = (snapshots, snapshotsIds): {} => {
150-
const perfData = {
158+
const getPerfMetrics = (snapshots, snapshotsIds): PerfData => {
159+
const perfData: PerfData = {
151160
barStack: [],
152161
componentData: {},
153162
maxTotalRender: 0,
@@ -160,12 +169,12 @@ const getPerfMetrics = (snapshots, snapshotsIds): {} => {
160169
};
161170

162171
/* EXPORT COMPONENT */
163-
const PerformanceVisx = (props: BarStackProps) => {
172+
const PerformanceVisx = (props: BarStackProps): JSX.Element => {
164173
// hook used to dispatch onhover action in rect
165174
const {
166175
width, height, snapshots, hierarchy,
167176
} = props;
168-
const [{ tabs, currentTab, currentTabInApp }, dispatch] = useStoreContext();
177+
const [{ currentTabInApp }, dispatch] = useStoreContext();
169178
const NO_STATE_MSG = 'No state change detected. Trigger an event to change state';
170179
const data = getPerfMetrics(snapshots, getSnapshotIds(hierarchy));
171180
const [series, setSeries] = useState(true);
@@ -178,10 +187,22 @@ const PerformanceVisx = (props: BarStackProps) => {
178187
dispatch(setCurrentTabInApp('performance'));
179188
}, [dispatch]);
180189

190+
type Series = {
191+
data: {
192+
barStack: ActionObj[],
193+
}
194+
name: string
195+
}
196+
197+
type ActionObj = {
198+
name: unknown,
199+
seriesName: unknown,
200+
}
201+
181202
// Creates the actions array used to populate the compare actions dropdown
182203
const getActions = () => {
183-
let seriesArr = localStorage.getItem('project');
184-
seriesArr = seriesArr === null ? [] : JSON.parse(seriesArr);
204+
const project = localStorage.getItem('project');
205+
const seriesArr: Series[] = project === null ? [] : JSON.parse(project);
185206
const actionsArr = [];
186207

187208
if (seriesArr.length) {
@@ -244,13 +265,12 @@ const PerformanceVisx = (props: BarStackProps) => {
244265
data.barStack = filteredSnapshots;
245266
}
246267

247-
268+
// maxheight is referring to the max height in render time to choose the scaling size for graph
269+
let maxHeight = 0;
248270
if (snapshot !== 'All Snapshots') {
249271
// filter barStack to make it equal to an array of length 1 with object matching snapshot ID to mirror the data.barStack object's shape
250272
const checkData = [data.barStack.find(comp => comp.snapshotId === snapshot)];
251273
const holdData = [];
252-
// maxheight is referring to the max height in render time to choose the scaling size for graph
253-
let maxHeight = 0;
254274
// looping through checkData which is composed of a single snapshot while pushing key/values to a new object and setting maxHeight
255275
for (const key in checkData[0]) {
256276
if (key !== 'route' && key !== 'snapshotId') {

0 commit comments

Comments
 (0)