Skip to content

Commit 1738eff

Browse files
added pseudocode to App.tsx, MainContainer.tsx, and Store.tsx
1 parent 09d8a0e commit 1738eff

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

src/app/components/App.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { InitialStateProps } from '../FrontendTypes';
99
// currentTabInApp is the current active tab within Reactime (Map, Performance, History, etc).
1010
// This is used to determine the proper tutorial to render when How To button is pressed.
1111

12+
// we initialize what our initialState is here
1213
const initialState: InitialStateProps = {
1314
port: null,
1415
currentTab: null,
@@ -19,7 +20,9 @@ const initialState: InitialStateProps = {
1920

2021
function App(): JSX.Element {
2122
return (
23+
// we wrap our application with the <Router> tag so that all components that are nested will have the react-router context
2224
<Router>
25+
{/* we wrap our MainContainer with the provider so that we will be able to use the store context. We create our store by using useReducer and passing it into the value property */}
2326
<StoreContext.Provider value={useReducer(mainReducer, initialState)}>
2427
<MainContainer />
2528
</StoreContext.Provider>

src/app/containers/MainContainer.tsx

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,27 @@ import {
1818
import { useStoreContext } from '../store';
1919

2020
function MainContainer(): JSX.Element {
21+
// we destructure the returned context object from the invocation of the useStoreContext function. Properties not found on the initialState object (store/dispatch) are from the useReducer function invocation in the App component
2122
const [store, dispatch] = useStoreContext();
23+
// we continue to destructure store and get the tabs/currentTab/port
2224
const { tabs, currentTab, port } = store;
25+
// We create a local state actionView and set it to true
2326
const [actionView, setActionView] = useState(true);
27+
2428
// this function handles Time Jump sidebar view
2529
const toggleActionContainer = () => {
30+
// sets actionView to the opposite boolean value
2631
setActionView(!actionView);
32+
2733
// aside is like an added text that appears "on the side" aside some text.
2834
const toggleElem = document.querySelector('aside');
35+
// toggles the addition or the removal of the 'no-aside' class
2936
toggleElem.classList.toggle('no-aside');
37+
3038
// hides the record toggle button from Actions Container in Time Jump sidebar view
3139
const recordBtn = document.getElementById('recordBtn');
40+
41+
// switches whether to display the button by changing the display property between none and flex
3242
if (recordBtn.style.display === 'none') {
3343
recordBtn.style.display = 'flex';
3444
} else {
@@ -37,22 +47,35 @@ function MainContainer(): JSX.Element {
3747
};
3848
// let port;
3949
useEffect(() => {
40-
// only open port once
50+
// only open port once so if it exists, do not run useEffect again
4151
if (port) return;
4252

43-
// open long-lived connection with background script
53+
// 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.
54+
// we connect our listeners to our service worker
4455
const currentPort = chrome.runtime.connect();
56+
4557
// listen for a message containing snapshots from the background script
4658
currentPort.onMessage.addListener(
4759
// parameter message is an object with following type script properties
48-
(message: { action: string; payload: Record<string, unknown>; sourceTab: number }) => {
60+
(message: {
61+
action: string;
62+
payload: Record<string, unknown>;
63+
sourceTab: number
64+
}) => {
65+
// we destructure message into action, payload, sourceTab
4966
const { action, payload, sourceTab } = message;
5067
let maxTab: number;
68+
69+
// if the sourceTab doesn't exist or is 0
5170
if (!sourceTab) {
71+
// we create a tabsArray of strings composed of keys from our payload object
5272
const tabsArray: Array<string> = Object.keys(payload);
73+
// we then map out our tabsArray where we convert each string into a number
5374
const numTabsArray: number[] = tabsArray.map((tab) => Number(tab));
75+
// we then get the number of tabs we have
5476
maxTab = Math.max(...numTabsArray);
5577
}
78+
5679
switch (action) {
5780
case 'deleteTab': {
5881
dispatch(deleteTab(payload));
@@ -82,17 +105,22 @@ function MainContainer(): JSX.Element {
82105
}
83106
default:
84107
}
108+
109+
// we return true so that the connection stays open, otherwise the message channel will close
85110
return true;
86111
},
87112
);
88113

114+
// Below is used to track bugs in case the above connection closes. Remember that it should persist throughout the application lifecycle
89115
currentPort.onDisconnect.addListener(() => {
90116
console.log('this port is disconnecting line 79');
91117
// disconnecting
92118
});
93119

94120
// assign port to state so it could be used by other components
95121
dispatch(setPort(currentPort));
122+
123+
// NOTE: There is no dependency array declared here.
96124
});
97125

98126
// Error Page launch IF(Content script not launched OR RDT not installed OR Target not React app)
@@ -108,6 +136,7 @@ function MainContainer(): JSX.Element {
108136
tabs[currentTab];
109137
// if viewIndex is -1, then use the sliderIndex instead
110138
const snapshotView = viewIndex === -1 ? snapshots[sliderIndex] : snapshots[viewIndex];
139+
111140
// cleaning hierarchy and snapshotView from stateless data
112141
const statelessCleaning = (obj: {
113142
name?: string;

src/app/store.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import React, { useContext } from 'react';
33

4+
// we create a context object and assign it to StoreContext
45
export const StoreContext = React.createContext();
56

7+
// the useStoreContext function allows us to use use the above declared StoreContext.
68
export const useStoreContext = () => useContext(StoreContext);

0 commit comments

Comments
 (0)