mirror of
https://github.com/HugeFrog24/shakethefrog.git
synced 2026-03-02 00:14:33 +00:00
Fuckaway Windows line endings
This commit is contained in:
93
app/[locale]/checkout/cancel/page.tsx
Normal file
93
app/[locale]/checkout/cancel/page.tsx
Normal file
@@ -0,0 +1,93 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useFeature } from '../../../providers/FeatureProvider';
|
||||
|
||||
export default function CheckoutCancelPage() {
|
||||
const router = useRouter();
|
||||
const t = useTranslations('ui');
|
||||
const paymentsEnabled = useFeature('paymentsEnabled');
|
||||
const [countdown, setCountdown] = useState(5);
|
||||
|
||||
// Redirect home immediately if payments are disabled
|
||||
useEffect(() => {
|
||||
if (!paymentsEnabled) {
|
||||
router.replace('/');
|
||||
}
|
||||
}, [paymentsEnabled, router]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!paymentsEnabled) return;
|
||||
|
||||
// Countdown timer to redirect to home
|
||||
const timer = setInterval(() => {
|
||||
setCountdown((prev) => {
|
||||
if (prev <= 1) {
|
||||
clearInterval(timer);
|
||||
router.push('/');
|
||||
return 0;
|
||||
}
|
||||
return prev - 1;
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
return () => clearInterval(timer);
|
||||
}, [router, paymentsEnabled]);
|
||||
|
||||
if (!paymentsEnabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const handleGoBack = () => {
|
||||
router.push('/');
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-br from-gray-50 to-gray-100 dark:from-gray-900 dark:to-gray-800 flex items-center justify-center p-4">
|
||||
<div className="max-w-md w-full bg-white dark:bg-gray-800 rounded-lg shadow-xl p-8 text-center">
|
||||
{/* Cancel Icon */}
|
||||
<div className="w-16 h-16 mx-auto mb-6 bg-gray-100 dark:bg-gray-700 rounded-full flex items-center justify-center">
|
||||
<svg className="w-8 h-8 text-gray-600 dark:text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</div>
|
||||
|
||||
{/* Cancel Message */}
|
||||
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4">
|
||||
{t('checkout.cancel.title')}
|
||||
</h1>
|
||||
|
||||
<p className="text-gray-600 dark:text-gray-300 mb-6">
|
||||
{t('checkout.cancel.message')}
|
||||
</p>
|
||||
|
||||
<p className="text-gray-500 dark:text-gray-400 mb-6">
|
||||
{t('checkout.cancel.tryAgain')}
|
||||
</p>
|
||||
|
||||
{/* Action Buttons */}
|
||||
<div className="space-y-3">
|
||||
<button
|
||||
onClick={handleGoBack}
|
||||
className="w-full px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white rounded-lg transition-colors font-medium"
|
||||
>
|
||||
{t('checkout.cancel.backToApp')}
|
||||
</button>
|
||||
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
{t('checkout.cancel.redirecting', { countdown })}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Help Info */}
|
||||
<div className="mt-8 pt-6 border-t border-gray-200 dark:border-gray-700">
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400">
|
||||
{t('checkout.cancel.needHelp')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
124
app/[locale]/checkout/success/page.tsx
Normal file
124
app/[locale]/checkout/success/page.tsx
Normal file
@@ -0,0 +1,124 @@
|
||||
'use client';
|
||||
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useSearchParams, useRouter } from 'next/navigation';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { appConfig } from '../../../config/app';
|
||||
import { SkinId } from '../../../types';
|
||||
import { useLocalizedSkinName } from '../../../hooks/useLocalizedSkinName';
|
||||
import { useFeature } from '../../../providers/FeatureProvider';
|
||||
|
||||
export default function CheckoutSuccessPage() {
|
||||
const searchParams = useSearchParams();
|
||||
const router = useRouter();
|
||||
const t = useTranslations('ui');
|
||||
const getLocalizedSkinName = useLocalizedSkinName();
|
||||
const paymentsEnabled = useFeature('paymentsEnabled');
|
||||
const [countdown, setCountdown] = useState(5);
|
||||
|
||||
const skinId = searchParams.get('skin') as SkinId;
|
||||
const skin = skinId ? appConfig.skins[skinId] : null;
|
||||
const skinName = skinId ? getLocalizedSkinName(skinId) : '';
|
||||
|
||||
// Redirect home immediately if payments are disabled
|
||||
useEffect(() => {
|
||||
if (!paymentsEnabled) {
|
||||
router.replace('/');
|
||||
}
|
||||
}, [paymentsEnabled, router]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!paymentsEnabled) return;
|
||||
|
||||
// Countdown timer to redirect to home
|
||||
const timer = setInterval(() => {
|
||||
setCountdown((prev) => {
|
||||
if (prev <= 1) {
|
||||
clearInterval(timer);
|
||||
// Redirect to home with the purchased skin
|
||||
const params = new URLSearchParams();
|
||||
if (skinId && skinId !== appConfig.defaultSkin) {
|
||||
params.set('skin', skinId);
|
||||
}
|
||||
const newUrl = `/${params.toString() ? '?' + params.toString() : ''}`;
|
||||
router.push(newUrl);
|
||||
return 0;
|
||||
}
|
||||
return prev - 1;
|
||||
});
|
||||
}, 1000);
|
||||
|
||||
return () => clearInterval(timer);
|
||||
}, [skinId, router, paymentsEnabled]);
|
||||
|
||||
if (!paymentsEnabled) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const handleGoToApp = () => {
|
||||
const params = new URLSearchParams();
|
||||
if (skinId && skinId !== appConfig.defaultSkin) {
|
||||
params.set('skin', skinId);
|
||||
}
|
||||
const newUrl = `/${params.toString() ? '?' + params.toString() : ''}`;
|
||||
router.push(newUrl);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gradient-to-br from-green-50 to-blue-50 dark:from-gray-900 dark:to-gray-800 flex items-center justify-center p-4">
|
||||
<div className="max-w-md w-full bg-white dark:bg-gray-800 rounded-lg shadow-xl p-8 text-center">
|
||||
{/* Success Icon */}
|
||||
<div className="w-16 h-16 mx-auto mb-6 bg-green-100 dark:bg-green-900 rounded-full flex items-center justify-center">
|
||||
<svg className="w-8 h-8 text-green-600 dark:text-green-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
</div>
|
||||
|
||||
{/* Success Message */}
|
||||
<h1 className="text-2xl font-bold text-gray-900 dark:text-white mb-4">
|
||||
{t('checkout.success.title')}
|
||||
</h1>
|
||||
|
||||
{skin && (
|
||||
<div className="mb-6">
|
||||
<div className="w-20 h-20 mx-auto mb-4 bg-gray-100 dark:bg-gray-700 rounded-lg flex items-center justify-center">
|
||||
<img
|
||||
src={skin.normal}
|
||||
alt={skinName}
|
||||
className="w-16 h-16"
|
||||
/>
|
||||
</div>
|
||||
<p className="text-gray-600 dark:text-gray-300">
|
||||
{t('checkout.success.unlockedSkin', { skinName })}
|
||||
</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<p className="text-gray-500 dark:text-gray-400 mb-6">
|
||||
{t('checkout.success.thankYou')}
|
||||
</p>
|
||||
|
||||
{/* Action Buttons */}
|
||||
<div className="space-y-3">
|
||||
<button
|
||||
onClick={handleGoToApp}
|
||||
className="w-full px-6 py-3 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-colors font-medium"
|
||||
>
|
||||
{t('checkout.success.goToApp')}
|
||||
</button>
|
||||
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400">
|
||||
{t('checkout.success.redirecting', { countdown })}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Receipt Info */}
|
||||
<div className="mt-8 pt-6 border-t border-gray-200 dark:border-gray-700">
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400">
|
||||
{t('checkout.success.receiptSent')}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -31,17 +31,14 @@ export default function Home() {
|
||||
const getLocalizedSkinName = useLocalizedSkinName();
|
||||
const t = useTranslations('ui');
|
||||
|
||||
// Check if device motion is available and handle permissions
|
||||
const requestMotionPermission = async () => {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
// Check if device motion is available
|
||||
if (!('DeviceMotionEvent' in window)) {
|
||||
setMotionPermission('denied');
|
||||
return;
|
||||
}
|
||||
|
||||
// Request permission on iOS devices
|
||||
if ('requestPermission' in DeviceMotionEvent) {
|
||||
try {
|
||||
// @ts-expect-error - TypeScript doesn't know about requestPermission
|
||||
@@ -52,39 +49,32 @@ export default function Home() {
|
||||
setMotionPermission('denied');
|
||||
}
|
||||
} else {
|
||||
// Android or desktop - no permission needed
|
||||
setMotionPermission('granted');
|
||||
}
|
||||
};
|
||||
|
||||
const triggerShake = useCallback((intensity: number) => {
|
||||
// Use ref instead of state to prevent race conditions
|
||||
if (!isAnimatingRef.current) {
|
||||
// Clear any existing timeout
|
||||
if (animationTimeoutRef.current) {
|
||||
clearTimeout(animationTimeoutRef.current);
|
||||
}
|
||||
|
||||
// Start shake animation
|
||||
isAnimatingRef.current = true;
|
||||
animationStartTimeRef.current = Date.now(); // Track when animation starts
|
||||
animationStartTimeRef.current = Date.now();
|
||||
setIsAnimating(true);
|
||||
setIsShaken(true);
|
||||
setShakeIntensity(intensity);
|
||||
setShakeCount(count => count + 1);
|
||||
|
||||
// Reset shake after configured duration
|
||||
animationTimeoutRef.current = setTimeout(() => {
|
||||
setIsShaken(false);
|
||||
setShakeIntensity(0);
|
||||
setIsAnimating(false);
|
||||
isAnimatingRef.current = false;
|
||||
|
||||
// Process next shake in queue if any
|
||||
setShakeQueue(prev => {
|
||||
if (prev.length > 0) {
|
||||
const [nextIntensity, ...rest] = prev;
|
||||
// Small delay before triggering next shake to ensure clean transition
|
||||
setTimeout(() => {
|
||||
triggerShake(nextIntensity);
|
||||
}, 16);
|
||||
@@ -94,17 +84,15 @@ export default function Home() {
|
||||
});
|
||||
}, shakeConfig.animations.shakeReset);
|
||||
} else {
|
||||
// Only queue if we're not at the start of the animation
|
||||
const timeSinceStart = Date.now() - animationStartTimeRef.current;
|
||||
if (timeSinceStart > 100) { // Only queue if animation has been running for a bit
|
||||
if (timeSinceStart > 100) {
|
||||
setShakeQueue(prev => {
|
||||
// Hard limit at 1 item
|
||||
if (prev.length >= 1) return prev;
|
||||
return [...prev, intensity];
|
||||
});
|
||||
}
|
||||
}
|
||||
}, []); // Remove isAnimating from dependencies since we're using ref
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const handleKeyPress = (event: KeyboardEvent) => {
|
||||
@@ -133,7 +121,6 @@ export default function Home() {
|
||||
}
|
||||
};
|
||||
|
||||
// Only add motion listener if permission is granted
|
||||
if (typeof window !== 'undefined') {
|
||||
if (motionPermission === 'granted' && 'DeviceMotionEvent' in window) {
|
||||
window.addEventListener('devicemotion', handleMotion);
|
||||
@@ -151,20 +138,17 @@ export default function Home() {
|
||||
};
|
||||
}, [lastUpdate, motionPermission, triggerShake]);
|
||||
|
||||
// Initial permission check
|
||||
useEffect(() => {
|
||||
requestMotionPermission();
|
||||
}, []);
|
||||
|
||||
const handleClick = () => {
|
||||
// Trigger haptic feedback for tap interaction
|
||||
if ('vibrate' in navigator) {
|
||||
navigator.vibrate(50); // Short 50ms vibration
|
||||
}
|
||||
triggerShake(shakeConfig.defaultTriggerIntensity);
|
||||
};
|
||||
|
||||
// Add cleanup in the component
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (animationTimeoutRef.current) {
|
||||
@@ -249,3 +233,4 @@ export default function Home() {
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
107
app/api/checkout/route.ts
Normal file
107
app/api/checkout/route.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createCheckout } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { initializeLemonSqueezy, getLemonSqueezyConfig } from '../../config/lemonsqueezy';
|
||||
import { getFeatureFlags } from '../../config/features';
|
||||
import { appConfig } from '../../config/app';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { paymentsEnabled } = getFeatureFlags();
|
||||
if (!paymentsEnabled) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Payments are currently disabled' },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
|
||||
// Initialize Lemon Squeezy SDK
|
||||
initializeLemonSqueezy();
|
||||
|
||||
const { skinId, locale } = await request.json();
|
||||
|
||||
if (!skinId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Skin ID is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!locale) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Locale is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get skin configuration
|
||||
const skin = appConfig.skins[skinId as keyof typeof appConfig.skins];
|
||||
|
||||
if (!skin) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid skin ID' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!skin.isPremium) {
|
||||
return NextResponse.json(
|
||||
{ error: 'This skin is not premium' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!skin.variantId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Variant ID not configured for this skin' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create checkout session
|
||||
const config = getLemonSqueezyConfig();
|
||||
const checkout = await createCheckout(config.storeId, skin.variantId!, {
|
||||
productOptions: {
|
||||
name: `Premium ${skin.name} Skin`,
|
||||
description: `Unlock the premium ${skin.name} skin for Shake the Frog!`,
|
||||
redirectUrl: `${config.baseUrl}/${locale}/checkout/success?skin=${skinId}`,
|
||||
receiptButtonText: 'Go to App',
|
||||
receiptThankYouNote: 'Thank you for your purchase! Your premium skin is now available.',
|
||||
},
|
||||
checkoutOptions: {
|
||||
embed: false,
|
||||
media: false,
|
||||
logo: true,
|
||||
desc: true,
|
||||
discount: true,
|
||||
subscriptionPreview: true,
|
||||
buttonColor: '#16a34a'
|
||||
},
|
||||
checkoutData: {
|
||||
custom: {
|
||||
skin_id: skinId,
|
||||
},
|
||||
},
|
||||
testMode: process.env.NODE_ENV !== 'production',
|
||||
});
|
||||
|
||||
if (checkout.error) {
|
||||
console.error('Checkout creation error:', checkout.error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create checkout session' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
checkoutUrl: checkout.data?.data.attributes.url,
|
||||
checkoutId: checkout.data?.data.id,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Checkout API error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
33
app/api/prices/route.ts
Normal file
33
app/api/prices/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getVariant } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { initializeLemonSqueezy } from '../../config/lemonsqueezy';
|
||||
import { getFeatureFlags } from '../../config/features';
|
||||
import { appConfig } from '../../config/app';
|
||||
|
||||
export async function GET() {
|
||||
const { paymentsEnabled } = getFeatureFlags();
|
||||
if (!paymentsEnabled) {
|
||||
return NextResponse.json({ prices: {}, enabled: false });
|
||||
}
|
||||
|
||||
// Initialize Lemon Squeezy SDK
|
||||
initializeLemonSqueezy();
|
||||
|
||||
const prices: Record<string, string> = {};
|
||||
|
||||
// Fetch prices for all premium skins
|
||||
for (const [skinId, skin] of Object.entries(appConfig.skins)) {
|
||||
if (skin.isPremium && skin.variantId) {
|
||||
const variant = await getVariant(skin.variantId);
|
||||
|
||||
if (!variant.data) {
|
||||
throw new Error(`No variant data found for ${skinId}`);
|
||||
}
|
||||
|
||||
const priceInCents = variant.data.data.attributes.price;
|
||||
prices[skinId] = `$${(priceInCents / 100).toFixed(2)}`;
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ prices });
|
||||
}
|
||||
148
app/api/webhooks/lemonsqueezy/route.ts
Normal file
148
app/api/webhooks/lemonsqueezy/route.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createHmac } from 'crypto';
|
||||
import { getLemonSqueezyConfig } from '../../../config/lemonsqueezy';
|
||||
|
||||
// Webhook payload interface using proper typing
|
||||
interface WebhookPayload {
|
||||
meta: {
|
||||
event_name: string;
|
||||
custom_data?: Record<string, unknown>;
|
||||
};
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
attributes: Record<string, unknown>;
|
||||
relationships?: Record<string, unknown>;
|
||||
};
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.text();
|
||||
const signature = request.headers.get('x-signature');
|
||||
|
||||
if (!signature) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing signature' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify webhook signature
|
||||
const config = getLemonSqueezyConfig();
|
||||
const secret = config.webhookSecret;
|
||||
|
||||
const hmac = createHmac('sha256', secret);
|
||||
hmac.update(body);
|
||||
const digest = hmac.digest('hex');
|
||||
|
||||
if (signature !== digest) {
|
||||
console.error('Invalid webhook signature');
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid signature' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Parse webhook payload
|
||||
const payload = JSON.parse(body);
|
||||
const eventName = payload.meta?.event_name;
|
||||
|
||||
console.log('Received webhook:', eventName);
|
||||
|
||||
// Handle different webhook events
|
||||
switch (eventName) {
|
||||
case 'order_created':
|
||||
await handleOrderCreated(payload);
|
||||
break;
|
||||
case 'subscription_created':
|
||||
await handleSubscriptionCreated(payload);
|
||||
break;
|
||||
case 'subscription_updated':
|
||||
await handleSubscriptionUpdated(payload);
|
||||
break;
|
||||
case 'subscription_cancelled':
|
||||
await handleSubscriptionCancelled(payload);
|
||||
break;
|
||||
default:
|
||||
console.log('Unhandled webhook event:', eventName);
|
||||
}
|
||||
|
||||
return NextResponse.json({ received: true });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Webhook processing error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Webhook processing failed' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleOrderCreated(payload: WebhookPayload) {
|
||||
const order = payload.data;
|
||||
const attributes = order.attributes as Record<string, unknown>;
|
||||
const firstOrderItem = attributes.first_order_item as Record<string, unknown> | undefined;
|
||||
const customData = firstOrderItem?.product_name;
|
||||
|
||||
console.log('Order created:', {
|
||||
orderId: order.id,
|
||||
customerEmail: attributes.user_email,
|
||||
total: attributes.total_formatted,
|
||||
status: attributes.status,
|
||||
customData: customData,
|
||||
});
|
||||
|
||||
// Here you could:
|
||||
// - Send confirmation email
|
||||
// - Update user permissions in your database
|
||||
// - Log the purchase for analytics
|
||||
// - Grant access to premium features
|
||||
}
|
||||
|
||||
async function handleSubscriptionCreated(payload: WebhookPayload) {
|
||||
const subscription = payload.data;
|
||||
const attributes = subscription.attributes as Record<string, unknown>;
|
||||
|
||||
console.log('Subscription created:', {
|
||||
subscriptionId: subscription.id,
|
||||
customerEmail: attributes.user_email,
|
||||
status: attributes.status,
|
||||
productName: attributes.product_name,
|
||||
});
|
||||
|
||||
// Handle subscription creation
|
||||
// - Update user subscription status
|
||||
// - Send welcome email
|
||||
// - Grant premium access
|
||||
}
|
||||
|
||||
async function handleSubscriptionUpdated(payload: WebhookPayload) {
|
||||
const subscription = payload.data;
|
||||
const attributes = subscription.attributes as Record<string, unknown>;
|
||||
|
||||
console.log('Subscription updated:', {
|
||||
subscriptionId: subscription.id,
|
||||
status: attributes.status,
|
||||
endsAt: attributes.ends_at,
|
||||
});
|
||||
|
||||
// Handle subscription updates
|
||||
// - Update user access based on status
|
||||
// - Handle plan changes
|
||||
}
|
||||
|
||||
async function handleSubscriptionCancelled(payload: WebhookPayload) {
|
||||
const subscription = payload.data;
|
||||
const attributes = subscription.attributes as Record<string, unknown>;
|
||||
|
||||
console.log('Subscription cancelled:', {
|
||||
subscriptionId: subscription.id,
|
||||
customerEmail: attributes.user_email,
|
||||
endsAt: attributes.ends_at,
|
||||
});
|
||||
|
||||
// Handle subscription cancellation
|
||||
// - Schedule access removal for end date
|
||||
// - Send cancellation confirmation
|
||||
}
|
||||
@@ -18,7 +18,6 @@ export function LanguageToggle() {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Define the available locales
|
||||
const locales: Locale[] = ['en', 'de', 'ru', 'ka', 'ar'];
|
||||
|
||||
const languageOptions: LanguageOption[] = locales.map((code) => ({
|
||||
@@ -28,7 +27,6 @@ export function LanguageToggle() {
|
||||
|
||||
const currentLanguage = languageOptions.find(lang => lang.code === locale) || languageOptions[0];
|
||||
|
||||
// Handle clicking outside to close dropdown
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
|
||||
@@ -45,7 +43,6 @@ export function LanguageToggle() {
|
||||
};
|
||||
}, [isOpen]);
|
||||
|
||||
// Handle escape key to close dropdown
|
||||
useEffect(() => {
|
||||
const handleEscape = (event: KeyboardEvent) => {
|
||||
if (event.key === 'Escape') {
|
||||
@@ -68,7 +65,6 @@ export function LanguageToggle() {
|
||||
|
||||
return (
|
||||
<div className="relative" ref={dropdownRef}>
|
||||
{/* Main toggle button */}
|
||||
<button
|
||||
onClick={toggleDropdown}
|
||||
className="flex items-center gap-2 p-2 rounded-lg bg-gray-200 dark:bg-gray-800 hover:bg-gray-300 dark:hover:bg-gray-700 transition-colors"
|
||||
@@ -87,7 +83,6 @@ export function LanguageToggle() {
|
||||
/>
|
||||
</button>
|
||||
|
||||
{/* Dropdown menu */}
|
||||
{isOpen && (
|
||||
<div className="absolute left-0 mt-2 w-36 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 z-50">
|
||||
<div className="py-1">
|
||||
@@ -118,4 +113,4 @@ export function LanguageToggle() {
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
143
app/components/PremiumCheckout.tsx
Normal file
143
app/components/PremiumCheckout.tsx
Normal file
@@ -0,0 +1,143 @@
|
||||
'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<string | null>(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 (
|
||||
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 p-4">
|
||||
<div className="bg-white dark:bg-gray-800 rounded-lg shadow-xl max-w-md w-full p-6">
|
||||
<div className="flex justify-between items-center mb-4">
|
||||
<h2 className="text-xl font-bold text-gray-900 dark:text-white">
|
||||
Premium Skin
|
||||
</h2>
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
|
||||
disabled={isLoading}
|
||||
>
|
||||
<svg className="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M6 18L18 6M6 6l12 12" />
|
||||
</svg>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="text-center mb-6">
|
||||
<div className="w-24 h-24 mx-auto mb-4 bg-gray-100 dark:bg-gray-700 rounded-lg flex items-center justify-center">
|
||||
<img
|
||||
src={skin.normal}
|
||||
alt={skinName}
|
||||
className="w-16 h-16"
|
||||
/>
|
||||
</div>
|
||||
<h3 className="text-lg font-semibold text-gray-900 dark:text-white mb-2">
|
||||
{skinName}
|
||||
</h3>
|
||||
<p className="text-gray-600 dark:text-gray-300 mb-4">
|
||||
Unlock this premium skin to customize your experience!
|
||||
</p>
|
||||
<div className="text-2xl font-bold text-green-600 dark:text-green-400">
|
||||
{pricesLoading ? '...' : (price ?? '')}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<div className="mb-4 p-3 bg-red-100 dark:bg-red-900 border border-red-300 dark:border-red-700 rounded-md">
|
||||
<p className="text-red-700 dark:text-red-300 text-sm">{error}</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex gap-3">
|
||||
<button
|
||||
onClick={onClose}
|
||||
className="flex-1 px-4 py-2 text-gray-700 dark:text-gray-300 bg-gray-200 dark:bg-gray-700 rounded-lg hover:bg-gray-300 dark:hover:bg-gray-600 transition-colors"
|
||||
disabled={isLoading}
|
||||
>
|
||||
Cancel
|
||||
</button>
|
||||
<button
|
||||
onClick={handlePurchase}
|
||||
disabled={isLoading}
|
||||
className="flex-1 px-4 py-2 bg-green-600 hover:bg-green-700 text-white rounded-lg transition-colors disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center"
|
||||
>
|
||||
{isLoading ? (
|
||||
<>
|
||||
<svg className="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
|
||||
</svg>
|
||||
Processing...
|
||||
</>
|
||||
) : (
|
||||
'Purchase'
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 text-center mt-4">
|
||||
Secure payment powered by Lemon Squeezy
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -6,7 +6,10 @@ import Image from 'next/image';
|
||||
import { appConfig } from '../config/app';
|
||||
import { SkinId } from '../types';
|
||||
import { useLocalizedSkinName } from '../hooks/useLocalizedSkinName';
|
||||
import { ChevronDownIcon } from '@heroicons/react/24/outline';
|
||||
import { usePrices } from '../hooks/usePrices';
|
||||
import { useFeature } from '../providers/FeatureProvider';
|
||||
import { ChevronDownIcon, LockClosedIcon } from '@heroicons/react/24/outline';
|
||||
import { PremiumCheckout } from './PremiumCheckout';
|
||||
|
||||
interface SkinOption {
|
||||
id: SkinId;
|
||||
@@ -18,14 +21,20 @@ export function SkinSelector() {
|
||||
const router = useRouter();
|
||||
const searchParams = useSearchParams();
|
||||
const getLocalizedSkinName = useLocalizedSkinName();
|
||||
const paymentsEnabled = useFeature('paymentsEnabled');
|
||||
const { getPrice, loading: pricesLoading } = usePrices();
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [showCheckout, setShowCheckout] = useState<SkinId | null>(null);
|
||||
const dropdownRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
const skinOptions: SkinOption[] = Object.entries(appConfig.skins).map(([id, skin]) => ({
|
||||
id: id as SkinId,
|
||||
name: getLocalizedSkinName(id),
|
||||
image: skin.normal
|
||||
}));
|
||||
// When payments are disabled, filter out premium skins entirely
|
||||
const skinOptions: SkinOption[] = Object.entries(appConfig.skins)
|
||||
.filter(([, skin]) => paymentsEnabled || !skin.isPremium)
|
||||
.map(([id, skin]) => ({
|
||||
id: id as SkinId,
|
||||
name: getLocalizedSkinName(id),
|
||||
image: skin.normal
|
||||
}));
|
||||
|
||||
const skinParam = searchParams.get('skin');
|
||||
|
||||
@@ -37,6 +46,16 @@ export function SkinSelector() {
|
||||
const currentSkinOption = skinOptions.find(skin => skin.id === currentSkin) || skinOptions[0];
|
||||
|
||||
const handleSkinChange = useCallback((newSkin: SkinId) => {
|
||||
const skin = appConfig.skins[newSkin];
|
||||
|
||||
// If it's a premium skin, show checkout modal
|
||||
if (skin.isPremium) {
|
||||
setShowCheckout(newSkin);
|
||||
setIsOpen(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// For free skins, change immediately
|
||||
const params = new URLSearchParams(searchParams.toString());
|
||||
|
||||
if (newSkin === appConfig.defaultSkin) {
|
||||
@@ -50,6 +69,10 @@ export function SkinSelector() {
|
||||
setIsOpen(false);
|
||||
}, [router, searchParams]);
|
||||
|
||||
const handleCheckoutClose = useCallback(() => {
|
||||
setShowCheckout(null);
|
||||
}, []);
|
||||
|
||||
// Handle clicking outside to close dropdown
|
||||
useEffect(() => {
|
||||
const handleClickOutside = (event: MouseEvent) => {
|
||||
@@ -119,33 +142,56 @@ export function SkinSelector() {
|
||||
{isOpen && (
|
||||
<div className="absolute left-0 mt-2 w-36 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 z-50">
|
||||
<div className="py-1">
|
||||
{skinOptions.map((option) => (
|
||||
<button
|
||||
key={option.id}
|
||||
onClick={() => handleSkinChange(option.id)}
|
||||
className={`w-full flex items-center gap-3 px-3 py-2 text-sm text-left hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors ${
|
||||
currentSkin === option.id
|
||||
? 'bg-gray-100 dark:bg-gray-700 text-blue-600 dark:text-blue-400'
|
||||
: 'text-gray-700 dark:text-gray-300'
|
||||
}`}
|
||||
role="menuitem"
|
||||
>
|
||||
<Image
|
||||
src={option.image}
|
||||
alt={option.name}
|
||||
width={16}
|
||||
height={16}
|
||||
className="rounded"
|
||||
/>
|
||||
<span>{option.name}</span>
|
||||
{currentSkin === option.id && (
|
||||
<div className="ml-auto w-2 h-2 bg-blue-600 dark:bg-blue-400 rounded-full"></div>
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
{skinOptions.map((option) => {
|
||||
const skin = appConfig.skins[option.id];
|
||||
const isPremium = skin.isPremium;
|
||||
|
||||
return (
|
||||
<button
|
||||
key={option.id}
|
||||
onClick={() => handleSkinChange(option.id)}
|
||||
className={`w-full flex items-center gap-3 px-3 py-2 text-sm text-left hover:bg-gray-100 dark:hover:bg-gray-700 transition-colors ${
|
||||
currentSkin === option.id
|
||||
? 'bg-gray-100 dark:bg-gray-700 text-blue-600 dark:text-blue-400'
|
||||
: 'text-gray-700 dark:text-gray-300'
|
||||
}`}
|
||||
role="menuitem"
|
||||
>
|
||||
<div className="relative">
|
||||
<Image
|
||||
src={option.image}
|
||||
alt={option.name}
|
||||
width={16}
|
||||
height={16}
|
||||
className="rounded"
|
||||
/>
|
||||
{isPremium && (
|
||||
<LockClosedIcon className="absolute -top-1 -right-1 w-3 h-3 text-yellow-500" />
|
||||
)}
|
||||
</div>
|
||||
<span className="flex-1">{option.name}</span>
|
||||
{isPremium && paymentsEnabled && (
|
||||
<span className="text-xs text-yellow-600 dark:text-yellow-400 font-medium">
|
||||
{pricesLoading ? '...' : (getPrice(option.id) ?? '')}
|
||||
</span>
|
||||
)}
|
||||
{currentSkin === option.id && (
|
||||
<div className="w-2 h-2 bg-blue-600 dark:bg-blue-400 rounded-full"></div>
|
||||
)}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Premium Checkout Modal */}
|
||||
{showCheckout && (
|
||||
<PremiumCheckout
|
||||
skinId={showCheckout}
|
||||
onClose={handleCheckoutClose}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2,9 +2,8 @@ import { useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { useMessages } from 'next-intl';
|
||||
import { getRandomEmoji } from '../config/emojis';
|
||||
|
||||
// Increase visibility duration for speech bubbles
|
||||
const VISIBILITY_MS = 3000; // 3 seconds for message visibility
|
||||
const COOLDOWN_MS = 2000; // 2 seconds between new messages
|
||||
const VISIBILITY_MS = 3000;
|
||||
const COOLDOWN_MS = 2000;
|
||||
|
||||
interface SpeechBubbleProps {
|
||||
isShaken: boolean;
|
||||
@@ -21,17 +20,13 @@ export function SpeechBubble({ triggerCount }: SpeechBubbleProps) {
|
||||
const showTimeRef = useRef<number>(0);
|
||||
const lastFadeTime = useRef(0);
|
||||
|
||||
// Load messages when component mounts or language changes
|
||||
useEffect(() => {
|
||||
// Only run if we haven't loaded messages yet
|
||||
if (messagesRef.current.length > 0) return;
|
||||
|
||||
// Get the character messages from the messages object
|
||||
try {
|
||||
const characterMessages = allMessages.character;
|
||||
|
||||
if (characterMessages && typeof characterMessages === 'object') {
|
||||
// Convert object values to array
|
||||
const messageArray = Object.values(characterMessages) as string[];
|
||||
|
||||
if (messageArray.length === 0) {
|
||||
@@ -47,24 +42,22 @@ export function SpeechBubble({ triggerCount }: SpeechBubbleProps) {
|
||||
} catch (error) {
|
||||
console.error(`Error loading character messages:`, error);
|
||||
}
|
||||
}, [allMessages]); // Depend on allMessages to reload when they change
|
||||
}, [allMessages]);
|
||||
|
||||
const getRandomMessage = useCallback(() => {
|
||||
const currentMessages = messagesRef.current;
|
||||
if (currentMessages.length === 0) return '';
|
||||
const randomIndex = Math.floor(Math.random() * currentMessages.length);
|
||||
const message = currentMessages[randomIndex];
|
||||
return `${message} ${getRandomEmoji()}`;
|
||||
}, []); // No dependencies needed since we use ref
|
||||
const messageValue = currentMessages[randomIndex];
|
||||
return `${messageValue} ${getRandomEmoji()}`;
|
||||
}, []);
|
||||
|
||||
// Handle new trigger events
|
||||
useEffect(() => {
|
||||
if (triggerCount === 0 || messagesRef.current.length === 0) return;
|
||||
|
||||
const now = Date.now();
|
||||
const timeSinceLastFade = now - lastFadeTime.current;
|
||||
|
||||
// If we're in cooldown, or a message is visible, queue the new message
|
||||
if (timeSinceLastFade < COOLDOWN_MS || isVisible) {
|
||||
const newMessage = getRandomMessage();
|
||||
if (newMessage) {
|
||||
@@ -73,7 +66,6 @@ export function SpeechBubble({ triggerCount }: SpeechBubbleProps) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Otherwise, show the message immediately
|
||||
lastTriggerTime.current = now;
|
||||
showTimeRef.current = now;
|
||||
const newMessage = getRandomMessage();
|
||||
@@ -83,17 +75,15 @@ export function SpeechBubble({ triggerCount }: SpeechBubbleProps) {
|
||||
}
|
||||
}, [triggerCount, isVisible, getRandomMessage]);
|
||||
|
||||
// Handle message queue
|
||||
useEffect(() => {
|
||||
if (messageQueue.length === 0 || isVisible) return;
|
||||
|
||||
const now = Date.now();
|
||||
const timeSinceLastFade = now - lastFadeTime.current;
|
||||
|
||||
// Only show next message if cooldown has expired
|
||||
if (timeSinceLastFade >= COOLDOWN_MS) {
|
||||
const nextMessage = messageQueue[0];
|
||||
setMessageQueue(prev => prev.slice(1)); // Remove the message from queue
|
||||
setMessageQueue(prev => prev.slice(1));
|
||||
lastTriggerTime.current = now;
|
||||
showTimeRef.current = now;
|
||||
setMessage(nextMessage);
|
||||
@@ -101,7 +91,6 @@ export function SpeechBubble({ triggerCount }: SpeechBubbleProps) {
|
||||
}
|
||||
}, [messageQueue, isVisible]);
|
||||
|
||||
// Handle visibility duration
|
||||
useEffect(() => {
|
||||
if (!isVisible) return;
|
||||
|
||||
|
||||
@@ -16,20 +16,25 @@ export const appConfig = {
|
||||
id: 'frog',
|
||||
name: 'Frog',
|
||||
normal: '/images/frog.svg',
|
||||
shaken: '/images/frog-shaken.svg'
|
||||
shaken: '/images/frog-shaken.svg',
|
||||
isPremium: false
|
||||
},
|
||||
mandarin: {
|
||||
id: 'mandarin',
|
||||
name: 'Mandarin',
|
||||
normal: '/images/mandarin.svg',
|
||||
// TODO: Create a proper shaken version of the mandarin skin
|
||||
shaken: '/images/mandarin.svg' // Using the same image for both states until a shaken version is created
|
||||
shaken: '/images/mandarin.svg', // Using the same image for both states until a shaken version is created
|
||||
isPremium: false,
|
||||
variantId: 'your_mandarin_variant_id_here' // Replace with actual variant ID when created
|
||||
},
|
||||
beaver: {
|
||||
id: 'beaver',
|
||||
name: 'Beaver',
|
||||
normal: '/images/beaver.svg',
|
||||
shaken: '/images/beaver-shaken.svg'
|
||||
shaken: '/images/beaver-shaken.svg',
|
||||
isPremium: true,
|
||||
variantId: '1047017'
|
||||
}
|
||||
},
|
||||
defaultSkin: 'frog'
|
||||
|
||||
20
app/config/features.ts
Normal file
20
app/config/features.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Server-side feature flag definitions.
|
||||
*
|
||||
* Flags are read from environment variables. The abstraction is kept thin
|
||||
* so a runtime provider (Flipt, Unleash, Flags SDK adapter, etc.) can be
|
||||
* swapped in later without changing any consumer code.
|
||||
*
|
||||
* Convention: FEATURE_<NAME>=1 → enabled
|
||||
* anything else → disabled
|
||||
*/
|
||||
|
||||
export interface FeatureFlags {
|
||||
paymentsEnabled: boolean;
|
||||
}
|
||||
|
||||
export function getFeatureFlags(): FeatureFlags {
|
||||
return {
|
||||
paymentsEnabled: process.env.FEATURE_PAYMENTS === '1',
|
||||
};
|
||||
}
|
||||
45
app/config/lemonsqueezy.ts
Normal file
45
app/config/lemonsqueezy.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { lemonSqueezySetup } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
|
||||
// Initialize Lemon Squeezy SDK
|
||||
export function initializeLemonSqueezy() {
|
||||
const apiKey = process.env.LEMONSQUEEZY_API_KEY;
|
||||
|
||||
if (!apiKey) {
|
||||
throw new Error('LEMONSQUEEZY_API_KEY is required');
|
||||
}
|
||||
|
||||
lemonSqueezySetup({
|
||||
apiKey,
|
||||
onError: (error) => {
|
||||
throw error; // Fail fast instead of just logging
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Lemon Squeezy configuration with lazy validation.
|
||||
// Config is only resolved on first access so the module can be safely
|
||||
// imported even when payment env vars are absent (e.g. payments disabled).
|
||||
let _config: { storeId: string; webhookSecret: string; baseUrl: string } | null = null;
|
||||
|
||||
export function getLemonSqueezyConfig() {
|
||||
if (_config) return _config;
|
||||
|
||||
const storeId = process.env.LEMONSQUEEZY_STORE_ID;
|
||||
const webhookSecret = process.env.LEMONSQUEEZY_WEBHOOK_SECRET;
|
||||
const baseUrl = process.env.NEXT_PUBLIC_APP_URL;
|
||||
|
||||
if (!storeId) {
|
||||
throw new Error('LEMONSQUEEZY_STORE_ID is required');
|
||||
}
|
||||
|
||||
if (!webhookSecret) {
|
||||
throw new Error('LEMONSQUEEZY_WEBHOOK_SECRET is required');
|
||||
}
|
||||
|
||||
if (!baseUrl) {
|
||||
throw new Error('NEXT_PUBLIC_APP_URL is required');
|
||||
}
|
||||
|
||||
_config = { storeId, webhookSecret, baseUrl };
|
||||
return _config;
|
||||
}
|
||||
60
app/hooks/usePrices.ts
Normal file
60
app/hooks/usePrices.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useFeature } from '../providers/FeatureProvider';
|
||||
|
||||
interface PricesData {
|
||||
prices: Record<string, string>;
|
||||
}
|
||||
|
||||
export function usePrices() {
|
||||
const paymentsEnabled = useFeature('paymentsEnabled');
|
||||
const [prices, setPrices] = useState<Record<string, string>>({});
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (!paymentsEnabled) {
|
||||
setLoading(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchPrices = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
const response = await fetch('/api/prices');
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch prices');
|
||||
}
|
||||
|
||||
const data: PricesData = await response.json();
|
||||
setPrices(data.prices);
|
||||
} catch (err) {
|
||||
console.error('Error fetching prices:', err);
|
||||
setError(err instanceof Error ? err.message : 'Failed to fetch prices');
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchPrices();
|
||||
}, [paymentsEnabled]);
|
||||
|
||||
const getPrice = (skinId: string): string | null => {
|
||||
if (!paymentsEnabled || loading) {
|
||||
return null;
|
||||
}
|
||||
return prices[skinId] ?? null;
|
||||
};
|
||||
|
||||
return {
|
||||
prices,
|
||||
loading,
|
||||
error,
|
||||
enabled: paymentsEnabled,
|
||||
getPrice
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
import type { Metadata } from 'next'
|
||||
import { Inter } from 'next/font/google'
|
||||
import { ThemeProvider } from './providers/ThemeProvider'
|
||||
import { FeatureProvider } from './providers/FeatureProvider'
|
||||
import { getFeatureFlags } from './config/features'
|
||||
import { appConfig } from './config/app'
|
||||
import { Suspense } from 'react'
|
||||
import './globals.css'
|
||||
@@ -41,18 +43,22 @@ export default function RootLayout({
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const features = getFeatureFlags();
|
||||
|
||||
return (
|
||||
<html suppressHydrationWarning>
|
||||
<body className={`${inter.className} transition-colors`}>
|
||||
<ThemeProvider>
|
||||
<Suspense fallback={
|
||||
<div className="flex h-[100dvh] items-center justify-center bg-green-50 dark:bg-slate-900">
|
||||
<p className="text-gray-600 dark:text-gray-400">Loading...</p>
|
||||
</div>
|
||||
}>
|
||||
{children}
|
||||
</Suspense>
|
||||
</ThemeProvider>
|
||||
<FeatureProvider features={features}>
|
||||
<ThemeProvider>
|
||||
<Suspense fallback={
|
||||
<div className="flex h-[100dvh] items-center justify-center bg-green-50 dark:bg-slate-900">
|
||||
<p className="text-gray-600 dark:text-gray-400">Loading...</p>
|
||||
</div>
|
||||
}>
|
||||
{children}
|
||||
</Suspense>
|
||||
</ThemeProvider>
|
||||
</FeatureProvider>
|
||||
</body>
|
||||
</html>
|
||||
)
|
||||
|
||||
27
app/providers/FeatureProvider.tsx
Normal file
27
app/providers/FeatureProvider.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
'use client';
|
||||
|
||||
import { createContext, useContext } from 'react';
|
||||
import type { FeatureFlags } from '../config/features';
|
||||
|
||||
const FeatureContext = createContext<FeatureFlags | undefined>(undefined);
|
||||
|
||||
interface FeatureProviderProps {
|
||||
features: FeatureFlags;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export function FeatureProvider({ features, children }: FeatureProviderProps) {
|
||||
return (
|
||||
<FeatureContext.Provider value={features}>
|
||||
{children}
|
||||
</FeatureContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useFeature<K extends keyof FeatureFlags>(key: K): FeatureFlags[K] {
|
||||
const context = useContext(FeatureContext);
|
||||
if (context === undefined) {
|
||||
throw new Error('useFeature must be used within a FeatureProvider');
|
||||
}
|
||||
return context[key];
|
||||
}
|
||||
Reference in New Issue
Block a user