'use client'; import { ToggleGroup, ToggleGroupItem } from '@/components/ui/toggle-group'; import { usePricePlans } from '@/config/price-config'; import { cn } from '@/lib/utils'; import { PaymentTypes, type PlanInterval, PlanIntervals, type PricePlan, } from '@/payment/types'; import { useTranslations } from 'next-intl'; import { useState } from 'react'; import { PricingCard } from './pricing-card'; interface PricingTableProps { metadata?: Record; currentPlan?: PricePlan | null; className?: string; } /** * Pricing Table Component * * 1. Displays all pricing plans with interval selection tabs for subscription plans, * free plans and one-time purchase plans are always displayed * 2. If a plan is disabled, it will not be displayed in the pricing table * 3. If a price is disabled, it will not be displayed in the pricing table */ export function PricingTable({ metadata, currentPlan, className, }: PricingTableProps) { const t = useTranslations('PricingPage'); const [interval, setInterval] = useState(PlanIntervals.MONTH); // Get price plans with translations const pricePlans = usePricePlans(); const plans = Object.values(pricePlans); // Current plan ID for comparison const currentPlanId = currentPlan?.id || null; // Filter plans into free, subscription and one-time plans const freePlans = plans.filter((plan) => plan.isFree && !plan.disabled); const subscriptionPlans = plans.filter( (plan) => !plan.isFree && !plan.disabled && plan.prices.some( (price) => !price.disabled && price.type === PaymentTypes.SUBSCRIPTION ) ); const oneTimePlans = plans.filter( (plan) => !plan.isFree && !plan.disabled && plan.prices.some( (price) => !price.disabled && price.type === PaymentTypes.ONE_TIME ) ); // Check if any plan has a monthly price option const hasMonthlyOption = subscriptionPlans.some((plan) => plan.prices.some( (price) => price.type === PaymentTypes.SUBSCRIPTION && price.interval === PlanIntervals.MONTH ) ); // Check if any plan has a yearly price option const hasYearlyOption = subscriptionPlans.some((plan) => plan.prices.some( (price) => price.type === PaymentTypes.SUBSCRIPTION && price.interval === PlanIntervals.YEAR ) ); const handleIntervalChange = (value: string) => { setInterval(value as PlanInterval); }; return (
{/* Show interval toggle if there are subscription plans */} {(hasMonthlyOption || hasYearlyOption) && subscriptionPlans.length > 0 && (
value && handleIntervalChange(value)} className="border rounded-lg p-1" > {hasMonthlyOption && ( {t('monthly')} )} {hasYearlyOption && ( {t('yearly')} )}
)} {/* Calculate total number of visible plans */} {(() => { const totalVisiblePlans = freePlans.length + subscriptionPlans.length + oneTimePlans.length; return (
= 3 && 'grid-cols-1 md:grid-cols-2 lg:grid-cols-3' )} > {/* Render free plans (always visible) */} {freePlans.map((plan) => ( ))} {/* Render subscription plans with the selected interval */} {subscriptionPlans.map((plan) => ( ))} {/* Render one-time plans (always visible) */} {oneTimePlans.map((plan) => ( ))}
); })()}
); }