-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.tsx
116 lines (101 loc) · 3.03 KB
/
popup.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { createRoot } from "react-dom/client";
import React from "react";
import { SetWIF } from "./views/SetWIF";
import { Sign } from "./views/Sign";
import Navigator from "./components//Navigator";
import ROUTES from "./Routes";
import Help from "./views/Help";
import { LogIn } from "./views/LogIn";
declare var chrome: any;
function App() {
const [route, setRoute] = React.useState(ROUTES.INITIALIZING);
const [wif, setWIF] = React.useState(null);
const [isRavenRebels, setIsRavenRebels] = React.useState(false);
const [orderRef, setOrderRef] = React.useState<null | string>(null);
const [
userHasToldIsNotToUseInstantSignIn,
setUserHasToldIsNotToUseInstantSignIn,
] = React.useState(false);
/* at startup read the saved value privateKeyWIF */
React.useEffect(() => {
chrome.storage.sync.get("privateKeyWIF", ({ privateKeyWIF }) => {
setWIF(privateKeyWIF);
setRoute(ROUTES.SIGN);
});
}, []);
/* Check if we can have instant sign in / log on / log in */
const checkForOrderRef = async () => {
if (userHasToldIsNotToUseInstantSignIn === true) {
return null;
}
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true,
});
if (!tab) {
return null;
}
const response = await chrome.tabs.sendMessage(tab.id, {
popup: { status: "alive" },
});
if (response && response.url) {
const URL = response.url;
const index = URL.indexOf("?");
const search = URL.substring(index);
const params = new URLSearchParams(search);
setOrderRef(params.get("orderRef"));
setIsRavenRebels(true);
}
};
React.useEffect(() => {
//Tell the world we are opened
checkForOrderRef();
const interval = setInterval(checkForOrderRef, 2 * 1000);
return () => {
clearInterval(interval); //clean up interval when componet is un-mounted
};
}, []);
if (
isRavenRebels &&
wif &&
orderRef &&
userHasToldIsNotToUseInstantSignIn === false
) {
const cancel = () => {
setUserHasToldIsNotToUseInstantSignIn(true);
setOrderRef(null);
};
return <LogIn orderRef={orderRef} wif={wif} onCancel={cancel} />;
}
const CurrentView = () => {
if (route === ROUTES.SIGN) {
return <Sign setRoute={setRoute} wif={wif} setWIF={setWIF} />;
} else if (route === ROUTES.SET_WIF) {
return (
<SetWIF
setWIF={setWIF}
wif={wif}
onSuccess={() => {
setRoute(ROUTES.SIGN);
}}
/>
);
} else if (route === ROUTES.HELP) {
return <Help />;
}
return null;
};
return (
<div>
<Navigator setRoute={setRoute} currentRoute={route} />
<h1 className="heading">Ravencoin</h1>
<img className="logo" src="./ravencoin-rvn-logo.png"></img>
<CurrentView />
</div>
);
}
// After
const container =
document.getElementById("app") || document.createDocumentFragment();
const root = createRoot(container); // createRoot(container!) if you use TypeScript
root.render(<App />);