'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,
LineItemSchema,
getPlanIntervals,
getPrimaryLineItem,
} from '@kit/billing';
import { formatCurrency } from '@kit/shared/utils';
import { Badge } from '@kit/ui/badge';
import { Button } from '@kit/ui/button';
import { If } from '@kit/ui/if';
import { Separator } from '@kit/ui/separator';
import { Trans } from '@kit/ui/trans';
import { cn } from '@kit/ui/utils';
import { LineItemDetails } from './line-item-details';
interface Paths {
signUp: string;
return: string;
}
export function PricingTable({
config,
paths,
CheckoutButtonRenderer,
redirectToCheckout = true,
displayPlanDetails = true,
}: {
config: BillingConfig;
paths: Paths;
displayPlanDetails?: boolean;
redirectToCheckout?: boolean;
CheckoutButtonRenderer?: React.ComponentType<{
planId: string;
productId: string;
highlighted?: boolean;
}>;
}) {
const intervals = getPlanIntervals(config).filter(Boolean) as string[];
const [interval, setInterval] = useState(intervals[0]!);
return (
{intervals.length > 1 ? (
) : null}
{config.products.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;
plan: {
id: string;
lineItems: z.infer[];
interval?: string;
name?: string;
href?: string;
label?: string;
};
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 highlighted = props.product.highlighted ?? false;
const lineItem = props.primaryLineItem;
// 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';
});
return (
{lineItem ? (
formatCurrency(props.product.currency, lineItem.cost)
) : props.plan.label ? (
) : (
)}
}
>
{(interval) => (
)}
/
}
>
{(CheckoutButton) => (
)}
);
}
function FeaturesList(
props: React.PropsWithChildren<{
features: string[];
highlighted?: boolean;
}>,
) {
return (
{props.features.map((feature) => {
return (
);
})}
);
}
function Price({ children }: React.PropsWithChildren) {
return (
{children}
);
}
function ListItem({ children }: React.PropsWithChildren) {
return (
{children}
);
}
function PlanIntervalSwitcher(
props: React.PropsWithChildren<{
intervals: string[];
interval: string;
setInterval: (interval: string) => void;
}>,
) {
return (
{props.intervals.map((plan, index) => {
const selected = plan === props.interval;
const className = cn(
'focus:!ring-0 !outline-none animate-in transition-all fade-in',
{
'rounded-r-none border-r-transparent': index === 0,
'rounded-l-none': index === props.intervals.length - 1,
['hover:text-primary border text-muted-foreground']: !selected,
['font-semibold cursor-default hover:text-initial hover:bg-background']:
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 (
);
}