'use client';
import { useState } from 'react';
import Link from 'next/link';
import { ArrowRight, CheckCircle } from 'lucide-react';
import { useTranslation } from 'react-i18next';
import { z } from 'zod';
import {
BillingConfig,
type LineItemSchema,
getPlanIntervals,
getPrimaryLineItem,
} from '@kit/billing';
import { Badge } from '@kit/ui/badge';
import { Button } from '@kit/ui/button';
import { If } from '@kit/ui/if';
import { Trans } from '@kit/ui/trans';
import { cn } from '@kit/ui/utils';
import { LineItemDetails } from './line-item-details';
import { PlanCostDisplay } from './plan-cost-display';
interface Paths {
signUp: string;
return: string;
}
type Interval = 'month' | 'year';
export function PricingTable({
config,
paths,
CheckoutButtonRenderer,
redirectToCheckout = true,
displayPlanDetails = true,
alwaysDisplayMonthlyPrice = true,
}: {
config: BillingConfig;
paths: Paths;
displayPlanDetails?: boolean;
alwaysDisplayMonthlyPrice?: boolean;
redirectToCheckout?: boolean;
CheckoutButtonRenderer?: React.ComponentType<{
planId: string;
productId: string;
highlighted?: boolean;
}>;
}) {
const intervals = getPlanIntervals(config).filter(Boolean) as Interval[];
const [interval, setInterval] = useState(intervals[0]!);
// Always filter out hidden products
const visibleProducts = config.products.filter((product) => !product.hidden);
return (
{intervals.length > 1 ? (
) : null}
{visibleProducts.map((product) => {
const plan = product.plans.find((plan) => {
if (plan.paymentType === 'recurring') {
return plan.interval === interval;
}
return plan;
});
if (!plan) {
return null;
}
const primaryLineItem = getPrimaryLineItem(config, plan.id);
if (!plan.custom && !primaryLineItem) {
throw new Error(`Primary line item not found for plan ${plan.id}`);
}
return (
);
})}
);
}
function PricingItem(
props: React.PropsWithChildren<{
className?: string;
displayPlanDetails: boolean;
paths: Paths;
selectable: boolean;
primaryLineItem: z.infer | undefined;
redirectToCheckout?: boolean;
alwaysDisplayMonthlyPrice?: boolean;
plan: {
id: string;
lineItems: z.infer[];
interval?: Interval;
name?: string;
href?: string;
label?: string;
custom?: boolean;
};
CheckoutButton?: React.ComponentType<{
planId: string;
productId: string;
highlighted?: boolean;
}>;
product: {
id: string;
name: string;
currency: string;
description: string;
badge?: string;
highlighted?: boolean;
features: string[];
};
}>,
) {
const { t, i18n } = useTranslation();
const highlighted = props.product.highlighted ?? false;
const lineItem = props.primaryLineItem!;
const isCustom = props.plan.custom ?? false;
const i18nKey = `billing:units.${lineItem.unit}`;
const unitLabel = lineItem?.unit
? i18n.exists(i18nKey)
? t(i18nKey, {
count: 1,
defaultValue: lineItem.unit,
})
: lineItem.unit
: '';
const isDefaultSeatUnit = lineItem?.unit === 'member';
// we exclude flat line items from the details since
// it doesn't need further explanation
const lineItemsToDisplay = props.plan.lineItems.filter((item) => {
return item.type !== 'flat';
});
const interval = props.plan.interval as Interval;
return (
}
>
}
>
{(interval) => (
)}
/
}
>
}
>
{(CheckoutButton) => (
)}
);
}
function FeaturesList(
props: React.PropsWithChildren<{
features: string[];
highlighted: boolean;
}>,
) {
return (
{props.features.map((feature) => {
return (
);
})}
);
}
function Price({
children,
isMonthlyPrice = true,
displayBillingPeriod = true,
}: React.PropsWithChildren<{
isMonthlyPrice?: boolean;
displayBillingPeriod?: boolean;
}>) {
return (
{children}
/
);
}
function ListItem({
children,
highlighted,
}: React.PropsWithChildren<{
highlighted: boolean;
}>) {
return (
{children}
);
}
function PlanIntervalSwitcher(
props: React.PropsWithChildren<{
intervals: Interval[];
interval: Interval;
setInterval: (interval: Interval) => void;
}>,
) {
return (
{props.intervals.map((plan, index) => {
const selected = plan === props.interval;
const className = cn(
'animate-in fade-in rounded-full !outline-hidden transition-all focus:!ring-0',
{
'border-r-transparent': index === 0,
['hover:text-primary text-muted-foreground']: !selected,
['cursor-default font-semibold']: selected,
['hover:bg-initial']: !selected,
},
);
return (
);
})}
);
}
function DefaultCheckoutButton(
props: React.PropsWithChildren<{
plan: {
id: string;
name?: string | undefined;
href?: string;
buttonLabel?: string;
};
product: {
name: string;
};
paths: Paths;
redirectToCheckout?: boolean;
highlighted?: boolean;
}>,
) {
const { t } = useTranslation('billing');
const signUpPath = props.paths.signUp;
const searchParams = new URLSearchParams({
next: props.paths.return,
plan: props.plan.id,
redirectToCheckout: props.redirectToCheckout ? 'true' : 'false',
});
const linkHref =
props.plan.href ?? `${signUpPath}?${searchParams.toString()}`;
const label = props.plan.buttonLabel ?? 'common:getStartedWithPlan';
return (
);
}