You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 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
Copy file name to clipboardExpand all lines: src/app/containers/ErrorContainer.tsx
+34-5Lines changed: 34 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -5,71 +5,100 @@ import Loader from '../components/Loader';
5
5
importErrorMsgfrom'../components/ErrorMsg';
6
6
import{useStoreContext}from'../store';
7
7
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
+
8
16
functionErrorContainer(): 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
9
18
const[store,dispatch]=useStoreContext();
19
+
// We continue to destructure store and get the tabs/currentTab/port
10
20
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.
// 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
13
27
consttitleTracker=useRef(currentTitle);
14
28
consttimeout=useRef(null);
15
29
30
+
// function that launches the main app
16
31
functionlaunch(): void{
17
32
dispatch(launchContentScript(tabs[currentTab]));
18
33
}
19
34
20
-
// check if tabObj exists > set status
35
+
// We create a status object that we may use later if tabs[currentTab] exists
21
36
conststatus={
22
37
contentScriptLaunched: false,
23
38
reactDevToolsInstalled: false,
24
39
targetPageisaReactApp: false,
25
40
};
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
26
43
if(tabs[currentTab]){
27
44
Object.assign(status,tabs[currentTab].status);
28
45
}
29
46
30
47
// hook that sets timer while waiting for a snapshot from the background script, resets if the tab changes/reloads
31
48
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
// this conditional helps us avoid unecessary state changes if the element and the value are already the same
33
52
if(loadingArray[i]!==value){
34
-
// avoid unecessary state changes
35
53
constloadingArrayClone=[...loadingArray];
36
54
loadingArrayClone[i]=value;
37
55
setLoading(loadingArrayClone);
38
56
}
39
57
}
40
58
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]
42
60
if(titleTracker.current!==currentTitle){
43
61
titleTracker.current=currentTitle;
62
+
44
63
setLoadingArray(0,true);
45
64
setLoadingArray(1,true);
46
65
setLoadingArray(2,true);
66
+
47
67
if(timeout.current){
48
68
clearTimeout(timeout.current);
49
69
timeout.current=null;
50
70
}
51
71
}
72
+
52
73
// if content script hasnt been found, set timer or immediately resolve
74
+
// check our status object and see if contentScriptLaunched is false
53
75
if(!status.contentScriptLaunched){
76
+
// if contentScriptLaunched is false, we check our loadingArray state at position [0]
54
77
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
55
79
timeout.current=setTimeout(()=>{
56
80
setLoadingArray(0,false);
57
81
},1500);
58
82
}
59
83
}else{
84
+
// if status.contentScriptLaunched is true, that means timeout.current !== null. This means that useEffect was triggered previously.
60
85
setLoadingArray(0,false);
61
86
}
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.
//Unload async function when Error Container is unmounted
96
+
//Unload async function when Error Container is unmounted
70
97
return()=>{
71
98
clearTimeout(timeout.current);
72
99
};
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
0 commit comments