Skip to content

Commit ea99f92

Browse files
author
Kevin Ngo
committed
Add base functionality for Save Series button
1 parent 466d453 commit ea99f92

File tree

8 files changed

+505
-408
lines changed

8 files changed

+505
-408
lines changed

src/app/actions/actions.ts

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
22
import * as types from '../constants/actionTypes';
33

4-
export const toggleMode = mode => ({
4+
export const save = (tabsObj) => ({
5+
type: types.SAVE,
6+
payload: tabsObj,
7+
});
8+
9+
export const toggleMode = (mode) => ({
510
type: types.TOGGLE_MODE,
611
payload: mode,
712
});
813

9-
export const addNewSnapshots = tabsObj => ({
14+
export const addNewSnapshots = (tabsObj) => ({
1015
type: types.NEW_SNAPSHOTS,
1116
payload: tabsObj,
1217
});
1318

14-
export const initialConnect = tabsObj => ({
19+
export const initialConnect = (tabsObj) => ({
1520
type: types.INITIAL_CONNECT,
1621
payload: tabsObj,
1722
});
1823

19-
export const setPort = port => ({
24+
export const setPort = (port) => ({
2025
type: types.SET_PORT,
2126
payload: port,
2227
});
@@ -25,12 +30,12 @@ export const emptySnapshots = () => ({
2530
type: types.EMPTY,
2631
});
2732

28-
export const changeView = index => ({
33+
export const changeView = (index) => ({
2934
type: types.CHANGE_VIEW,
3035
payload: index,
3136
});
3237

33-
export const changeSlider = index => ({
38+
export const changeSlider = (index) => ({
3439
type: types.CHANGE_SLIDER,
3540
payload: index,
3641
});
@@ -54,22 +59,22 @@ export const pause = () => ({
5459
type: types.PAUSE,
5560
});
5661

57-
export const startPlaying = intervalId => ({
62+
export const startPlaying = (intervalId) => ({
5863
type: types.PLAY,
5964
payload: intervalId,
6065
});
6166

62-
export const importSnapshots = newSnaps => ({
67+
export const importSnapshots = (newSnaps) => ({
6368
type: types.IMPORT,
6469
payload: newSnaps,
6570
});
6671

67-
export const setTab = tab => ({
72+
export const setTab = (tab) => ({
6873
type: types.SET_TAB,
6974
payload: tab,
7075
});
7176

72-
export const deleteTab = tab => ({
77+
export const deleteTab = (tab) => ({
7378
type: types.DELETE_TAB,
7479
payload: tab,
7580
});
@@ -79,12 +84,12 @@ export const resetSlider = () => ({
7984
});
8085

8186
export const onHover = (rtid) => ({
82-
type: types.ON_HOVER,
87+
type: types.ON_HOVER,
8388
//the payload should be something to relate the component we're hovering and highlight that component on the DOM
84-
payload: rtid
85-
})
89+
payload: rtid,
90+
});
8691

8792
export const onHoverExit = (rtid) => ({
8893
type: types.ON_HOVER_EXIT,
89-
payload: rtid
90-
})
94+
payload: rtid,
95+
});

src/app/components/PerformanceVisx.tsx

Lines changed: 59 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import RenderingFrequency from './RenderingFrequency';
44
import FormControlLabel from '@material-ui/core/FormControlLabel';
55
import Switch from '@material-ui/core/Switch';
66
import BarGraph from './BarGraph';
7+
import { save } from '../actions/actions';
8+
import { useStoreContext } from '../store';
79

810
/* NOTES
911
Issue - Not fully compatible with recoil apps. Reference the recoil-todo-test.
@@ -26,39 +28,42 @@ interface BarStackProps {
2628
/* DATA HANDLING HELPER FUNCTIONS */
2729
const traverse = (snapshot, data, currTotalRender = 0) => {
2830
if (!snapshot.children[0]) return;
29-
31+
console.log('data in PerformanceVisx >>>', data);
3032
// loop through snapshots
3133
snapshot.children.forEach((child, idx) => {
3234
const componentName = child.name + -[idx + 1];
3335

3436
// Get component Rendering Time
35-
const renderTime = Number(Number.parseFloat(child.componentData.actualDuration).toPrecision(5));
37+
const renderTime = Number(
38+
Number.parseFloat(child.componentData.actualDuration).toPrecision(5)
39+
);
3640
// sums render time for all children
3741
currTotalRender += renderTime;
38-
// components as keys and set the value to their rendering time
39-
data['barStack'][data.barStack.length - 1][componentName] = renderTime;
40-
42+
// components as keys and set the value to their rendering time
43+
data['barStack'][data.barStack.length - 1][componentName] = renderTime;
44+
4145
// Get component stateType
4246
if (!data.componentData[componentName]) {
4347
data.componentData[componentName] = {
4448
stateType: 'stateless',
4549
renderFrequency: 0,
4650
totalRenderTime: 0,
47-
rtid: ''
51+
rtid: '',
4852
};
49-
if (child.state !== 'stateless') data.componentData[componentName].stateType = 'stateful';
53+
if (child.state !== 'stateless')
54+
data.componentData[componentName].stateType = 'stateful';
5055
}
5156
// increment render frequencies
5257
if (renderTime > 0) {
5358
data.componentData[componentName].renderFrequency++;
5459
}
5560

56-
// add to total render time
61+
// add to total render time
5762
data.componentData[componentName].totalRenderTime += renderTime;
58-
// Get rtid for the hovering feature
63+
// Get rtid for the hovering feature
5964
data.componentData[componentName].rtid = child.rtid;
6065
traverse(snapshot.children[idx], data, currTotalRender);
61-
})
66+
});
6267
// reassigns total render time to max render time
6368
data.maxTotalRender = Math.max(currTotalRender, data.maxTotalRender);
6469
return data;
@@ -67,7 +72,7 @@ const traverse = (snapshot, data, currTotalRender = 0) => {
6772
const getSnapshotIds = (obj, snapshotIds = []): string[] => {
6873
snapshotIds.push(`${obj.name}.${obj.branch}`);
6974
if (obj.children) {
70-
obj.children.forEach(child => {
75+
obj.children.forEach((child) => {
7176
getSnapshotIds(child, snapshotIds);
7277
});
7378
}
@@ -82,44 +87,66 @@ const getPerfMetrics = (snapshots, snapshotsIds): {} => {
8287
maxTotalRender: 0,
8388
};
8489
snapshots.forEach((snapshot, i) => {
85-
perfData.barStack.push({snapshotId: snapshotsIds[i]});
90+
perfData.barStack.push({ snapshotId: snapshotsIds[i] });
8691
traverse(snapshot, perfData);
8792
});
8893
return perfData;
8994
};
90-
95+
96+
// interface saveData {
97+
// snapshots: [];
98+
// }
99+
91100
/* EXPORT COMPONENT */
92101
const PerformanceVisx = (props: BarStackProps) => {
93102
// hook used to dispatch onhover action in rect
94103
const { width, height, snapshots, hierarchy } = props;
95104

105+
// const [dispatch] = useStoreContext();
106+
const [{ tabs, currentTab }, dispatch] = useStoreContext();
107+
108+
console.log('tabs',tabs)
109+
console.log('currentTabs', currentTab)
110+
96111
const [isToggled, setIsToggled] = useState('barStack');
97112
const toggleView = () => {
98-
isToggled === 'frequencyCards' ? setIsToggled('barStack') : setIsToggled('frequencyCards');
99-
}
113+
isToggled === 'frequencyCards'
114+
? setIsToggled('barStack')
115+
: setIsToggled('frequencyCards');
116+
};
117+
100118
// filter and structure incoming data for VISX
101119
const data = getPerfMetrics(snapshots, getSnapshotIds(hierarchy));
102-
103-
// if performance tab is too small it will not return VISX component
104-
return (
105-
<div className='renderTab'>
106-
<FormControlLabel style={{"margin-left":"30px", "margin-top": "0px"}}
120+
121+
const toStorage = {
122+
currentTab,
123+
title: tabs[currentTab]['title'],
124+
data,
125+
}
126+
127+
// Extract individual data from chrome.locals.storage and visualize
128+
// Need to setup dropdown menu . Fill dropdown with tabsID (sessions)
129+
// When you select dropdown, change view with ReactRouter
130+
// have side-by-side comparison with visx bargraphs in alternate view
131+
132+
// if performance tab is too small it will not return VISX component
133+
return (
134+
<div className="renderTab">
135+
<FormControlLabel
136+
style={{ 'margin-left': '30px', 'margin-top': '0px' }}
107137
control={
108-
<Switch
109-
onChange={toggleView}
110-
name="checkedB"
111-
color="primary"
112-
/>
138+
<Switch onChange={toggleView} name="checkedB" color="primary" />
113139
}
114140
label="Component Details"
115141
/>
116-
{/* <button onClick={toggleView}>Toggle Button</button> */}
117-
<div style={{"display": "flex", "justify-content": "center"}}>
118-
{isToggled === 'frequencyCards'
119-
? <RenderingFrequency data={data.componentData}/>
120-
: <BarGraph data={data} width={width} height={height}/>
121-
}
122-
</div>
142+
<button onClick={() => dispatch(save(toStorage))}>Save Series</button>
143+
<div style={{ display: 'flex', 'justify-content': 'center' }}>
144+
{isToggled === 'frequencyCards' ? (
145+
<RenderingFrequency data={data.componentData} />
146+
) : (
147+
<BarGraph data={data} width={width} height={height} />
148+
)}
149+
</div>
123150
</div>
124151
);
125152
};

src/app/constants/actionTypes.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export const NEW_SNAPSHOTS = 'NEW_SNAPSHOTS';
1313
export const SET_TAB = 'SET_TAB';
1414
export const DELETE_TAB = 'DELETE_TAB';
1515
export const SLIDER_ZERO = 'SLIDER_ZERO';
16-
export const ON_HOVER = 'ON_HOVER';
17-
export const ON_HOVER_EXIT = 'ON_HOVER_EXIT';
16+
export const ON_HOVER = 'ON_HOVER';
17+
export const ON_HOVER_EXIT = 'ON_HOVER_EXIT';
18+
export const SAVE = 'SAVE';

src/app/containers/ActionContainer.tsx

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,45 @@ import { useStoreContext } from '../store';
99

1010
const resetSlider = () => {
1111
const slider = document.querySelector('.rc-slider-handle');
12-
if (slider) { slider.setAttribute('style', 'left: 0'); }
12+
if (slider) {
13+
slider.setAttribute('style', 'left: 0');
14+
}
1315
};
1416

1517
function ActionContainer() {
1618
const [{ tabs, currentTab }, dispatch] = useStoreContext();
1719
const { hierarchy, sliderIndex, viewIndex } = tabs[currentTab];
1820
let actionsArr = [];
19-
const hierarchyArr:any[] = [];
21+
const hierarchyArr: any[] = [];
2022

2123
// function to traverse state from hiararchy and also getting information on display name and component name
22-
const displayArray = (obj:{stateSnapshot:{children:any[]}, name:number, branch:number, index:number, children?:[]}) => {
23-
if (obj.stateSnapshot.children.length > 0 && obj.stateSnapshot.children[0] && obj.stateSnapshot.children[0].state && obj.stateSnapshot.children[0].name) {
24-
const newObj:Record<string, unknown> = {
24+
const displayArray = (obj: {
25+
stateSnapshot: { children: any[] };
26+
name: number;
27+
branch: number;
28+
index: number;
29+
children?: [];
30+
}) => {
31+
if (
32+
obj.stateSnapshot.children.length > 0 &&
33+
obj.stateSnapshot.children[0] &&
34+
obj.stateSnapshot.children[0].state &&
35+
obj.stateSnapshot.children[0].name
36+
) {
37+
const newObj: Record<string, unknown> = {
2538
index: obj.index,
2639
displayName: `${obj.name}.${obj.branch}`,
2740
state: obj.stateSnapshot.children[0].state,
2841
componentName: obj.stateSnapshot.children[0].name,
29-
componentData: JSON.stringify(obj.stateSnapshot.children[0].componentData) === '{}' ? '' : obj.stateSnapshot.children[0].componentData,
42+
componentData:
43+
JSON.stringify(obj.stateSnapshot.children[0].componentData) === '{}'
44+
? ''
45+
: obj.stateSnapshot.children[0].componentData,
3046
};
3147
hierarchyArr.push(newObj);
3248
}
3349
if (obj.children) {
34-
obj.children.forEach(element => {
50+
obj.children.forEach((element) => {
3551
displayArray(element);
3652
});
3753
}
@@ -42,7 +58,7 @@ function ActionContainer() {
4258
if (hierarchy) displayArray(hierarchy);
4359

4460
// handles keyboard presses, function passes an event and index of each action-component
45-
function handleOnKeyDown(e:KeyboardEvent, i:number) {
61+
function handleOnKeyDown(e: KeyboardEvent, i: number) {
4662
let currIndex = i;
4763
// up array key pressed
4864
if (e.keyCode === 38) {
@@ -64,26 +80,37 @@ function ActionContainer() {
6480
}
6581
}
6682

67-
actionsArr = hierarchyArr.map((snapshot:{state?: Record<string, unknown>, key: string, displayName:string, componentName:string, componentData:{actualDuration: number}|undefined}, index) => {
68-
const selected = index === viewIndex;
69-
const last = viewIndex === -1 && index === hierarchyArr.length - 1;
70-
return (
71-
<Action
72-
key={`action${index}`}
73-
index={index}
74-
state={snapshot.state}
75-
displayName={snapshot.displayName}
76-
componentName={snapshot.componentName}
77-
componentData={snapshot.componentData}
78-
selected={selected}
79-
last={last}
80-
dispatch={dispatch}
81-
sliderIndex={sliderIndex}
82-
handleOnkeyDown={handleOnKeyDown}
83-
viewIndex={viewIndex}
84-
/>
85-
);
86-
});
83+
actionsArr = hierarchyArr.map(
84+
(
85+
snapshot: {
86+
state?: Record<string, unknown>;
87+
key: string;
88+
displayName: string;
89+
componentName: string;
90+
componentData: { actualDuration: number } | undefined;
91+
},
92+
index
93+
) => {
94+
const selected = index === viewIndex;
95+
const last = viewIndex === -1 && index === hierarchyArr.length - 1;
96+
return (
97+
<Action
98+
key={`action${index}`}
99+
index={index}
100+
state={snapshot.state}
101+
displayName={snapshot.displayName}
102+
componentName={snapshot.componentName}
103+
componentData={snapshot.componentData}
104+
selected={selected}
105+
last={last}
106+
dispatch={dispatch}
107+
sliderIndex={sliderIndex}
108+
handleOnkeyDown={handleOnKeyDown}
109+
viewIndex={viewIndex}
110+
/>
111+
);
112+
}
113+
);
87114

88115
return (
89116
<div className="action-container">

src/app/containers/ButtonsContainer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ function howToUseHandler() {
4949

5050
function ButtonsContainer() {
5151
const [{ tabs, currentTab }, dispatch] = useStoreContext();
52+
console.log('useStore in ButtonsContainer', useStoreContext())
53+
console.log('dispatch in ButtonsContainer', useStoreContext().dispatch)
5254
const {
5355
snapshots,
5456
mode: { paused, persist },

0 commit comments

Comments
 (0)