Skip to content

Commit 09a8299

Browse files
committed
refactor mainReducer to post name
1 parent fcc8e14 commit 09a8299

File tree

5 files changed

+64
-19
lines changed

5 files changed

+64
-19
lines changed

src/app/components/Chart.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ class Chart extends Component {
2323

2424
componentDidMount() {
2525
const { hierarchy } = this.props;
26-
console.log('this is hierarchy on didMount chart', hierarchy)
26+
// console.log('this is hierarchy on didMount chart', hierarchy)
2727
root = JSON.parse(JSON.stringify(hierarchy));
2828
this.maked3Tree();
2929
}
3030

3131
componentDidUpdate() {
3232
const { hierarchy } = this.props;
33-
console.log('this is hierarchy on didUpdate chart', hierarchy)
33+
// console.log('this is hierarchy on didUpdate chart', hierarchy)
3434
root = JSON.parse(JSON.stringify(hierarchy));
3535
this.maked3Tree();
3636
}
@@ -52,7 +52,7 @@ class Chart extends Component {
5252
};
5353
const width = 600 - margin.right - margin.left;
5454
const height = 700 - margin.top - margin.bottom;
55-
console.log('this is this.chartRef.current on chart', this.chartRef.current)
55+
// console.log('this is this.chartRef.current on chart', this.chartRef.current)
5656
const chartContainer = d3.select(this.chartRef.current)
5757
.append('svg') // chartContainer is now pointing to svg
5858
.attr('width', width)
@@ -145,7 +145,7 @@ class Chart extends Component {
145145
// this arranges the angle of the text
146146
.attr('transform', function (d) { return 'rotate(' + (d.x < Math.PI ? d.x - Math.PI / 2 : d.x + Math.PI / 2) * 1 / Math.PI + ')'; })
147147
.text(function (d) {
148-
console.log('this is d from text char line 148', d)
148+
// console.log('this is d from text char line 148', d)
149149
// save d.data.index to variable
150150
// gabi and nate :: display the name of of specific patch
151151
return `${d.data.name}.${d.data.branch}`;

src/app/containers/ActionContainer.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function ActionContainer() {
3636
}
3737
}
3838
displayArray(hierarchy)
39-
console.log('this is hierarchyArr', hierarchyArr)
39+
// console.log('this is hierarchyArr', hierarchyArr)
4040

4141
actionsArr = hierarchyArr.map((snapshot, index) => {
4242
const selected = index === viewIndex;

src/app/containers/MainContainer.jsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import { useStoreContext } from '../store';
1313
function MainContainer() {
1414
const [store, dispatch] = useStoreContext();
1515
const { tabs, currentTab, port: currentPort } = store;
16-
console.log('entered MainContainer');
16+
// console.log('entered MainContainer');
1717

1818
// add event listeners to background script
1919
useEffect(() => {
2020
// only open port once
2121
if (currentPort) return;
2222
// open connection with background script
23-
console.log('opening connection with background script');
23+
// console.log('opening connection with background script');
2424
const port = chrome.runtime.connect();
25-
console.log('connection opened?');
25+
// console.log('connection opened?');
2626

2727
// listen for a message containing snapshots from the background script
2828
port.onMessage.addListener(message => {
29-
console.log('this is message from port', message)
29+
// console.log('this is message from port', message)
3030
const { action, payload, sourceTab } = message;
3131
switch (action) {
3232
case 'deleteTab': {

src/app/reducers/mainReducer.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,44 @@
22
import produce from 'immer';
33
import * as types from '../constants/actionTypes';
44

5+
56
export default (state, action) => produce(state, draft => {
67
const { port, currentTab, tabs } = draft;
78
const {
8-
snapshots, mode, intervalId, viewIndex, sliderIndex,
9+
hierarchy, snapshots, mode, intervalId, viewIndex, sliderIndex,
910
} = tabs[currentTab] || {};
11+
12+
13+
// gabi and nate :: function that find the index in the hiearachy and extract the name of the equivalent index to add to the post message
14+
const findName = (index, hierarchy) => {
15+
if(hierarchy.index == index){
16+
return hierarchy.name;
17+
}
18+
else {
19+
const hierarchyChildArray = [];
20+
for (let hierarchyChild of hierarchy.children){
21+
hierarchyChildArray.push(findName(index, hierarchyChild));
22+
}
23+
for (let hierarchyChildName of hierarchyChildArray){
24+
if(hierarchyChildName){
25+
return hierarchyChildName
26+
}
27+
}
28+
}
29+
}
1030

1131
switch (action.type) {
1232
case types.MOVE_BACKWARD: {
1333
if (snapshots.length > 0 && sliderIndex > 0) {
1434
const newIndex = sliderIndex - 1;
35+
// gabi and nate :: find the name by the newIndex parsing through the hierarchy to send to background.js the current name in the jump action
36+
const nameFromIndex = findName(newIndex, hierarchy)
1537

1638
port.postMessage({
1739
action: 'jumpToSnap',
1840
payload: snapshots[newIndex],
1941
index: newIndex,
42+
name: nameFromIndex,
2043
tabId: currentTab,
2144
});
2245
clearInterval(intervalId);
@@ -29,10 +52,13 @@ export default (state, action) => produce(state, draft => {
2952
case types.MOVE_FORWARD: {
3053
if (sliderIndex < snapshots.length - 1) {
3154
const newIndex = sliderIndex + 1;
55+
// gabi and nate :: find the name by the newIndex parsing through the hierarchy to send to background.js the current name in the jump action
56+
const nameFromIndex = findName(newIndex, hierarchy)
3257

3358
port.postMessage({
3459
action: 'jumpToSnap',
3560
index: newIndex,
61+
name: nameFromIndex,
3662
payload: snapshots[newIndex],
3763
tabId: currentTab,
3864
});
@@ -48,9 +74,11 @@ export default (state, action) => produce(state, draft => {
4874
break;
4975
}
5076
case types.SLIDER_ZERO: {
77+
// gabi and nate :: reset name to 0 to send to background.js the current name in the jump action
5178
port.postMessage({
5279
action: 'jumpToSnap',
5380
index: 0,
81+
name: 0,
5482
payload: snapshots[0],
5583
tabId: currentTab,
5684
});
@@ -64,10 +92,14 @@ export default (state, action) => produce(state, draft => {
6492
break;
6593
}
6694
case types.CHANGE_SLIDER: {
95+
// gabi and nate :: finds the name by the action.payload, parsing through the hierarchy to send to background.js the current name in the jump action
96+
const nameFromIndex = findName(action.payload, hierarchy)
97+
6798
port.postMessage({
6899
action: 'jumpToSnap',
69100
payload: snapshots[action.payload],
70101
index: action.payload,
102+
name: nameFromIndex,
71103
tabId: currentTab,
72104
});
73105
tabs[currentTab].sliderIndex = action.payload;
@@ -85,6 +117,10 @@ export default (state, action) => produce(state, draft => {
85117
tabs[currentTab].currLocation = tabs[currentTab].hierarchy;
86118
// reset index
87119
tabs[currentTab].index = 0;
120+
// gabi and nate :: reset name
121+
tabs[currentTab].name = 0;
122+
// gabi and nate :: reset branch
123+
tabs[currentTab].branch = 0;
88124
break;
89125
}
90126
case types.SET_PORT: {
@@ -168,7 +204,7 @@ export default (state, action) => produce(state, draft => {
168204
break;
169205
}
170206
case types.SET_TAB: {
171-
console.log('this is action.payload', action.payload)
207+
// console.log('this is SET_TAB action.payload', action.payload)
172208
if (typeof action.payload === 'number') {
173209
draft.currentTab = action.payload;
174210
break;

src/extension/background.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ function sendToHierarchy(tabObj, newNode) {
6969
}
7070
}
7171

72-
function changeCurrLocation(tabObj, rootNode, index) {
72+
function changeCurrLocation(tabObj, rootNode, index, name) {
7373
// gabi and nate :: index comes from the app's main reducer to locate the right current location on tabObj
7474
// check if current node has the index wanted
7575
if (rootNode.index === index) {
7676
tabObj.currLocation = rootNode;
7777
// gabi and nate :: index of current location from where the next node will be a child
78-
tabObj.currParent = index;
78+
tabObj.currParent = name;
7979
return;
8080
}
8181
// base case if no children
@@ -85,7 +85,7 @@ function changeCurrLocation(tabObj, rootNode, index) {
8585
}
8686
if (rootNode.children) {
8787
rootNode.children.forEach(child => {
88-
changeCurrLocation(tabObj, child, index);
88+
changeCurrLocation(tabObj, child, index, name);
8989
});
9090
}
9191
}
@@ -139,7 +139,11 @@ chrome.runtime.onConnect.addListener(port => {
139139
// reassigning pointer to the appropriate node to branch off of
140140
tabsObj[tabId].currLocation = tabsObj[tabId].hierarchy;
141141
// reset index
142-
tabsObj[tabId].index = 1;
142+
tabsObj[tabId].index = 0;
143+
// reset name
144+
tabs[currentTab].name = 0;
145+
// reset branch
146+
tabs[currentTab].branch = 0;
143147
return true;
144148
case 'setLock':
145149
tabsObj[tabId].mode.locked = payload;
@@ -164,7 +168,7 @@ chrome.runtime.onMessage.addListener((request, sender) => {
164168
if (request.type === 'SIGN_CONNECT') return true;
165169
const tabTitle = sender.tab.title;
166170
const tabId = sender.tab.id;
167-
const { action, index } = request;
171+
const { action, index, name } = request;
168172
let isReactTimeTravel = false;
169173

170174
// Filter out tabs that don't have reactime
@@ -188,7 +192,7 @@ chrome.runtime.onMessage.addListener((request, sender) => {
188192
// console.log('this tabsObj[tabId] sent to changeCurrLocation', tabsObj[tabId])
189193
// console.log('this tabsObj[tabId].hierarchy sent to changeCurrLocation', tabsObj[tabId].hierarchy)
190194
// console.log('this index sent to changeCurrLocation', index)
191-
changeCurrLocation(tabsObj[tabId], tabsObj[tabId].hierarchy, index);
195+
changeCurrLocation(tabsObj[tabId], tabsObj[tabId].hierarchy, index, name);
192196
break;
193197
}
194198
// this case causes d3 graph to display 1 instead of 0
@@ -199,13 +203,18 @@ chrome.runtime.onMessage.addListener((request, sender) => {
199203
if (!persist) {
200204
tabsObj[tabId].snapshots.splice(1);
201205
// reset children in root node to reset graph
202-
// if (tabsObj[tabId].hierarchy)
203206
if (tabsObj[tabId].hierarchy)
204207
tabsObj[tabId].hierarchy.children = [];
205208
// reassigning pointer to the appropriate node to branch off of
209+
// tabsObj[tabId].hierarchy = null;
206210
tabsObj[tabId].currLocation = tabsObj[tabId].hierarchy;
211+
// tabsObj[tabId].currLocation = null;
207212
// reset index
208-
tabsObj[tabId].index = 1;
213+
tabsObj[tabId].index = 0;
214+
// reset currParent
215+
tabsObj[tabId].currParent = 0;
216+
// reset currBranch
217+
tabsObj[tabId].currBranch = 0;
209218

210219
// send a message to devtools
211220
portsArr.forEach(bg =>

0 commit comments

Comments
 (0)