'use client'; import { useState } from 'react'; import { AnimatePresence, motion } from 'framer-motion'; import { PriceIntervals } from '@documenso/ee/server-only/stripe/get-prices-by-interval'; import { useIsMounted } from '@documenso/lib/client-only/hooks/use-is-mounted'; import { toHumanPrice } from '@documenso/lib/universal/stripe/to-human-price'; import { Button } from '@documenso/ui/primitives/button'; import { Card, CardContent, CardTitle } from '@documenso/ui/primitives/card'; import { Tabs, TabsList, TabsTrigger } from '@documenso/ui/primitives/tabs'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { createCheckout } from './create-checkout.action'; type Interval = keyof PriceIntervals; const INTERVALS: Interval[] = ['day', 'week', 'month', 'year']; // eslint-disable-next-line @typescript-eslint/consistent-type-assertions const isInterval = (value: unknown): value is Interval => INTERVALS.includes(value as Interval); const FRIENDLY_INTERVALS: Record = { day: 'Daily', week: 'Weekly', month: 'Monthly', year: 'Yearly', }; const MotionCard = motion(Card); export type BillingPlansProps = { prices: PriceIntervals; }; export const BillingPlans = ({ prices }: BillingPlansProps) => { const { toast } = useToast(); const isMounted = useIsMounted(); const [interval, setInterval] = useState('month'); const [isFetchingCheckoutSession, setIsFetchingCheckoutSession] = useState(false); const onSubscribeClick = async (priceId: string) => { try { setIsFetchingCheckoutSession(true); const url = await createCheckout({ priceId }); if (!url) { throw new Error('Unable to create session'); } window.open(url); } catch (_err) { toast({ title: 'Something went wrong', description: 'An error occurred while trying to create a checkout session.', variant: 'destructive', }); } finally { setIsFetchingCheckoutSession(false); } }; return (
isInterval(value) && setInterval(value)}> {INTERVALS.map( (interval) => prices[interval].length > 0 && ( {FRIENDLY_INTERVALS[interval]} ), )}
{prices[interval].map((price) => ( {price.product.name}
${toHumanPrice(price.unit_amount ?? 0)} {price.currency.toUpperCase()}{' '} per {interval}
{price.product.description}
{price.product.features && price.product.features.length > 0 && (
Includes:
    {price.product.features.map((feature, index) => (
  • {feature.name}
  • ))}
)}
))}
); };