Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

11.4 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { LemonButton } from '@posthog/lemon-ui'
import { Link } from '@posthog/lemon-ui'

import { BillingUpgradeCTA } from 'lib/components/BillingUpgradeCTA'
import { Dayjs } from 'lib/dayjs'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { YcCreditsState, billingLogic } from 'lib/logic/billing/billingLogic'

import { BillingPlan, BillingProductV2Type, StartupProgramLabel } from '~/types'

import planEnterprise from 'public/plan_enterprise.png'
import planFree from 'public/plan_free.svg'
import planPaid from 'public/plan_paid.svg'
import planStartup from 'public/plan_startup.svg'
import planTeams from 'public/plan_teams.png'
import planYc from 'public/plan_yc.svg'

import { billingProductLogic } from './billingProductLogic'
import { paymentEntryLogic } from './paymentEntryLogic'
import { PlanComparisonModal } from './PlanComparison'

const PLAN_BADGES: Record<BillingPlan, string> = {
    [BillingPlan.Free]: planFree,
    [BillingPlan.Paid]: planPaid,
    [BillingPlan.Teams]: planTeams, // Legacy
    [BillingPlan.Boost]: planTeams, // TODO: Add Boost badge
    [BillingPlan.Scale]: planTeams, // TODO: Add Scale badge
    [BillingPlan.Enterprise]: planEnterprise,
}

const STARTUP_PROGRAM_BADGES: Record<StartupProgramLabel, string> = {
    [StartupProgramLabel.YC]: planYc,
    [StartupProgramLabel.Startup]: planStartup,
}

interface CopyVariation {
    title: string | null
    subtitle: string | null
    getDescription: (
        billingPlan: BillingPlan,
        scrollToProduct: (productType: string) => void,
        ycCreditsSummary: string
    ) => JSX.Element
    backgroundColor: string
}

const getYcCreditsSummary = (ycCreditsState: YcCreditsState | null, expiresAt: Dayjs | undefined): string => {
    const expiryDate = expiresAt?.format('LL')
    switch (ycCreditsState) {
        case 'ending':
            return expiryDate
                ? `Your YC credits expire on ${expiryDate} and won't renew.`
                : "Your YC credits won't renew."
        case 'used_until_renewal':
            return `You'll get $50K in credits every year, forever, until you hit $25M in funding. You've used this year's credits, so you pay for usage until they renew${expiryDate ? ` on ${expiryDate}` : ''}.`
        default:
            return "You'll get $50K in credits every year, forever, until you hit $25M in funding. Your credits will renew automatically."
    }
}

const BADGE_CONFIG: Record<BillingPlan | StartupProgramLabel, CopyVariation> = {
    [BillingPlan.Free]: {
        title: 'Get the whole hog.',
        subtitle: 'Only pay for what you use.',
        backgroundColor: 'bg-danger-highlight',
        getDescription: () => (
            <>
                <p>Add your credit card to remove usage limits and unlock all platform features.</p>
                <p>Set billing limits as low as $0 to control your spend.</p>
                <p className="italic">P.S. You still keep the monthly free allowance for every product!</p>
            </>
        ),
    },
    [BillingPlan.Paid]: {
        title: 'Good call!',
        subtitle: "You're on the Pay-as-you-go plan.",
        backgroundColor: 'bg-warning-highlight',
        getDescription: (_billingPlan: BillingPlan, scrollToProduct: (productType: string) => void) => (
            <p>
                If you're growing like crazy, you might want to check out our{' '}
                {scrollToProduct ? (
                    <>
                        <Link onClick={() => scrollToProduct('platform_and_support')}>Platform packages</Link>
                    </>
                ) : (
                    'Platform packages'
                )}
                .
            </p>
        ),
    },
    [BillingPlan.Teams]: {
        title: 'Good call!',
        subtitle: "You're on the Pay-as-you-go plan (with Teams add-on).",
        backgroundColor: 'bg-warning-highlight',
        getDescription: (_billingPlan: BillingPlan, scrollToProduct: (productType: string) => void) => (
            <p>
                If you're growing like crazy, you might want to check out the{' '}
                {scrollToProduct ? <Link onClick={() => scrollToProduct('enterprise')}>Enterprise</Link> : 'Enterprise'}{' '}
                plan.
            </p>
        ),
    },
    [BillingPlan.Boost]: {
        title: 'Good call!',
        subtitle: "You're on the Pay-as-you-go plan (with Boost add-on).",
        backgroundColor: 'bg-warning-highlight',
        getDescription: (_billingPlan: BillingPlan, scrollToProduct: (productType: string) => void) => (
            <p>
                If you're growing like crazy, you might want to check out the{' '}
                {scrollToProduct ? <Link onClick={() => scrollToProduct('enterprise')}>Enterprise</Link> : 'Enterprise'}{' '}
                plan.
            </p>
        ),
    },
    [BillingPlan.Scale]: {
        title: 'Good call!',
        subtitle: "You're on the Pay-as-you-go plan (with Scale add-on).",
        backgroundColor: 'bg-warning-highlight',
        getDescription: (_billingPlan: BillingPlan, scrollToProduct: (productType: string) => void) => (
            <p>
                If you're growing like crazy, you might want to check out the{' '}
                {scrollToProduct ? <Link onClick={() => scrollToProduct('enterprise')}>Enterprise</Link> : 'Enterprise'}{' '}
                plan.
            </p>
        ),
    },
    [BillingPlan.Enterprise]: {
        title: 'Good call!',
        subtitle: "You're on the Enterprise plan.",
        backgroundColor: 'bg-success-highlight',
        getDescription: () => <p>It doesn't get any better than this!</p>,
    },
    [StartupProgramLabel.Startup]: {
        title: 'Good for you!',
        subtitle: "You're on the startup plan.",
        backgroundColor: 'bg-warning-highlight',
        getDescription: (_billingPlan: BillingPlan, scrollToProduct: (productType: string) => void) => (
            <>
                <p>
                    If you're growing like crazy, you might want to check out our{' '}
                    {scrollToProduct ? (
                        <Link onClick={() => scrollToProduct('platform_and_support')}>Platform packages</Link>
                    ) : (
                        'Platform packages'
                    )}
                    .
                </p>
                <p className="text-xs text-secondary mb-0">
                    Credits can't be used for every product. Check{' '}
                    <Link to="https://posthog.com/startups" target="_blank">
                        posthog.com/startups
                    </Link>{' '}
                    for details.
                </p>
            </>
        ),
    },
    [StartupProgramLabel.YC]: {
        title: 'Lucky you!',
        subtitle: "You're on the YC plan.",
        backgroundColor: 'bg-warning-highlight',
        getDescription: (
            _billingPlan: BillingPlan,
            scrollToProduct: (productType: string) => void,
            ycCreditsSummary: string
        ) => (
            <>
                <p>{ycCreditsSummary}</p>
                <p>
                    If you're growing like crazy, you might want to check out our{' '}
                    {scrollToProduct ? (
                        <Link onClick={() => scrollToProduct('platform_and_support')}>Platform packages</Link>
                    ) : (
                        'Platform packages'
                    )}
                    .
                </p>
                <p className="text-xs text-secondary mb-0">
                    Credits can't be used for every product. Check{' '}
                    <Link to="https://posthog.com/startups" target="_blank">
                        posthog.com/startups
                    </Link>{' '}
                    for details.
                </p>
            </>
        ),
    },
}

export const BillingHero = ({ product }: { product: BillingProductV2Type }): JSX.Element | null => {
    const { startPaymentEntryFlow } = useActions(paymentEntryLogic)
    const { billing, redirectPath, billingPlan, startupProgramLabelCurrent, ycCreditsState, isManagedAccount } =
        useValues(billingLogic)
    const { scrollToProduct } = useActions(billingLogic)
    const { isPlanComparisonModalOpen, billingProductLoading } = useValues(billingProductLogic({ product }))
    const { toggleIsPlanComparisonModalOpen } = useActions(billingProductLogic({ product }))
    const showUpgradeToManagedAccount = useFeatureFlag('SHOW_UPGRADE_TO_MANAGED_ACCOUNT')

    if (!billingPlan) {
        return null
    }

    const showUpgradeOptions = billingPlan === BillingPlan.Free && (!isManagedAccount || showUpgradeToManagedAccount)
    // YC credits that are used up and do not renew no longer cover any usage, so show the plan the customer pays on
    const startupProgramLabel = ycCreditsState === 'ended' ? null : startupProgramLabelCurrent
    const copyVariation =
        (startupProgramLabel ? BADGE_CONFIG[startupProgramLabel] : BADGE_CONFIG[billingPlan]) ||
        BADGE_CONFIG[BillingPlan.Paid]
    const planBadge =
        (startupProgramLabel ? STARTUP_PROGRAM_BADGES[startupProgramLabel] : PLAN_BADGES[billingPlan]) ||
        PLAN_BADGES[BillingPlan.Paid]

    return (
        <div className={`relative rounded-lg ${copyVariation.backgroundColor}`}>
            <div className="@container p-4 relative">
                <img
                    src={planBadge}
                    alt={startupProgramLabel ? `${startupProgramLabel} plan badge` : `${billingPlan} plan badge`}
                    className="float-right w-[33cqw] min-w-32 max-w-48 ml-6 mb-4"
                />
                {copyVariation.title && <h1 className="mb-0">{copyVariation.title}</h1>}
                {copyVariation.subtitle && <h1 className="text-danger leading-tight">{copyVariation.subtitle}</h1>}
                <div className="mt-2">
                    {copyVariation.getDescription(
                        billingPlan,
                        scrollToProduct,
                        getYcCreditsSummary(ycCreditsState, billing?.amount_off_expires_at)
                    )}
                </div>
                {showUpgradeOptions && (
                    <div className="flex items-center gap-2">
                        <BillingUpgradeCTA
                            className="inline-block"
                            type="primary"
                            status="alt"
                            data-attr="billing-page-core-upgrade-cta"
                            disableClientSideRouting
                            loading={!!billingProductLoading}
                            onClick={() => startPaymentEntryFlow(product, redirectPath)}
                        >
                            Upgrade now
                        </BillingUpgradeCTA>
                        <LemonButton
                            className="inline-block"
                            onClick={() => toggleIsPlanComparisonModalOpen()}
                            type="primary"
                        >
                            Compare plans
                        </LemonButton>
                    </div>
                )}
            </div>
            {showUpgradeOptions && (
                <PlanComparisonModal
                    product={product}
                    title="Compare our plans"
                    includeAddons={false}
                    modalOpen={isPlanComparisonModalOpen}
                    onClose={() => toggleIsPlanComparisonModalOpen()}
                />
            )}
        </div>
    )
}