import {type FC, useEffect, useRef, useState} from "react"; import {LogoDark} from "@/LogoDark.tsx"; import {__} from "@/Ulititlies.ts"; import {MorphingPopover, MorphingPopoverContent, MorphingPopoverTrigger} from "@/components/ui/morphing-popover.tsx"; import * as motion from 'motion/react-client'; import classNames from "classnames"; import {GlowEffect} from "../components/motion-primitives/glow-effect.tsx"; import {TextShimmer} from "../components/motion-primitives/text-shimmer.tsx"; import {activateLicense, deactivateLicense, loadLicenseData} from "@/licensing.ts"; import {useAtom} from "jotai"; import { hasLoadedLicenseAtom, isActivatingAtom, isDeactivatingAtom, licenseDataAtom, licenseErrorAtom, loadingActivationDataAtom } from "@/store/licenseStore.ts"; export type LicenseProps = { } /** * This component handles the license functionality, but it's not enabled currently as server-side licensing is not deployed yet. * This is only for internal testing currently. */ export const License: FC = ({}) => { const [loadingActivationData, setLoadingActivationData] = useAtom(loadingActivationDataAtom); const [licenseData, setLicenseData] = useAtom(licenseDataAtom); const [hasLoaded, setHasLoaded] = useAtom(hasLoadedLicenseAtom); const [isActivating, setIsActivating] = useAtom(isActivatingAtom); const licenseKeyRef = useRef(CouponsPlusWelcome.licenseKey || '') const [error, setError] = useAtom(licenseErrorAtom); const [isDeactivating, setIsDeactivating] = useAtom(isDeactivatingAtom); useEffect(() => { // Initialize license data from window object if not already set if (licenseData === undefined) { setLicenseData(CouponsPlusWelcome.licenseKey); } }, []); useEffect(() => { if (typeof licenseData === 'string' && licenseData.length > 0 && !hasLoaded) { setHasLoaded(true); setLoadingActivationData(true); // load the data const fetchData = async () => { try { const data = await loadLicenseData() setLicenseData(data); } catch (e) { setError(e as Error); } finally { setLoadingActivationData(false); } } fetchData(); } }, [licenseData, hasLoaded, setLicenseData, setLoadingActivationData, setError, setHasLoaded]) const handleActivate = () => { if (!licenseKeyRef.current) { return; } setIsActivating(true); setError(null); /** * Let's give it two seconds to make sure the transition looks smooth and to make the user feel like something is happening * we don't want it to be instant */ setTimeout(async () => { // now can we make the call try { const response = await activateLicense(licenseKeyRef.current); setLicenseData(licenseKeyRef.current); console.log('response', response); } catch (e) { console.log('Error activating license:', e); setError(e as Error); } finally { setIsActivating(false); } }, 2 * 1000); }; const handleDeactivate = () => { if (!licenseKeyRef.current) { return; } setIsDeactivating(true); setError(null); /** * Let's give it two seconds to make sure the transition looks smooth and to make the user feel like something is happening * we don't want it to be instant */ setTimeout(async () => { // now can we make the call try { const response = await deactivateLicense(); console.log('response', response); setLicenseData(undefined); licenseKeyRef.current = '' setHasLoaded(false) } catch (e) { console.log('Error deactivating license:', e); setError(e as Error); } finally { setIsDeactivating(false) } }, 100); }; const showInput = !licenseData return
!open && setIsActivating(false)}> {('PRO')} {showInput && <> { licenseKeyRef.current = e.target.value }} onPointerDown={(e) => e.stopPropagation()} onClick={(e) => e.stopPropagation()} className="m-0 !px-4 py-0 w-full !h-10 flex items-center border-[1px] !border-gray-200 !rounded-3 !bg-gray-100/50 text-gray-800 placeholder:text-gray-350 active:!shadow-none focus:!shadow-none focus:!border-gray-350 focus:!ring-[2px] focus:!ring-offset-2 focus:!ring-gray-200" placeholder={__('Enter your license key')} />

{__('Do not share your license key. Improper use (abuse) of your license key might result in the suspension of your account.')}

} {!showInput && !isDeactivating && licenseData && typeof licenseData !== 'string' &&
{licenseData.is_activated ? : }

{licenseData.is_activated ? __('License Activated. Enjoy.') : __('License Inactive')}

{licenseData.data.key}

{__('License key')}

}
{('PRO')} {licenseKeyRef.current && ( licenseKeyRef.current )} {false &&
Activating...
}
{licenseData && (typeof licenseData === 'string' ? !loadingActivationData : !isActivating) && } {error &&

{__('Cannot process license!')}

{error.message}

}
}