-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.tsx
88 lines (78 loc) · 2.32 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
import React, { useEffect, useState } from 'react'
import { Storage } from "@plasmohq/storage"
import { useStorage } from "@plasmohq/storage/hook"
const popup = () => {
const storage = new Storage()
const [loading, setLoading] = useState(true)
const [user, setUser] = useStorage("user", null)
/**
* Asynchronously retrieves the user's cookie from the Chrome browser and the local storage.
* If the cookie exists, it checks if the stored cookie is the same as the current cookie.
* If they are not the same, it updates the user's cookie.
* If the cookie does not exist, it sets the user to null and clears the Chrome storage.
* Finally, it sets the loading state to false.
*
* @async
* @function getCookie
*/
async function getCookie() {
let currentCookie: chrome.cookies.Cookie | string | null = await chrome.cookies.get({ "url": "https://localhost:5173", "name": "user-token" });
let currentStoredCookie = await storage.get("user")
if (currentCookie) {
currentCookie = currentCookie.value
if (currentStoredCookie !== currentCookie) {
setUser(currentCookie)
}
}
else {
setUser(null)
await chrome.storage.sync.clear();
}
setLoading(false)
}
useEffect(() => {
getCookie()
}, [])
if (loading || user === null) return (<div>Loading...</div>)
return (
<div
style={{
display: "flex",
flexWrap: "wrap",
flexDirection: "column",
alignItems: "center",
width: "200px",
height: "300px",
justifyContent: "center",
}}
>
{!!user && (
<>
<h5>Hi, {user}</h5>
<button
title="Log out"
onClick={async () => {
await chrome.storage.sync.clear();
await chrome.cookies.remove({ "url": "https://localhost:5173", "name": "user-token" });
}}
>
Logout
</button>
</>
)}
{!user && (
<>
<button
title="Sign in or Sign up"
onClick={() => {
chrome.tabs.create({ url: "http://localhost:5173" });
}}
>
Sign in or Sign up
</button>
</>
)}
</div>
)
}
export default popup