Skip to content

Commit 0262055

Browse files
authored
Merge pull request #45 from oslabs-beta/rydang/treelabel
added name labeling to the tree, also reset lock and pause button state upon refresh
2 parents 9534209 + e15a596 commit 0262055

File tree

7 files changed

+65
-52
lines changed

7 files changed

+65
-52
lines changed

src/app/components/Action.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const Action = (props) => {
1515
<div
1616
className="jump-button"
1717
onClick={() => handleJumpSnapshot(index)}
18-
onKeyPress={() => console.log('key pressed')}
1918
tabIndex={index}
2019
role="button"
2120
>

src/app/components/Tree.jsx

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import React from 'react';
22
import JSONTree from 'react-json-tree';
3+
import PropTypes from 'prop-types';
34

45
const getItemString = (type, data, itemType, itemString) => (
56
<span>
6-
{/* //
7-
{type} */}
7+
{data.name}
88
</span>
99
);
1010

1111
const Tree = (props) => {
1212
const { snapshot } = props;
13-
console.log('TREE COMPONENT IS PRINTED');
14-
console.log(snapshot);
1513
return (
1614
<React.Fragment>
1715
{snapshot && (
@@ -20,11 +18,26 @@ const Tree = (props) => {
2018
theme={{ tree: () => ({ className: 'json-tree' }) }}
2119
shouldExpandNode={() => true}
2220
getItemString={getItemString}
23-
// labelRenderer={raw => <strong>{raw}</strong>}
24-
// valueRenderer={raw => <em>{raw}</em>}
21+
labelRenderer={(raw) => {
22+
if (typeof raw[0] !== 'number') return <span>{raw[0]}</span>;
23+
}}
2524
/>
2625
)}
2726
</React.Fragment>
2827
);
2928
};
29+
30+
Tree.propTypes = {
31+
snapshot: PropTypes.shape({
32+
state: PropTypes.oneOfType([
33+
PropTypes.string,
34+
PropTypes.object,
35+
]),
36+
children: PropTypes.arrayOf(
37+
PropTypes.object,
38+
),
39+
name: PropTypes.string,
40+
}).isRequired,
41+
};
42+
3043
export default Tree;

src/app/containers/MainContainer.jsx

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@ class MainContainer extends Component {
3939
switch (action) {
4040
case 'sendSnapshots': {
4141
const snapshotIndex = payload.length - 1;
42-
4342
// set state with the information received from the background script
4443
this.setState({ snapshots: payload, snapshotIndex });
4544
break;
4645
}
47-
case 'initialConnectSnapshot': {
46+
case 'initialConnectSnapshots': {
4847
const { snapshots, mode } = payload;
4948
const snapshotIndex = snapshots.length - 1;
5049
this.setState({ snapshots, snapshotIndex, mode });
@@ -69,7 +68,7 @@ class MainContainer extends Component {
6968
if (snapshots.length > 0 && snapshotIndex > 0) {
7069
const newIndex = snapshotIndex - 1;
7170
// second callback parameter of setState to invoke handleJumpSnapshot
72-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex) );
71+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
7372
}
7473
}
7574

@@ -78,34 +77,34 @@ class MainContainer extends Component {
7877
this.pause();
7978
if (snapshotIndex < snapshots.length - 1) {
8079
const newIndex = snapshotIndex + 1;
81-
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex) );
80+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
8281
}
8382
}
8483

8584
play() {
86-
globalPlaying = !globalPlaying
87-
this.setState({playing: globalPlaying}, () => {
88-
if(this.state.playing){
85+
globalPlaying = !globalPlaying;
86+
this.setState({ playing: globalPlaying }, () => {
87+
if (this.state.playing) {
8988
intervalId = setInterval(() => {
9089
const { snapshots, snapshotIndex } = this.state;
91-
if (snapshotIndex < snapshots.length - 1) {
92-
const newIndex = snapshotIndex + 1;
93-
this.setState({ snapshotIndex: newIndex}, this.handleJumpSnapshot(newIndex) );
94-
} else {
95-
// clear interval when play reaches the end
96-
globalPlaying = false;
97-
clearInterval(intervalId);
98-
this.setState({ playing: false })
99-
}
90+
if (snapshotIndex < snapshots.length - 1) {
91+
const newIndex = snapshotIndex + 1;
92+
this.setState({ snapshotIndex: newIndex }, this.handleJumpSnapshot(newIndex));
93+
} else {
94+
// clear interval when play reaches the end
95+
globalPlaying = false;
96+
clearInterval(intervalId);
97+
this.setState({ playing: false })
98+
}
10099
}, 1000);
101100
} else {
102101
clearInterval(intervalId);
103102
}
104-
})
103+
});
105104
}
106105

107106
pause() {
108-
this.setState({playing: false}, clearInterval(intervalId))
107+
this.setState({ playing: false }, clearInterval(intervalId));
109108
}
110109

111110
emptySnapshot() {
@@ -161,7 +160,7 @@ class MainContainer extends Component {
161160
handleJumpSnapshot={this.handleJumpSnapshot}
162161
emptySnapshot={this.emptySnapshot}
163162
/>
164-
<StateContainer snapshot={snapshots[snapshotIndex]} />
163+
{(snapshots.length) ? <StateContainer snapshot={snapshots[snapshotIndex]} /> : null}
165164
<TravelContainer
166165
snapshotsLength={snapshots.length}
167166
snapshotIndex={snapshotIndex}
@@ -170,8 +169,8 @@ class MainContainer extends Component {
170169
moveBackward={this.moveBackward}
171170
moveForward={this.moveForward}
172171
play={this.play}
173-
playing = {playing}
174-
pause = {this.pause}
172+
playing={playing}
173+
pause={this.pause}
175174
/>
176175
<ButtonsContainer mode={mode} toggleMode={this.toggleMode} />
177176
</div>

src/app/containers/TravelContainer.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const TravelContainer = ({
1515
}) => (
1616
<div className="travel-container">
1717
<div className="play-button" onClick={play}>
18-
{ playing ? 'Pause': 'Play' }
18+
{playing ? 'Pause' : 'Play'}
1919
</div>
2020
<MainSlider
2121
snapshotLength={snapshotsLength}
@@ -34,13 +34,14 @@ const TravelContainer = ({
3434
);
3535

3636
TravelContainer.propTypes = {
37-
pause: PropTypes.func.isRequried,
37+
pause: PropTypes.func.isRequired,
3838
play: PropTypes.func.isRequired,
3939
moveBackward: PropTypes.func.isRequired,
4040
moveForward: PropTypes.func.isRequired,
4141
snapshotsLength: PropTypes.number.isRequired,
4242
handleChangeSnapshot: PropTypes.func.isRequired,
4343
handleJumpSnapshot: PropTypes.func.isRequired,
4444
snapshotIndex: PropTypes.number.isRequired,
45+
playing: PropTypes.bool.isRequired,
4546
};
4647
export default TravelContainer;

src/extension/background.js

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ chrome.runtime.onConnect.addListener((port) => {
1515
// send it to devtools as soon as connection to devtools is made
1616
if (snapshotArr.length > 0) {
1717
bg.postMessage({
18-
action: 'initialConnectSnapshot',
18+
action: 'initialConnectSnapshots',
1919
payload: {
2020
snapshots: snapshotArr,
2121
mode,
@@ -57,28 +57,34 @@ chrome.runtime.onMessage.addListener((request) => {
5757
switch (action) {
5858
case 'tabReload':
5959
firstSnapshot = true;
60+
mode.locked = false;
61+
mode.paused = false;
6062
if (!persist) snapshotArr = [];
6163
break;
6264
case 'recordSnap':
63-
// don't do anything for the first snapshot if current mode is persist
64-
if (persist && firstSnapshot) {
65+
if (firstSnapshot) {
6566
firstSnapshot = false;
67+
// don't add anything to snapshot storage if mode is persisting for the initial snapshot
68+
if (!persist) snapshotArr.push(request.payload);
69+
bg.postMessage({
70+
action: 'initialConnectSnapshots',
71+
payload: {
72+
snapshots: snapshotArr,
73+
mode,
74+
},
75+
});
6676
break;
6777
}
68-
firstSnapshot = false;
6978
snapshotArr.push(request.payload);
70-
// if port is not null, send a message to devtools
71-
if (bg) {
72-
// TODO:
73-
// get active tab id
74-
// get snapshot arr from tab object
79+
// TODO:
80+
// get active tab id
81+
// get snapshot arr from tab object
7582

76-
// send message to devtools
77-
bg.postMessage({
78-
action: 'sendSnapshots',
79-
payload: snapshotArr,
80-
});
81-
}
83+
// send message to devtools
84+
bg.postMessage({
85+
action: 'sendSnapshots',
86+
payload: snapshotArr,
87+
});
8288
break;
8389
default:
8490
}

src/extension/contentScript.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
console.log('contentScript running');
2-
31
// listening for messages from npm package
42
window.addEventListener('message', (msg) => {
53
// post initial Message to npm package
6-
const { action, payload } = msg.data;
7-
console.log('npm -> contentScript', msg.data);
4+
const { action } = msg.data;
85
if (action === 'recordSnap') chrome.runtime.sendMessage(msg.data);
96
});
107

118
// listening for messages from background.js
12-
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
9+
chrome.runtime.onMessage.addListener((request) => {
1310
// send the message to npm package
1411
const { action } = request;
1512
switch (action) {
@@ -29,4 +26,3 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
2926
chrome.runtime.sendMessage({ action: 'tabReload' });
3027

3128
window.postMessage({ action: 'contentScriptStarted' });
32-
console.log('contentScriptStarted msg sent');

src/extension/devtools.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
chrome.devtools.panels.create('React Time-Travel', null, 'panel.html', (panel) => {
2-
console.log('devtools.js => panel created');
32
});

0 commit comments

Comments
 (0)