Skip to content

Commit 6d55367

Browse files
Pseudocode added to Loader.tsx and ErrorContainer.tsx
1 parent 1738eff commit 6d55367

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

src/app/components/Loader.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ import { faCheck, faExclamationCircle } from '@fortawesome/free-solid-svg-icons'
88
// Displays the result of the check when loading is done
99
const handleResult = (result: boolean): JSX.Element =>
1010
result ? (
11+
// if result boolean is true, we display a checkmark icon
1112
<FontAwesomeIcon icon={faCheck} className='check' size='lg' />
1213
) : (
14+
// if the result boolean is false, we display a fail icon
1315
<FontAwesomeIcon icon={faExclamationCircle} className='fail' size='lg' />
1416
);
1517

1618
// Returns the Loader component
1719
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
1820
const Loader = ({ loading, result }): JSX.Element =>
1921
loading ? (
22+
// if the loadingArray value is true, we display a loading icon
2023
<ClipLoader color='#123abc' size={30} loading={loading} />
2124
) : (
25+
// else we display a component produced by handleResult depending on if the result parameter (which takes an argument from the status object in ErrorContainer) is true or false
2226
handleResult(result)
2327
);
2428

src/app/containers/ErrorContainer.tsx

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,71 +5,100 @@ import Loader from '../components/Loader';
55
import ErrorMsg from '../components/ErrorMsg';
66
import { useStoreContext } from '../store';
77

8+
/*
9+
This is the loading popup that a user may get when first initalizing the application. This page checks:
10+
11+
1. if the content script has been launched on the current tab
12+
2. if React Dev Tools has been installed
13+
3. if target tab contains a compatible React app
14+
*/
15+
816
function ErrorContainer(): JSX.Element {
17+
// 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
918
const [store, dispatch] = useStoreContext();
19+
// We continue to destructure store and get the tabs/currentTab/port
1020
const { tabs, currentTitle, currentTab } = store;
11-
// hooks for error checks
21+
22+
// We create a local state "loadingArray" and set it to an array with three true elements. These will be used as hooks for error checking against a 'status' object that is declared later in a few lines.
23+
// This loadingArray is used later in the return statement to display a spinning loader icon if it's true. If it's false, either a checkmark icon or an exclamation icon will be displayed to the user.
1224
const [loadingArray, setLoading] = useState([true, true, true]);
25+
26+
// useRef returns an object with a property 'initialValue' and a value of whatever was passed in. This allows us to reference a value that's not needed for rendering
1327
const titleTracker = useRef(currentTitle);
1428
const timeout = useRef(null);
1529

30+
// function that launches the main app
1631
function launch(): void {
1732
dispatch(launchContentScript(tabs[currentTab]));
1833
}
1934

20-
// check if tabObj exists > set status
35+
// We create a status object that we may use later if tabs[currentTab] exists
2136
const status = {
2237
contentScriptLaunched: false,
2338
reactDevToolsInstalled: false,
2439
targetPageisaReactApp: false,
2540
};
41+
42+
// If we do have a tabs[currentTab] object, we replace the status obj we declared above with the properties of the tabs[currentTab].status
2643
if (tabs[currentTab]) {
2744
Object.assign(status, tabs[currentTab].status);
2845
}
2946

3047
// hook that sets timer while waiting for a snapshot from the background script, resets if the tab changes/reloads
3148
useEffect(() => {
49+
// We declare a function setLoadingArray which checks our local state loadingArray and compares it with a boolean value. If they don't match, we set the element to the passed in value boolean and update our local state with the new loadingArray
3250
function setLoadingArray(i: number, value: boolean) {
51+
// this conditional helps us avoid unecessary state changes if the element and the value are already the same
3352
if (loadingArray[i] !== value) {
34-
// avoid unecessary state changes
3553
const loadingArrayClone = [...loadingArray];
3654
loadingArrayClone[i] = value;
3755
setLoading(loadingArrayClone);
3856
}
3957
}
4058

41-
// check for tab reload/change: reset loadingArray
59+
// if the current tab changes/reloads, we reset loadingArray to it's default [true, true, true]
4260
if (titleTracker.current !== currentTitle) {
4361
titleTracker.current = currentTitle;
62+
4463
setLoadingArray(0, true);
4564
setLoadingArray(1, true);
4665
setLoadingArray(2, true);
66+
4767
if (timeout.current) {
4868
clearTimeout(timeout.current);
4969
timeout.current = null;
5070
}
5171
}
72+
5273
// if content script hasnt been found, set timer or immediately resolve
74+
// check our status object and see if contentScriptLaunched is false
5375
if (!status.contentScriptLaunched) {
76+
// if contentScriptLaunched is false, we check our loadingArray state at position [0]
5477
if (loadingArray[0] === true) {
78+
// if the element was true, then that means our timeout.current is still null so we now set it to a setTimeout function that will flip loadingArray[0] to false after 1.5 seconds
5579
timeout.current = setTimeout(() => {
5680
setLoadingArray(0, false);
5781
}, 1500);
5882
}
5983
} else {
84+
// if status.contentScriptLaunched is true, that means timeout.current !== null. This means that useEffect was triggered previously.
6085
setLoadingArray(0, false);
6186
}
87+
88+
// The next two if statements are written in a way to allow the checking of 'content script hook', 'reactDevTools check', and 'target page is a react app' to be run in chronological order.
6289
if (loadingArray[0] === false && status.contentScriptLaunched === true) {
6390
setLoadingArray(1, false);
6491
}
6592
if (loadingArray[1] === false && status.reactDevToolsInstalled === true) {
6693
setLoadingArray(2, false);
6794
}
6895

69-
//Unload async function when Error Container is unmounted
96+
// Unload async function when Error Container is unmounted
7097
return () => {
7198
clearTimeout(timeout.current);
7299
};
100+
101+
// within our dependency array, we're keeping track of if the status, currentTitle/tab, timeout, or loadingArray changes and we re-run the useEffect hook if they do
73102
}, [status, currentTitle, timeout, loadingArray]);
74103

75104
return (

0 commit comments

Comments
 (0)