"use client";
import { useState } from "react";
import Link from "next/link";
import { ArrowRight, CheckCircle } from "lucide-react";
import { useTranslations } from "next-intl";
import * as 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.output | undefined;
redirectToCheckout?: boolean;
alwaysDisplayMonthlyPrice?: boolean;
plan: {
id: string;
lineItems: z.output[];
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 = useTranslations();
const highlighted = props.product.highlighted ?? false;
const lineItem = props.primaryLineItem!;
const isCustom = props.plan.custom ?? false;
const i18nKey = lineItem?.unit ? `billing.units.${lineItem.unit}` : "";
const unitLabel = lineItem?.unit
? t.has(i18nKey)
? t(i18nKey, {
count: 1,
defaultValue: lineItem.unit,
} as never)
: 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 transition-all focus:!ring-0",
{
"border-r-transparent": index === 0,
["hover:text-primary text-muted-foreground"]: !selected,
["cursor-default"]: 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 = useTranslations();
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 (
);
}