import TransitionWrapper from "@/common/TransitionWrapper"; import Button from "@/components/Button"; import Card, { CardContent, CardFooter } from "@/components/Card"; import Notice from "@/components/Notice"; import { Input } from "@/components/ui/input"; import { spcApi } from "@/lib/api"; import { LINKS } from "@/lib/constants"; import { cn, formatDate } from "@/lib/utils"; import { useAppStore } from "@/store/store"; import { useMemo, useState } from "@wordpress/element"; import { __ } from "@wordpress/i18n"; import { CheckCircle, Copy, KeySquare } from "lucide-react"; import { toast } from "sonner"; const LicenseCard = () => { const { licenseData } = useAppStore(); const { key = '', expires = '', license: licenseState, customer_email: email, item_name: plan, activations_left: activationsLeft, } = licenseData; const valid = licenseState === 'valid'; const cloakedKey = useMemo(() => { if (!key) return ''; return '•'.repeat(Math.min(key.length - 4, 28)) + key.substring(key.length - 4); }, [key]); const cloakedEmail = useMemo(() => { if (!email) return ''; const [localPart, domainPart] = email.split('@'); const [domain, tld] = domainPart.split('.'); return `${localPart.substring(0, 2)}${'•'.repeat(localPart.length - 2)}@${domain.substring(0, 2)}${'•'.repeat(domain.length - 2)}.${tld}`; }, [email]); const [notification, setNotification] = useState(null) const [loading, setLoading] = useState(false); const [inputValue, setInputValue] = useState(key); const { asyncLocked, lockAsync, setLicenseData } = useAppStore(); let timeout = null; const toggleLicense = async (e) => { e.preventDefault(); setLoading(true); lockAsync(true); clearTimeout(timeout); setNotification(null); const formData = new FormData(e.currentTarget); const keyToSend = valid ? key : formData.get('licenseKey') as string; const response = await spcApi.toggleLicenseKey({ action: valid ? 'deactivate' : 'activate', key: keyToSend }) if (!response.success) { setNotification({ type: 'error', message: response.message }); timeout = setTimeout(() => { setNotification(null); }, 5000); setLoading(false); lockAsync(false); return; } if (valid) { setInputValue(''); } setLicenseData(response.data.license); setNotification({ type: 'success', message: response.data.message, }); timeout = setTimeout(() => { setNotification(null); }, 5000); setLoading(false); lockAsync(false); } return (
{valid ? ( ) : ( )}

{valid ? __('License Activated Successfully!', 'wp-cloudflare-page-cache') : __('Activate Super Page Cache Pro', 'wp-cloudflare-page-cache') }

{valid ? __('Super Page Cache Pro is now active on your site.', 'wp-cloudflare-page-cache') : __('Enter your license key to unlock Pro features', 'wp-cloudflare-page-cache') }

{!valid && (<>
setInputValue(e.target.value)} disabled={valid || loading} name="licenseKey" required />

{__('Your license key should be 32-40 characters long and contain letters and numbers.', 'wp-cloudflare-page-cache')}

)} {valid && (<>
{__('Licensed to', 'wp-cloudflare-page-cache')}
{cloakedEmail}
{__('Plan', 'wp-cloudflare-page-cache')}
{plan}
{__('Expires', 'wp-cloudflare-page-cache')}
{formatDate(expires)}
{__('Activations Left', 'wp-cloudflare-page-cache')}
{activationsLeft}
{__('License Key', 'wp-cloudflare-page-cache')}
{cloakedKey}
)}
{ notification && ( ) }
) } export default LicenseCard;