'use client'; import { Badge } from '@/components/ui/badge'; import { Card, CardContent, CardDescription, CardHeader, CardTitle, } from '@/components/ui/card'; import { getCreditPackages } from '@/config/credits-config'; import { websiteConfig } from '@/config/website'; import { useCurrentUser } from '@/hooks/use-current-user'; import { useCurrentPlan } from '@/hooks/use-payment'; import { formatPrice } from '@/lib/formatter'; import { cn } from '@/lib/utils'; import { CircleCheckBigIcon, CoinsIcon } from 'lucide-react'; import { useTranslations } from 'next-intl'; import { CreditCheckoutButton } from './credit-checkout-button'; /** * Credit packages component */ export function CreditPackages() { // Check if credits are enabled - move this check before any hooks if (!websiteConfig.credits.enableCredits) { return null; } const t = useTranslations('Dashboard.settings.credits.packages'); // Get current user and payment info const currentUser = useCurrentUser(); const { data: paymentData, isLoading: isLoadingPayment } = useCurrentPlan( currentUser?.id ); const currentPlan = paymentData?.currentPlan; // Get credit packages with translations - must be called here to maintain hook order // This function contains useTranslations hook, so it must be called before any conditional returns const creditPackages = Object.values(getCreditPackages()).filter( (pkg) => !pkg.disabled && pkg.price.priceId ); // Don't render anything while loading to prevent flash if (isLoadingPayment) { return null; } // Don't render anything if we don't have payment data yet if (!paymentData) { return null; } // Check if user is on free plan and enablePackagesForFreePlan is false const isFreePlan = currentPlan?.isFree === true; // Check if user is on free plan and enablePackagesForFreePlan is false if (isFreePlan && !websiteConfig.credits.enablePackagesForFreePlan) { return null; } return ( {t('title')} {t('description')}
{creditPackages.map((creditPackage) => ( {creditPackage.popular && (
{t('popular')}
)} {/* Price and Credits - Left/Right Layout */}
{creditPackage.amount.toLocaleString()}
{formatPrice( creditPackage.price.amount, creditPackage.price.currency )}
{creditPackage.description}
{/* purchase button using checkout */} {t('purchase')}
))}
); }