Skip to content

Commit 544bf1f

Browse files
authored
Merge pull request #88 from oslabs-beta/bryan-sierra/tabs
Bryan sierra/tabs
2 parents 01eba9e + 143f39e commit 544bf1f

File tree

6 files changed

+56
-18
lines changed

6 files changed

+56
-18
lines changed

src/app/components/SwitchApp.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,27 @@ import { useStoreContext } from '../store';
44
import { setTab } from '../actions/actions';
55

66
const SwitchAppDropdown = () => {
7-
// const { loadApp } = setTab;
87
const [{ currentTab, tabs }, dispatch] = useStoreContext();
98

109
const tabsArray = [];
1110

1211
Object.keys(tabs).forEach(tab => {
13-
tabsArray.push({ value: tab, label: tab });
12+
if (tab !== 'sourceTab') tabsArray.push({ value: tab, label: tabs[tab].title });
1413
});
1514

1615
const currTab = {
1716
value: currentTab,
18-
label: currentTab,
17+
label: tabs[currentTab].title,
1918
};
2019

20+
2121
return (
2222
<Select
2323
className="react-select-container"
2424
classNamePrefix="react-select"
2525
value={currTab}
26-
// onChange={dispatch(setTab(loadApp))}
2726
onChange={e => {
2827
dispatch(setTab(parseInt(e.value, 10)));
29-
// console.log(e)
3028
}}
3129
options={tabsArray}
3230
/>

src/app/containers/HeadContainer.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
import React from 'react';
22
import SwitchAppDropdown from '../components/SwitchApp';
3+
import { useStoreContext } from '../store';
4+
35

46
function HeadContainer() {
7+
const [store] = useStoreContext();
8+
const { tabs, currentTab } = store;
9+
const { title } = tabs[currentTab];
510
return (
611
<div className="head-container">
712
<SwitchAppDropdown />
13+
<div>
14+
{title}
15+
</div>
816
</div>
917
);
1018
}

src/app/containers/MainContainer.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import StateContainer from './StateContainer';
55
import TravelContainer from './TravelContainer';
66
import ButtonsContainer from './ButtonsContainer';
77

8-
import { addNewSnapshots, initialConnect, setPort } from '../actions/actions';
8+
import {
9+
addNewSnapshots, initialConnect, setPort, setTab,
10+
} from '../actions/actions';
911
import { useStoreContext } from '../store';
1012

1113
function MainContainer() {
@@ -25,6 +27,7 @@ function MainContainer() {
2527
const { action, payload } = message;
2628
switch (action) {
2729
case 'sendSnapshots': {
30+
if (payload.sourceTab !== currentTab) dispatch(setTab(payload.sourceTab));
2831
// set state with the information received from the background script
2932
dispatch(addNewSnapshots(payload));
3033
break;
@@ -34,6 +37,10 @@ function MainContainer() {
3437
setnpm(true);
3538
break;
3639
}
40+
case 'activatedTab': {
41+
// console.log(payload, 'activatedTab in main Container');
42+
break;
43+
}
3744
default:
3845
}
3946
});
@@ -47,7 +54,6 @@ function MainContainer() {
4754
});
4855

4956
if (!npmExists) return <div style={{ color: 'black' }}>please install our npm package in your app</div>;
50-
5157
const { viewIndex, sliderIndex, snapshots } = tabs[currentTab];
5258

5359
// if viewIndex is -1, then use the sliderIndex instead

src/app/reducers/mainReducer.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,20 @@ export default (state, action) => produce(state, draft => {
133133
const { payload } = action;
134134

135135
Object.keys(payload).forEach(tab => {
136-
const { snapshots: newSnaps } = payload[tab];
137-
tabs[tab] = {
138-
...tabs[tab],
139-
...payload[tab],
140-
sliderIndex: newSnaps.length - 1,
141-
};
136+
if (tab !== 'sourceTab') {
137+
const { snapshots: newSnaps } = payload[tab];
138+
tabs[tab] = {
139+
...tabs[tab],
140+
...payload[tab],
141+
sliderIndex: newSnaps.length - 1,
142+
};
143+
}
142144
});
143145

144146
break;
145147
}
146148
case types.SET_TAB: {
147-
draft.currentTab = action.paylod;
149+
draft.currentTab = action.payload;
148150
break;
149151
}
150152
default:

src/app/styles/layout/_headContainer.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@
22
height: 5%;
33
background-color: $head-color;
44
}
5+
6+
.head-container {
7+
display: flex;
8+
flex-direction: row;
9+
align-items: center;
10+
justify-content: space-around;
11+
}

src/extension/background.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
let bg;
2-
const tabsObj = {};
3-
function createTabObj() {
2+
const tabsObj = {
3+
sourceTab: null,
4+
};
5+
6+
function createTabObj(title) {
47
return {
8+
title,
59
snapshots: [],
610
mode: {
711
persist: false,
@@ -62,7 +66,7 @@ chrome.runtime.onConnect.addListener(port => {
6266
chrome.runtime.onMessage.addListener((request, sender) => {
6367
// IGNORE THE AUTOMTAIC MESSAGE SENT BY CHROME WHEN CONTENT SCRIPT IS FIRST LOADED
6468
if (request.type === 'SIGN_CONNECT') return;
65-
69+
const tabTitle = sender.tab.title;
6670
const tabId = sender.tab.id;
6771
const { action } = request;
6872
let isReactTimeTravel = false;
@@ -74,7 +78,7 @@ chrome.runtime.onMessage.addListener((request, sender) => {
7478

7579
// everytime we get a new tabid, add it to the object
7680
if (isReactTimeTravel && !(tabId in tabsObj)) {
77-
tabsObj[tabId] = createTabObj();
81+
tabsObj[tabId] = createTabObj(tabTitle);
7882
}
7983

8084
const { persist } = tabsObj[tabId].mode;
@@ -101,6 +105,7 @@ chrome.runtime.onMessage.addListener((request, sender) => {
101105
}
102106

103107
tabsObj[tabId].snapshots.push(request.payload);
108+
tabsObj.sourceTab = tabId;
104109

105110
// send message to devtools
106111
if (bg) {
@@ -111,9 +116,21 @@ chrome.runtime.onMessage.addListener((request, sender) => {
111116
}
112117
break;
113118
default:
119+
break;
114120
}
115121
});
116122

123+
// chrome.tabs.onActivated.addListener((info) => {
124+
// console.log('this is activated', info);
125+
// if (bg) {
126+
// console.log('hello', bg);
127+
// bg.postMessage({
128+
// action: 'activatedTab',
129+
// payload: info.tabId,
130+
// });
131+
// }
132+
// });
133+
117134
// when tab is closed, remove the tabid from the tabsObj
118135
chrome.tabs.onRemoved.addListener(tabId => {
119136
delete tabsObj[tabId];

0 commit comments

Comments
 (0)