-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.tsx
154 lines (137 loc) · 4.47 KB
/
options.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
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
import { useEffect, useState } from "react"
import { Storage } from "@plasmohq/storage"
import "./style.css"
const storage = new Storage()
interface BlinkoConfig {
blinkoInstance: string
blinkoToken: string
}
function OptionsPage() {
const [config, setConfig] = useState<BlinkoConfig>({
blinkoInstance: "",
blinkoToken: ""
})
const [error, setError] = useState("")
const [isLoading, setIsLoading] = useState(false)
const [showSuccess, setShowSuccess] = useState(false)
useEffect(() => {
loadConfig()
}, [])
const loadConfig = async () => {
const savedConfig = await storage.get("blinkoConfig")
if (savedConfig) {
setConfig(JSON.parse(savedConfig))
}
}
const validateConfig = async (config: BlinkoConfig): Promise<boolean> => {
try {
const response = await fetch(`${config.blinkoInstance}/api/v1/note/list`, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${config.blinkoToken}`
},
body: JSON.stringify({
page: 1,
size: 1,
orderBy: "desc",
type: -1,
isArchived: false,
isRecycle: false,
searchText: ""
})
})
return response.ok
} catch (e) {
return false
}
}
const handleUpdate = async () => {
setError("")
setShowSuccess(false)
setIsLoading(true)
const isValid = await validateConfig(config)
if (!isValid) {
setError("blinko实例地址或者token错误,请检查")
setIsLoading(false)
return
}
await storage.set("blinkoConfig", JSON.stringify(config))
setShowSuccess(true)
setTimeout(() => {
setShowSuccess(false)
}, 2000)
setIsLoading(false)
}
const handleClose = () => {
window.close()
}
return (
<div className="w-[480px] min-h-[400px] bg-white p-8">
<div className="flex justify-between items-center mb-8 -mx-8 -mt-8 px-8">
<h1 className="text-2xl font-bold text-gray-800 bg-purple-200 px-4 py-2 rounded-r-full w-[400px]">save to blinko</h1>
<button
onClick={handleClose}
className="text-gray-500 hover:text-gray-700">
<svg xmlns="http://www.w3.org/2000/svg" className="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
</svg>
</button>
</div>
<div className="space-y-6">
<div>
<h2 className="text-xl font-medium text-gray-800 mb-4">config</h2>
<div className="space-y-4">
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
blinko instance address:
</label>
<input
type="text"
className="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:border-blue-500"
value={config.blinkoInstance}
onChange={(e) =>
setConfig({ ...config, blinkoInstance: e.target.value })
}
placeholder="https://blinko.jiahongw.com"
/>
</div>
<div>
<label className="block text-sm font-medium text-gray-700 mb-1">
blinko access token:
</label>
<input
type="password"
className="w-full px-3 py-2 border border-gray-200 rounded-lg focus:outline-none focus:border-blue-500"
value={config.blinkoToken}
onChange={(e) =>
setConfig({ ...config, blinkoToken: e.target.value })
}
placeholder="token..."
/>
</div>
</div>
</div>
{error && (
<div className="bg-red-50 text-red-600 p-3 rounded-lg text-sm">
{error}
</div>
)}
{showSuccess && (
<div className="bg-green-50 text-green-600 p-3 rounded-lg text-sm">
配置更新成功
</div>
)}
<div className="flex justify-end">
<button
onClick={handleUpdate}
disabled={isLoading}
className="w-32 py-2 px-4 bg-yellow-100 hover:bg-yellow-200 text-gray-800 font-medium rounded-lg transition-colors">
{isLoading ? "updating..." : "update"}
</button>
</div>
</div>
</div>
)
}
export default OptionsPage