Skip to content

Commit d33e991

Browse files
authored
Merge pull request #16 from oslabs-beta/ctstamper/Timeout
Progressed on fixing connectivity issue.
2 parents f70a910 + ba6a634 commit d33e991

File tree

8 files changed

+142
-17
lines changed

8 files changed

+142
-17
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,9 @@
184184
"@fortawesome/free-solid-svg-icons": "^5.15.1",
185185
"@fortawesome/react-fontawesome": "^0.1.12",
186186
"@mui/icons-material": "^5.14.1",
187-
"@mui/material": "^5.14.1",
187+
"@mui/material": "^5.14.12",
188188
"@mui/styled-engine-sc": "^5.12.0",
189+
"@mui/system": "^5.14.12",
189190
"@reduxjs/toolkit": "^1.9.6",
190191
"@types/react-dom": "^17.0.14",
191192
"@types/react-router-dom": "^5.3.3",

src/app/FrontendTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ export interface InitialStateProps {
142142
currentTitle: null | string;
143143
tabs: unknown;
144144
currentTabInApp: null | string;
145+
connectionStatus: boolean;
146+
reconnectRequested: boolean;
145147
}
146148

147149
export interface DiffProps {

src/app/RTKslices.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const initialState: InitialStateProps = { // we initialize what our initialState
88
currentTitle: 'No Target',
99
tabs: {},
1010
currentTabInApp: null,
11+
connectionStatus: true,
12+
reconnectRequested: false,
1113
};
1214

1315
const findName = (index, obj) => {
@@ -151,7 +153,7 @@ export const mainSlice = createSlice({
151153
console.log('setTab: state end', current(state));
152154

153155
},
154-
deleteTab : (state, action) => {
156+
deleteTab: (state, action) => {
155157
console.log('deleteTab: ', current(state));
156158
delete state.tabs[action.payload];
157159
console.log('deleteTab: state end', current(state));
@@ -369,9 +371,6 @@ export const mainSlice = createSlice({
369371

370372

371373
},
372-
373-
374-
375374
toggleMode: (state, action)=>{
376375
console.log('Toggle Mode: ', current(state));
377376

@@ -507,6 +506,22 @@ export const mainSlice = createSlice({
507506
};
508507
});
509508
tabs[currentTab] = { ...tabs[currentTab], seriesSavedStatus: false };
509+
},
510+
disconnected: (state) => {
511+
console.log('disconnected: ', current(state));
512+
state.connectionStatus = false;
513+
console.log('disconnected: state end', current(state));
514+
},
515+
startReconnect: (state) => {
516+
console.log('startReconnect: ', current(state));
517+
state.reconnectRequested = true;
518+
console.log('startReconnect: state end', current(state));
519+
},
520+
endReconnect: (state) => {
521+
console.log('startReconnect: ', current(state));
522+
state.reconnectRequested = false;
523+
state.connectionStatus = true;
524+
console.log('startReconnect: state end', current(state));
510525
}
511526
},
512527
})
@@ -537,7 +552,10 @@ export const {
537552
onHoverExit,
538553
save,
539554
toggleExpanded,
540-
deleteSeries
555+
deleteSeries,
556+
disconnected,
557+
startReconnect,
558+
endReconnect,
541559
} = mainSlice.actions
542560

543561

src/app/components/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const initialState: InitialStateProps = { // we initialize what our initialState
1919
currentTitle: 'No Target',
2020
tabs: {},
2121
currentTabInApp: null,
22+
connectionStatus: true,
23+
reconnectRequested: false,
2224
};
2325

2426
function App(): JSX.Element {

src/app/components/StatusDot.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import React from 'react';
2+
// import { styled } from '@mui/system';
3+
import styled from 'styled-components';
4+
5+
const Dot = styled('div')({
6+
borderRadius: '50%',
7+
width: '12px',
8+
height: '12px',
9+
display: 'inline-block',
10+
});
11+
12+
const StatusDot = ({ status }) => {
13+
let color;
14+
switch (status) {
15+
case 'active':
16+
color = 'green';
17+
break;
18+
case 'inactive':
19+
color = 'red';
20+
break;
21+
default:
22+
color = 'green';
23+
}
24+
return (<Dot style={{ backgroundColor: color }}/>)
25+
};
26+
27+
export default StatusDot;

src/app/containers/ButtonsContainer.tsx

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import * as React from 'react';
2+
//importing useState from react to handle local state for button reconnect functionality
3+
import { useState } from 'react';
24
// import { importSnapshots, toggleMode } from '../actions/actions';
35
// import { useStoreContext } from '../store';
46
import { Button } from '@mui/material';
7+
//importing necesary material UI components for dialogue popup
8+
import { Dialog, DialogTitle, DialogContent, DialogActions } from '@mui/material';
59
import Tutorial from '../components/Tutorial';
610
import LockIcon from '@mui/icons-material/Lock';
711
import LockOpenIcon from '@mui/icons-material/LockOpen';
812
import FileDownloadIcon from '@mui/icons-material/FileDownload';
913
import FileUploadIcon from '@mui/icons-material/FileUpload';
10-
import { toggleMode, importSnapshots } from '../RTKslices';
14+
import { toggleMode, importSnapshots, startReconnect } from '../RTKslices';
1115
import { useDispatch, useSelector } from 'react-redux';
16+
import StatusDot from '../components/StatusDot';
1217

1318
function exportHandler(snapshots: []): void { // function that takes in our tabs[currentTab] object to be exported as a JSON file. NOTE: TypeScript needs to be updated
1419
const fileDownload: HTMLAnchorElement = document.createElement('a'); // invisible HTML element that will hold our tabs[currentTab] object
@@ -44,15 +49,35 @@ function importHandler(dispatch: (a: unknown) => void): void { // function handl
4449
fileUpload.click(); // click is a method on all HTML elements that simulates a mouse click, triggering the element's click event
4550
}
4651

52+
4753
function ButtonsContainer(): JSX.Element {
4854
// const [{ tabs, currentTab, currentTabInApp }, dispatch] = useStoreContext();
4955
// const [state, dispatch] = useStoreContext();
5056
const dispatch = useDispatch();
5157
const currentTab = useSelector((state: any) => state.main.currentTab);
5258
const tabs = useSelector((state: any)=>state.main.tabs);
5359
const currentTabInApp = useSelector((state: any)=> state.main.currentTabInApp);
60+
const connectionStatus = useSelector((state: any)=> state.main.connectionStatus);
5461
const { mode: { paused }} = tabs[currentTab];
55-
62+
//adding a local state using useState for the reconnect button functionality
63+
const [reconnectDialogOpen, setReconnectDialogOpen] = useState(false);
64+
65+
//logic for handling dialog box opening and closing
66+
const handleReconnectClick = () => {
67+
connectionStatus ? setReconnectDialogOpen(true) : dispatch(startReconnect());
68+
}
69+
70+
const handleReconnectConfirm = () => {
71+
//reconnection logic here
72+
dispatch(startReconnect());
73+
setReconnectDialogOpen(false);
74+
}
75+
76+
const handleReconnectCancel = () => {
77+
//closing the dialog
78+
setReconnectDialogOpen(false);
79+
}
80+
5681
return (
5782
<div className='buttons-container'>
5883
<Button
@@ -82,6 +107,35 @@ function ButtonsContainer(): JSX.Element {
82107
//commented out so we can use useDispatch in Tutorial.tsx
83108
dispatch={dispatch}
84109
currentTabInApp={currentTabInApp} />
110+
{/* adding a button for reconnection functionality 10/5/2023 */}
111+
<Button
112+
variant='outlined'
113+
className='reconnect-button'
114+
type='button'
115+
//update onClick functionality to include a popup that contains....
116+
onClick={handleReconnectClick}
117+
endIcon={
118+
<span style={{ position: 'relative', display: 'flex', alignItems: 'center' }}>
119+
<StatusDot status={connectionStatus ? 'active' : 'inactive'} />
120+
</span>
121+
}
122+
>
123+
Reconnect
124+
</Button>
125+
<Dialog open={reconnectDialogOpen} onClose={handleReconnectCancel}>
126+
<DialogTitle>Reconnect Confirmation</DialogTitle>
127+
<DialogContent>
128+
{/* //insert info here on current connection status */}
129+
</DialogContent>
130+
<DialogActions>
131+
<Button onClick={handleReconnectCancel} color="primary">
132+
Cancel
133+
</Button>
134+
<Button onClick={handleReconnectConfirm} color="primary">
135+
Confirm Reconnect
136+
</Button>
137+
</DialogActions>
138+
</Dialog>
85139
</div>
86140
);
87141
}

src/app/containers/MainContainer.tsx

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import {
1313
deleteTab,
1414
noDev,
1515
setCurrentLocation,
16+
disconnected,
17+
endReconnect,
1618
pause,
1719
} from '../RTKslices';
1820
import { useDispatch, useSelector } from 'react-redux';
@@ -35,6 +37,7 @@ function MainContainer(): JSX.Element {
3537
const currentTab = useSelector((state: any) => state.main.currentTab);
3638
const tabs = useSelector((state: any) => state.main.tabs);
3739
const port = useSelector((state: any) => state.main.port);
40+
const { connectionStatus, reconnectRequested } = useSelector((state: any) => state.main);
3841

3942
const [actionView, setActionView] = useState(true); // We create a local state 'actionView' and set it to true
4043

@@ -53,19 +56,22 @@ function MainContainer(): JSX.Element {
5356
recordBtn.style.display = 'none';
5457
}
5558
};
56-
59+
5760
useEffect(() => {
61+
console.log('LOL: ', port);
5862
if (port) return; // only open port once so if it exists, do not run useEffect again
59-
63+
console.log('Okie')
6064
// chrome.runtime allows our application to retrieve our service worker (our eventual bundles/background.bundle.js after running npm run build), details about the manifest, and allows us to listen and respond to events in our application lifecycle.
6165
const currentPort = chrome.runtime.connect(); // we connect to our service worker
6266
console.log('currentPort', currentPort);
63-
const keepAliveMainContainer = setInterval(() => { // interval to keep connection to background.js alive
64-
currentPort.postMessage({
65-
action: 'keepAlive' // messages sent to port to keep connection alive
66-
})
67-
}, 295000) // messages must happen within five minutes
68-
67+
const keepAliveMainContainer = () => { // interval to keep connection to background.js alive
68+
console.log('Hi :))');
69+
currentPort.postMessage({
70+
action: 'keepAlive' // messages sent to port to keep connection alive
71+
});
72+
} // messages must happen within ~five minutes
73+
setInterval(keepAliveMainContainer, 30000);
74+
6975
// listen for a message containing snapshots from the /extension/build/background.js service worker
7076
currentPort.onMessage.addListener(
7177
// parameter message is an object with following type script properties
@@ -117,13 +123,28 @@ function MainContainer(): JSX.Element {
117123
},
118124
);
119125

126+
// chrome.runtime.connect().onDisconnect.addListener(() => { // used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
120127
currentPort.onDisconnect.addListener(() => { // used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
121128
console.log('this port is disconnecting line 52');
129+
130+
dispatch(disconnected());
122131
});
132+
123133
console.log('currentPort', currentPort);
124134
dispatch(setPort(currentPort)); // assign port to state so it could be used by other components
135+
// if (!connectionStatus && reconnectRequested) dispatch(endReconnect());
125136
});
126137

138+
// useEffect(() => {
139+
// if (initialization) return;
140+
// chrome.runtime.connect().onDisconnect.addListener(() => { // used to track when the above connection closes unexpectedly. Remember that it should persist throughout the application lifecycle
141+
// console.log('this port is disconnecting line 52');
142+
143+
// dispatch(disconnected());
144+
// });
145+
// });
146+
147+
127148
// Error Page launch IF(Content script not launched OR RDT not installed OR Target not React app)
128149
if (
129150
!tabs[currentTab] ||

src/app/styles/layout/_buttonsContainer.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
.buttons-container {
66
margin: 0 1% 0 1%;
77
display: grid;
8-
grid-template-columns: repeat(4, 1fr);
8+
grid-template-columns: repeat(5, 1fr);
99
grid-gap: 1%;
1010
padding: 1% 0 1% 0;
1111
}

0 commit comments

Comments
 (0)