'use client'; import { useState } from 'react'; import { useParams } from 'next/navigation'; import { appConfig } from '../config/app'; import { SkinId } from '../types'; import { useLocalizedSkinName } from '../hooks/useLocalizedSkinName'; import { usePrices } from '../hooks/usePrices'; import { useFeature } from '../providers/FeatureProvider'; interface PremiumCheckoutProps { skinId: SkinId; onClose: () => void; } export function PremiumCheckout({ skinId, onClose }: PremiumCheckoutProps) { const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); const params = useParams(); const getLocalizedSkinName = useLocalizedSkinName(); const paymentsEnabled = useFeature('paymentsEnabled'); const { getPrice, loading: pricesLoading } = usePrices(); const skin = appConfig.skins[skinId]; const skinName = getLocalizedSkinName(skinId); const price = getPrice(skinId); const locale = params.locale as string; // Guard: never render if payments are disabled or skin is not premium if (!paymentsEnabled || !skin?.isPremium) { return null; } const handlePurchase = async () => { setIsLoading(true); setError(null); try { const response = await fetch('/api/checkout', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ skinId, locale }), }); const data = await response.json(); if (!response.ok) { throw new Error(data.error || 'Failed to create checkout'); } // Redirect to Lemon Squeezy checkout if (data.checkoutUrl) { window.location.href = data.checkoutUrl; } else { throw new Error('No checkout URL received'); } } catch (err) { console.error('Checkout error:', err); setError(err instanceof Error ? err.message : 'An error occurred'); } finally { setIsLoading(false); } }; return (

Premium Skin

{skinName}

{skinName}

Unlock this premium skin to customize your experience!

{pricesLoading ? '...' : (price ?? '')}
{error && (

{error}

)}

Secure payment powered by Lemon Squeezy

); }