Skip to content

Commit 2954ce9

Browse files
authored
Merge pull request #15 from oslabs-beta/josh/statecontainer
Data flows from npm package to chrome extension
2 parents b2625ce + 678e73c commit 2954ce9

22 files changed

+28698
-1867
lines changed

extension/app/components/Action.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import PropTypes from 'prop-types';
33

44
const Action = (props) => {
55
const {
6-
snapshot, selected, handleChangeSnapshot, index,
6+
snapshot, selected, handleChangeSnapshot, handleSendSnapshot, index,
77
} = props;
88
return (
99
<div
@@ -12,8 +12,8 @@ const Action = (props) => {
1212
handleChangeSnapshot(index);
1313
}}
1414
>
15-
{snapshot.state}
16-
{selected.toString()}
15+
{index}
16+
<button onClick={() => handleSendSnapshot(index)}>Jump</button>
1717
</div>
1818
);
1919
};

extension/app/containers/ActionContainer.jsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ class ActionContainer extends Component {
77
}
88

99
render() {
10-
const { snapshots, snapshotIndex, handleChangeSnapshot } = this.props;
10+
const {
11+
snapshots, snapshotIndex, handleChangeSnapshot, handleSendSnapshot,
12+
} = this.props;
1113
let actionsArr = [];
12-
if (snapshots) {
14+
if (snapshots.length > 0) {
1315
actionsArr = snapshots.map((snapshot, index) => {
1416
const selected = index === snapshotIndex;
1517
return (
@@ -19,6 +21,7 @@ class ActionContainer extends Component {
1921
snapshot={snapshot}
2022
selected={selected}
2123
handleChangeSnapshot={handleChangeSnapshot}
24+
handleSendSnapshot={handleSendSnapshot}
2225
/>
2326
);
2427
});

extension/app/containers/MainContainer.jsx

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,38 @@ class MainContainer extends Component {
88
constructor() {
99
super();
1010
this.state = {
11-
snapshots: [{ state: 'snapshot1' , state2: 'othercomp snapshot1'}, { state: 'snapshot2' }, { state: 'snapshot3' }],
11+
snapshots: [],
1212
snapshotIndex: 0,
13+
port: null,
1314
};
1415

1516
this.handleChangeSnapshot = this.handleChangeSnapshot.bind(this);
17+
this.handleSendSnapshot = this.handleSendSnapshot.bind(this);
1618
}
1719

1820
componentDidMount() {
19-
// add a listener to capture messages from our backend
20-
// this should be in the inject script
21-
// window.addEventListener(
22-
// 'message',
23-
// (event) => {
24-
// // We only accept messages from ourselves
25-
// if (event.source !== window) return;
26-
// console.log(`Message received from backend: ${event.payload}`);
27-
// },
28-
// false,
29-
// );
21+
const port = chrome.runtime.connect();
22+
port.onMessage.addListener((snapshots) => {
23+
console.log('message from background script', snapshots);
24+
this.setState({ snapshots });
25+
});
26+
port.onDisconnect.addListener((obj) => {
27+
console.log('disconnected port', obj);
28+
});
29+
this.setState({ port });
3030
}
3131

32+
// this method changes the snapshotIndex state
33+
// snapshotIndex could be changed from either the ActionContainer or the TravelContainer
3234
handleChangeSnapshot(snapshotIndex) {
33-
console.log('handleChangeSnapshot', snapshotIndex);
34-
// change snapshotIndex
35-
// snapshotIndex could be changed from either the ActionContainer or the TravelContainer
3635
this.setState({ snapshotIndex });
3736
}
3837

38+
handleSendSnapshot(snapshotIndex) {
39+
const { snapshots, port } = this.state;
40+
port.postMessage({ action: 'jumpToSnap', payload: snapshots[snapshotIndex] });
41+
}
42+
3943
render() {
4044
const { snapshots, snapshotIndex } = this.state;
4145

@@ -48,13 +52,14 @@ class MainContainer extends Component {
4852
snapshots={snapshots}
4953
snapshotIndex={snapshotIndex}
5054
handleChangeSnapshot={this.handleChangeSnapshot}
55+
handleSendSnapshot={this.handleSendSnapshot}
5156
/>
5257
<StateContainer snapshot={snapshots[snapshotIndex]} />
5358
</div>
5459
<TravelContainer
55-
snapshotsLength = {snapshots.length}
56-
handleChangeSnapshot = {this.handleChangeSnapshot}
57-
snapshotIndex = {snapshotIndex}
60+
snapshotsLength={snapshots.length}
61+
handleChangeSnapshot={this.handleChangeSnapshot}
62+
snapshotIndex={snapshotIndex}
5863
/>
5964
</div>
6065
);

extension/app/containers/StateContainer.jsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
11
import React, { Component } from 'react';
22
import ReactJson from 'react-json-view';
33

4+
const JsonDisplay = snapshot => (
5+
<ReactJson enableClipboard={false} theme="solarized" groupArraysAfterLength={50} src={snapshot} />
6+
);
47
class StateContainer extends Component {
58
constructor(props) {
69
super(props);
710
}
811

912
render() {
13+
let snapshotObjs = [];
1014
const { snapshot } = this.props;
11-
return (
12-
<div className="state-container">
13-
<ReactJson
14-
enableClipboard={false}
15-
theme="solarized"
16-
groupArraysAfterLength={50}
17-
src={snapshot}
18-
/>
19-
</div>
20-
);
15+
if (snapshot) {
16+
snapshotObjs = snapshot.map(component => JsonDisplay(component));
17+
}
18+
return <div className="state-container">{snapshotObjs}</div>;
2119
}
2220
}
2321

extension/app/styles/styles.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@
5555
}
5656

5757
.action-component {
58+
display: flex;
59+
flex-direction: row;
60+
justify-content: space-around;
61+
align-items: center;
5862
height: 40px;
5963
width: 100%;
6064
background-color: rgb(170, 170, 170);
@@ -66,3 +70,8 @@
6670
.action-component.selected {
6771
background-color: rgb(134, 134, 134);
6872
}
73+
74+
.action-component button {
75+
position: relative;
76+
right: 0px;
77+
}

extension/background.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
console.log('background.js file is running');
2+
3+
let bg;
4+
5+
// need a function to clear snapshotArr when either tab is closed or page is refreshed
6+
const snapshotArr = [];
7+
8+
// establishing connection with devtools
9+
chrome.runtime.onConnect.addListener((port) => {
10+
bg = port;
11+
bg.postMessage({ from: 'background.js', message: 'established connection', date: Date.now() });
12+
13+
// if snapshots were saved in the snapshotArr,
14+
// send it to devtools as soon as connection to devtools is made
15+
if (snapshotArr.length > 0) {
16+
bg.postMessage(snapshotArr);
17+
}
18+
19+
// receive snapshot from background and send it to contentScript
20+
port.onMessage.addListener((msg) => {
21+
console.log('contentScript -> background', msg);
22+
chrome.runtime.sendMessage({ data: msg });
23+
});
24+
});
25+
26+
// background.js recieves message from contentScript.js
27+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
28+
// if port is not null, send a message to devtools
29+
if (bg) {
30+
snapshotArr.push(request);
31+
bg.postMessage(snapshotArr);
32+
// else, push snapshot into an array
33+
} else snapshotArr.push(request);
34+
});

extension/contentScript.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
console.log('contentScript injected');
2+
3+
// listening for messages from npm package
4+
window.addEventListener('message', (msg) => {
5+
const { action, payload } = msg.data;
6+
7+
if (action === 'recordSnap') chrome.runtime.sendMessage(payload);
8+
// console.log(msg);
9+
});
10+
11+
// listening for messages from background.js
12+
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
13+
// send the message to npm package
14+
console.log('devtools -> contentScript', request);
15+
const { action } = request;
16+
if (action === 'jumpToSnap') window.postMessage({ request });
17+
});

extension/devtools.js

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

extension/manifest.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,16 @@
44
"devtools_page": "devtools.html",
55
"description": "A Chrome extension that helps debug React by memorizing the state of components with every render.",
66
"manifest_version": 2,
7-
"content_security_policy": "script-src 'self' 'unsafe-eval' ; object-src 'self'"
7+
"content_security_policy": "script-src 'self' 'unsafe-eval' ; object-src 'self'",
8+
"background": {
9+
"scripts": ["background.js"],
10+
"persistent": false
11+
},
12+
"content_scripts": [
13+
{
14+
"matches": ["<all_urls>"],
15+
"js": ["contentScript.js"]
16+
}
17+
],
18+
"permissions": ["contextMenus", "tabs", "<all_urls>"]
819
}

extension/panel.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<body>
1111
<div id="root"></div>
1212
<link rel="stylesheet" type="text/css" href="./app/styles/panel.css" />
13+
<script type="text/javascript" src="panel.js"></script>
1314
<script type="text/javascript" src="dist/app.bundle.js"></script>
1415
</body>
1516
</html>

0 commit comments

Comments
 (0)