-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathActivateLicense.tsx
292 lines (269 loc) · 9.4 KB
/
ActivateLicense.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
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
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import {
Tooltip,
TooltipContent,
TooltipProvider,
TooltipTrigger,
} from "@/components/ui/tooltip";
import { useState } from "react";
import { useLicenseKey } from "@/hooks/useLicenseKey";
import { storage } from "wxt/storage";
import { CheckCircleIcon, ArrowPathIcon } from "@heroicons/react/24/solid";
import { encryptData } from "@/lib/cryptoUtils";
import { generatePhrase } from "@/lib/namegenerator";
const handshakePassword = import.meta.env.VITE_QUICKPEEK_HANDSHAKE_PASSWORD;
// Add this type definition
type LicenseStorageData = {
license_key: string;
status: string;
activation_limit: string;
activation_usage: string;
created_at: string;
expires_at: string;
test_mode: string;
instance_name: string;
store_id: string;
order_id: string;
order_item_id: string;
variant_name: string;
product_name: string;
customer_name: string;
customer_email: string;
saved_date: string;
};
// Update storage definition
const licenseStorage = storage.defineItem<LicenseStorageData | null>(
"sync:licenseData",
{
fallback: null,
version: 1,
}
);
async function storeLicenseData(data: any) {
try {
const encryptedData = {
license_key: JSON.stringify(await encryptData(data.license_key.key)),
status: JSON.stringify(await encryptData(data.license_key.status)),
activation_limit: JSON.stringify(
await encryptData(data.license_key.activation_limit)
),
activation_usage: JSON.stringify(
await encryptData(data.license_key.activation_usage)
),
created_at: JSON.stringify(
await encryptData(data.license_key.created_at)
),
expires_at: JSON.stringify(
await encryptData(data.license_key.expires_at)
),
test_mode: JSON.stringify(await encryptData(data.license_key.test_mode)),
instance_name: JSON.stringify(
await encryptData(data.instance?.name || null)
),
store_id: JSON.stringify(await encryptData(data.meta.store_id)),
order_id: JSON.stringify(await encryptData(data.meta.order_id)),
order_item_id: JSON.stringify(await encryptData(data.meta.order_item_id)),
variant_name: JSON.stringify(await encryptData(data.meta.variant_name)),
product_name: JSON.stringify(await encryptData(data.meta.product_name)),
customer_name: JSON.stringify(await encryptData(data.meta.customer_name)),
customer_email: JSON.stringify(
await encryptData(data.meta.customer_email)
),
saved_date: JSON.stringify(
await encryptData(new Date().toISOString().split("T")[0])
),
};
await licenseStorage.setValue(encryptedData);
console.log("License data saved successfully");
} catch (error) {
console.error("Error storing license data:", error);
throw new Error("Failed to store license data");
}
}
const ActivateLicense = () => {
const [activationKey, setActivationKey] = useState("");
const [userName, setUserName] = useState("");
const [loading, setLoading] = useState({
activation: false,
validation: false,
});
const handleActivate = async (e: React.FormEvent) => {
const instanceName = generatePhrase();
e.preventDefault();
if (!activationKey.trim()) {
alert("Please enter a license key");
return;
}
setLoading((prev) => ({ ...prev, activation: true }));
try {
const response = await fetch(
"https://quickpeek.vercel.app/api/activatelicense",
{
method: "POST",
headers: {
"Content-Type": "application/json",
// Add any additional headers if needed
},
body: JSON.stringify({
license_key: activationKey,
instance_name: userName ? userName : instanceName,
handshake_password: handshakePassword,
}),
}
);
const data = await response.json();
console.log(data);
if (!response.ok) {
throw new Error(`Error: ${data.message || "Unknown error"}`);
}
// Check if store_id matches the required values
if (data.meta?.store_id === 134128 || data.meta?.store_id === 132851) {
// Store the license data
await storeLicenseData(data);
console.log("License data stored successfully");
// Update the licenseKey state
refreshLicenseData();
// toast.success("License activated successfully!", {
// duration: 2000,
// position: "bottom-right",
// });
setActivationKey(""); // Clear input field
setUserName(""); // Clear input field
// Reload the page after a delay
setTimeout(() => {
window.location.reload();
}, 1000);
} else {
console.log(
"Store ID does not match required values. License data not stored."
);
}
} catch (error) {
console.error("Activation error:", error);
// toast.error("Failed to activate license. Please try again.", {
// duration: 2000,
// position: "bottom-right",
// });
} finally {
setLoading((prev) => ({ ...prev, activation: false }));
}
};
const { licenseData, refreshLicenseData } = useLicenseKey();
// Helper function to mask the license key
const maskLicenseKey = (licenseKey: string): string => {
if (!licenseKey) return "No license found"; // Handle empty or undefined keys
// Split the license key into segments
const segments = licenseKey.split("-");
// Mask the middle segments
return segments
.map((segment, index) => (index >= 1 && index <= 3 ? "XXXX" : segment))
.join("-");
};
// Function to handle copy to clipboard
const handleCopy = async (licenseKey: string) => {
if (licenseKey) {
try {
await navigator.clipboard.writeText(licenseKey);
} catch (err) {
console.error("Failed to copy text: ", err);
// toast.error("Failed to copy license key to clipboard", {
// duration: 2000,
// position: "bottom-right",
// });
}
}
};
return (
<div className="space-y-4">
{/* License Status Card */}
<div className="rounded-lg border p-2">
<div className="flex flex-col space-y-4 sm:flex-row sm:justify-between sm:items-center sm:space-y-0">
<div className="flex items-center space-x-2">
<TooltipProvider>
<Tooltip>
<TooltipTrigger>
<p
className="font-medium text-xs sm:text-base cursor-pointer hover:opacity-75 transition-opacity"
onClick={() => handleCopy(licenseData?.license_key || "")}
>
{licenseData?.license_key
? maskLicenseKey(licenseData.license_key)
: "No license found"}
</p>
</TooltipTrigger>
<TooltipContent>
<p className="text-xs">Click to copy</p>
</TooltipContent>
</Tooltip>
</TooltipProvider>
</div>
<div>
{licenseData?.status === "active" ? (
<div className="inline-flex items-center space-x-2 border border-green-600 text-green-700 px-3 py-1 rounded-full">
<CheckCircleIcon className="w-4 h-4" />
<span className="text-xs font-medium">
{licenseData?.status.toUpperCase()}
</span>
</div>
) : licenseData?.status === "disabled" ||
licenseData?.status === "inactive" ? (
<div className="text-red-600 text-sm font-medium">
{licenseData?.status.toUpperCase()}
</div>
) : (
<div className="text-gray-500 text-sm">
Checking license status...
</div>
)}
</div>
</div>
</div>
{/* Activation Form */}
<form className="rounded-lg border p-4" onSubmit={handleActivate}>
<div className="space-y-4">
<div>
<Label htmlFor="activationKey" className="text-sm font-medium">
License Key
</Label>
<Input
type="text"
id="activationKey"
value={activationKey}
onChange={(e) => setActivationKey(e.target.value)}
placeholder="Enter your license key"
disabled={loading.activation}
className="mt-1 text-sm"
/>
</div>
<div>
<Label htmlFor="userName" className="text-sm font-medium">
Your Name (Optional)
</Label>
<Input
type="text"
id="userName"
value={userName}
onChange={(e) => setUserName(e.target.value)}
placeholder="Enter your name"
disabled={loading.activation}
className="mt-1 text-sm"
/>
</div>
<Button
type="submit"
disabled={loading.activation}
className="w-full"
>
{loading.activation && (
<ArrowPathIcon className="w-4 h-4 mr-2 animate-spin" />
)}
{loading.activation ? "Activating..." : "Activate License"}
</Button>
</div>
</form>
</div>
);
};
export default ActivateLicense;