diff --git a/src/components/system-info.tsx b/src/components/system-info.tsx index 43db77f..76ac837 100644 --- a/src/components/system-info.tsx +++ b/src/components/system-info.tsx @@ -8,6 +8,8 @@ import HideIcon from "@/assets/icons/hide-icon.png"; import { Input } from "./ui/input"; import { Button } from "./ui/button"; import { ReloadIcon } from "@radix-ui/react-icons"; +import { LicenseCenter } from "@/middlelayers/license"; +import toast from "react-hot-toast"; const App = () => { const [version, setVersion] = useState("0.1.0"); @@ -43,7 +45,19 @@ const App = () => { } setSaveLicenseLoading(true); - saveLicense(license).then(() => {}); + LicenseCenter.getInstance() + .validateLicense(license) + .then((result) => { + if (result.isPro) { + saveLicense(license); + toast.success("License Key Saved"); + } else { + toast.error("Invalid License Key"); + } + }) + .finally(() => { + setSaveLicenseLoading(false); + }); } function onLicenseInputChange(val: string) { @@ -102,7 +116,7 @@ const App = () => { onClick={onSaveLicenseClick} disabled={saveLicenseLoading || !licenseChanged} > - {saveLicenseLoading ?? ( + {saveLicenseLoading && ( )} Active diff --git a/src/middlelayers/license.ts b/src/middlelayers/license.ts new file mode 100644 index 0000000..25efdfd --- /dev/null +++ b/src/middlelayers/license.ts @@ -0,0 +1,39 @@ +import { getClientID } from '@/utils/app' +import { sendHttpRequest } from './datafetch/utils/http' + +export class LicenseCenter { + private static instance: LicenseCenter + + private readonly validateEndpoint = "https://track3-pro-api.domc.me/api/license/validate" + + private constructor() { } + + public static getInstance(): LicenseCenter { + if (!LicenseCenter.instance) { + LicenseCenter.instance = new LicenseCenter() + } + + return LicenseCenter.instance + } + + public async validateLicense(license: string): Promise<{ + isPro: boolean, + }> { + try { + const resp = await sendHttpRequest<{ + isPro: boolean + }>("POST", this.validateEndpoint, 10000, { + "x-track3-client-id": await getClientID(), + 'x-track3-api-key': license + }) + + return { + isPro: resp.isPro + } + } catch (e) { + return { + isPro: false + } + } + } +}