'use client' import { useState, useEffect, SetStateAction, Dispatch } from 'react' import Link from 'next/link' import PlanCards from '@/components/Plan' import { UpdateBillingLink, CancelLink, ResumeButton, PauseLink, UnpauseButton } from '@/components/Manage' import { Plan, Subscription, SubscriptionState } from '@/types' export const SubscriptionComponent = ({ sub, plans }: { sub: Subscription | null, plans: Plan[] } ) => { // Make sure Lemon.js is loaded useEffect(() => { window.createLemonSqueezy() }, []) const [subscription, setSubscription] = useState(() => { if (sub) { return { id: sub.lemonSqueezyId, planName: sub.plan?.variantName, planInterval: sub.plan?.interval, productId: sub.plan?.productId, variantId: sub.plan?.variantId, status: sub.status, renewalDate: sub.renewsAt, trialEndDate: sub.trialEndsAt, expiryDate: sub.endsAt, unpauseDate: sub.resumesAt, price: sub.price / 100, } } else { return undefined } }) if (sub) { switch(subscription?.status) { case 'active': return case 'on_trial': return case 'past_due': return case 'cancelled': return case 'paused': return case 'unpaid': return case 'expired': return } } return ( <>

Please sign up to a paid plan.

) } export const ActiveSubscription = ({ subscription, setSubscription }: { subscription: SubscriptionState, setSubscription: Dispatch> } ) => { return ( <>

You are currently on the {subscription?.planName} {subscription?.planInterval}ly plan, paying ${subscription?.price}/{subscription?.planInterval}.

Your next renewal will be on {formatDate(subscription?.renewalDate)}.


Change plan →

) } export const CancelledSubscription = ({ subscription, setSubscription }: { subscription: SubscriptionState, setSubscription: Dispatch> } ) => { return ( <>

You are currently on the {subscription?.planName} {subscription?.planInterval}ly plan, paying ${subscription?.price}/{subscription?.planInterval}.

Your subscription has been cancelled and will end on {formatDate(subscription?.expiryDate)}. After this date you will no longer have access to the app.

) } export const PausedSubscription = ({ subscription, setSubscription }: { subscription: SubscriptionState, setSubscription: Dispatch> } ) => { return ( <>

You are currently on the {subscription?.planName} {subscription?.planInterval}ly plan, paying ${subscription?.price}/{subscription?.planInterval}.

{subscription?.unpauseDate ? (

Your subscription payments are currently paused. Your subscription will automatically resume on {formatDate(subscription?.unpauseDate)}.

) : (

Your subscription payments are currently paused.

)}

) } export const TrialSubscription = ({ subscription, setSubscription }: { subscription: SubscriptionState, setSubscription: Dispatch> } ) => { return ( <>

You are currently on a free trial of the {subscription?.planName} {subscription?.planInterval}ly plan, paying ${subscription?.price}/{subscription?.planInterval}.

Your trial ends on {formatDate(subscription?.trialEndDate)}. You can cancel your subscription before this date and you won't be charged.


Change plan →

) } export const PastDueSubscription = ({ subscription, setSubscription }: { subscription: SubscriptionState, setSubscription: Dispatch> } ) => { return ( <>
Your latest payment failed. We will re-try this payment up to four times, after which your subscription will be cancelled.
If you need to update your billing details, you can do so below.

You are currently on the {subscription?.planName} {subscription?.planInterval}ly plan, paying ${subscription?.price}/{subscription?.planInterval}.

We will attempt a payment on {formatDate(subscription?.renewalDate)}.


) } export const UnpaidSubscription = ({ subscription, setSubscription }: { subscription: SubscriptionState, setSubscription: Dispatch> } ) => { /* Unpaid subscriptions have had four failed recovery payments. If you have dunning enabled in your store settings, customers will be sent emails trying to reactivate their subscription. If you don't have dunning enabled the subscription will remain "unpaid". */ return ( <>

We haven't been able to make a successful payment and your subscription is currently marked as unpaid.

Please update your billing information to regain access.


) } export const ExpiredSubscription = ({ subscription, plans, setSubscription }: { subscription: SubscriptionState, plans: Plan[], setSubscription: Dispatch> } ) => { return ( <>

Your subscription expired on {formatDate(subscription?.expiryDate)}.

Please create a new subscription to regain access.


) } function formatDate(date?: Date | null) { if (!date) return '' return new Date(date).toLocaleString('en-US', { month: 'short', day: "2-digit", year: 'numeric' }) }