-
Notifications
You must be signed in to change notification settings - Fork 27
/
popup.jsx
348 lines (330 loc) · 11 KB
/
popup.jsx
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import React, { useState, useEffect } from "react";
import "./css/popup.css";
import * as Client from "@web3-storage/w3up-client";
function Popup() {
const [email, setEmail] = useState("");
const [spaceName, setSpaceName] = useState("");
const [spaceDID, setSpaceDID] = useState("");
const [isCreatingSpace, setIsCreatingSpace] = useState(false);
const [isAuthenticated, setIsAuthenticated] = useState(false);
const [emailVerified, setEmailVerified] = useState(false);
const [isLoginProcessStarted, setIsLoginProcessStarted] = useState(false);
const [account, setAccount] = useState(null);
const [popupWindow, setPopupWindow] = useState(null);
const [paymentPlanSelected, setPaymentPlanSelected] = useState(false);
const [loadingPaymentPlan, setLoadingPaymentPlan] = useState(true);
// Effect for handling login continuation and initial setup
useEffect(() => {
chrome.storage.local.get(
[
"spaceDID",
"spaceName",
"emailVerified",
"loginStarted",
"email",
"paymentPlanSelected",
],
async function (result) {
if (result.spaceDID) {
setSpaceDID(result.spaceDID);
setIsAuthenticated(true);
}
if (result.spaceName) {
setSpaceName(result.spaceName);
}
setEmailVerified(result.emailVerified || false);
setPaymentPlanSelected(result.paymentPlanSelected || false);
setEmail(result.email || "");
if (result.loginStarted) {
setIsLoginProcessStarted(true);
const client = await Client.create();
const fetchedAccount = await client.login(result.email);
setAccount(fetchedAccount);
// Check payment plan status
await checkPaymentPlan(fetchedAccount);
// Update local storage to reflect changes
chrome.storage.local.set(
{ emailVerified: true, loginStarted: false },
() => {
setEmailVerified(true);
setIsLoginProcessStarted(false);
if (popupWindow) {
try {
popupWindow.focus();
} catch (error) {
console.error("Error focusing the popup window:", error);
}
}
}
);
}
}
);
}, [popupWindow]);
// Separate effect for rechecking payment plan when needed
useEffect(() => {
if (emailVerified && !paymentPlanSelected && email) {
const fetchAndCheckPaymentPlan = async () => {
const client = await Client.create();
const fetchedAccount = await client.login(email);
setAccount(fetchedAccount);
await checkPaymentPlan(fetchedAccount);
};
fetchAndCheckPaymentPlan();
}
}, [emailVerified, paymentPlanSelected, email]);
const checkPaymentPlan = async (account) => {
try {
setLoadingPaymentPlan(true);
const res = await account.plan.get();
if (res.ok) {
setPaymentPlanSelected(true);
} else {
console.log("Waiting for payment plan to be selected...");
}
} catch (error) {
console.error("Error checking payment plan:", error);
} finally {
setLoadingPaymentPlan(false);
}
};
const handleEmailSubmit = async (e) => {
e.preventDefault();
// Set a flag in local storage
chrome.storage.local.set({ loginStarted: true, email: email }, function () {
// Calculate the top right position
const width = 400;
const height = 628;
const left = window.screen.availWidth - width;
const top = 0;
// Open the popup in a new window and specify the position
const popupUrl = chrome.runtime.getURL("popup.html");
const windowFeatures = `width=${width},height=${height},left=${left},top=${top},resizable=no`;
const newWindow = window.open(popupUrl, "DScanPopup", windowFeatures);
setPopupWindow(newWindow);
window.close();
setIsLoginProcessStarted(true);
});
};
const handleSpaceSubmit = async (e) => {
e.preventDefault();
if (!paymentPlanSelected) {
alert("Please select a payment plan before creating a space.");
return;
}
setIsCreatingSpace(true);
const client = await Client.create();
// Check if account is null and re-login if necessary
if (!account) {
if (email) {
try {
const reFetchedAccount = await client.login(email);
setAccount(reFetchedAccount);
proceedWithSpaceRegistration(client, reFetchedAccount);
} catch (error) {
console.error("Failed to re-login:", error);
alert("Failed to login. Please try again.");
return;
}
} else {
alert("No account found. Please log in first.");
return;
}
} else {
proceedWithSpaceRegistration(client, account);
}
};
const proceedWithSpaceRegistration = async (client, account) => {
const space = await client.createSpace(spaceName);
// Provision the space with the user's account
await account.provision(space.did());
// Save space to store and set as current
await space.save();
await client.setCurrentSpace(space.did());
const recovery = await space.createRecovery(account.did());
await client.capability.access.delegate({
space: space.did(),
delegations: [recovery],
});
const spaceDID = space.did();
setSpaceDID(spaceDID);
setIsCreatingSpace(false);
chrome.storage.local.set(
{ spaceDID, spaceName, emailVerified: true },
function () {
setIsAuthenticated(true);
setEmailVerified(true);
}
);
};
const handleRemoveSpace = () => {
chrome.storage.local.get("email", function (result) {
if (result.email) {
setEmail(result.email);
// Set emailVerified to true to show registration UI
setEmailVerified(true);
setIsAuthenticated(false);
setSpaceDID("");
setSpaceName("");
chrome.storage.local.set(
{ spaceDID: null, spaceName: null, emailVerified: true },
() => {
// Clear the account state
setAccount(null);
}
);
} else {
console.error("Email not found in storage.");
}
});
};
function formatKey(str) {
if (str.length > 35) {
return (
str.substr(0, 23) + "..." + str.substr(str.length - 10, str.length)
);
}
return str;
}
const handleContinueWithSavedSpace = () => {
window.location.href = "file.html";
};
return (
<div>
<section className="center">
{isAuthenticated ? (
<div>
<p style={{ textAlign: "center" }}>
Your current space is <b>{spaceName}</b>
<span className="saved-key">{formatKey(spaceDID)}</span>
</p>
<button className="remove-btn" onClick={handleRemoveSpace}>
Remove Space
</button>
<button
className="continue-btn"
onClick={handleContinueWithSavedSpace}
>
Continue with Saved Space
</button>
<div class="instructions">
<p className="w3console">
<code>
Go to{" "}
<a
href="https://console.web3.storage/"
className="instructions-link"
target="_blank"
rel="noopener noreferrer"
>
console.web3.storage ⁂
</a>{" "}
and log in to manage your account.
</code>
</p>
</div>
</div>
) : emailVerified ? (
<div>
<p>Create a space to upload data 🗳️</p>
<form onSubmit={handleSpaceSubmit}>
<input
id="spaceName"
type="text"
value={spaceName}
placeholder="Enter your space name"
onChange={(e) => setSpaceName(e.target.value)}
required
/>
<button className="space-btn">Create</button>
</form>
{emailVerified && !paymentPlanSelected && !loadingPaymentPlan && (
<div className="billing">
<p className="payment-warning">
Please select a plan, like the free tier, and enter payment
details to create a space.
</p>
<div className="paywall-msg">
<p>Why is there a paywall?</p>
<p>
web3.storage encounters daily spam from hundreds of
accounts, making it challenging for their team to manage.
</p>
</div>
</div>
)}
{isCreatingSpace ? <p>creating...</p> : ""}
</div>
) : isLoginProcessStarted ? (
<div>
<p>
Please click on the link sent to 📥 {email} to verify your
identity..
</p>
<br />
<p style={{ fontSize: "small", color: "red" }}>
DO NOT close this window until you've verified your email.
</p>
</div>
) : (
<div>
<p>Log in / Sign up with your email 🔐</p>
<form onSubmit={handleEmailSubmit}>
<input
id="email"
type="email"
value={email}
placeholder="Enter your email"
onChange={(e) => setEmail(e.target.value)}
required
/>
<button className="login-btn">Login</button>
</form>
<div class="instructions">
<ul>
<p>
<code>What's new in DScan v4.0?</code>
</p>
<code>
<li>- Self-sovereign identity</li>
</code>
<br />
<code>
<li>- User-owned storage</li>
</code>
<br />
<code>
<li>- Customizable QR code styles and logo integration</li>
</code>
<br />
<code>
<li>
Learn more about w3up in this{" "}
<a
href="https://blog.web3.storage/posts/the-data-layer-is-here-with-the-new-web3-storage"
className="instructions-link"
target="_blank"
rel="noopener noreferrer"
>
blog
</a>
</li>
</code>
</ul>
<br />
<br />
<br />
<br />
<center>
<p>
<code>Own your identity, own your data!</code>
</p>
</center>
</div>
</div>
)}
</section>
</div>
);
}
export default Popup;