Skip to content

Commit 5e31fb5

Browse files
committed
tab management
1 parent 45ac7fb commit 5e31fb5

File tree

6 files changed

+55
-13
lines changed

6 files changed

+55
-13
lines changed

src/app/components/SwitchApp.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ 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"

src/app/containers/HeadContainer.jsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
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+
// eslint-disable-next-line prefer-destructuring
10+
const title = tabs[currentTab].title;
11+
// console.log('tabs', tabs);
12+
// console.log('currentTab', currentTab);
513
return (
614
<div className="head-container">
715
<SwitchAppDropdown />
16+
<div>
17+
{title}
18+
</div>
819
</div>
920
);
1021
}

src/app/containers/MainContainer.jsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import TravelContainer from './TravelContainer';
66
import ButtonsContainer from './ButtonsContainer';
77

88
import {
9-
addNewSnapshots, initialConnect, setPort,
9+
addNewSnapshots, initialConnect, setPort, setTab,
1010
} from '../actions/actions';
1111
import { useStoreContext } from '../store';
1212

@@ -27,6 +27,7 @@ function MainContainer() {
2727
const { action, payload } = message;
2828
switch (action) {
2929
case 'sendSnapshots': {
30+
if (payload.sourceTab !== currentTab) dispatch(setTab(payload.sourceTab));
3031
// set state with the information received from the background script
3132
dispatch(addNewSnapshots(payload));
3233
break;
@@ -36,6 +37,10 @@ function MainContainer() {
3637
setnpm(true);
3738
break;
3839
}
40+
case 'activatedTab': {
41+
// console.log(payload, 'activatedTab in main Container');
42+
break;
43+
}
3944
default:
4045
}
4146
});

src/app/reducers/mainReducer.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,13 @@ export default (state, action) => produce(state, (draft) => {
123123
const { payload } = action;
124124

125125
Object.keys(payload).forEach((tab) => {
126-
const { snapshots: newSnaps } = payload[tab];
127-
tabs[tab] = {
128-
...tabs[tab],
129-
...payload[tab],
130-
sliderIndex: newSnaps.length - 1,
126+
if (tab !== 'sourceTab') {
127+
const { snapshots: newSnaps } = payload[tab];
128+
tabs[tab] = {
129+
...tabs[tab],
130+
...payload[tab],
131+
sliderIndex: newSnaps.length - 1,
132+
};
131133
};
132134
});
133135

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)