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.

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

import { IconChevronDown, IconChevronRight } from '@posthog/icons'
import { LemonButton } from '@posthog/lemon-ui'

import { billingLogic } from 'lib/logic/billing/billingLogic'
import { billingProductDisplayName, billingProductRowDisplayName } from 'lib/logic/billing/billingProductDisplayName'
import { humanFriendlyCurrency } from 'lib/utils/numbers'
import { capitalizeFirstLetter } from 'lib/utils/strings'

import { BillingProductV2Type } from '~/types'

import { createGaugeItems, isBillingLimitShown } from './billing-utils'
import { BillingGauge } from './BillingGauge'
import { billingProductLogic } from './billingProductLogic'
import { BillingProductPricingTable } from './BillingProductPricingTable'

// Companions bill under the parent card but outside its billing limit, so the card's own amounts leave them out.
// The parent card says whether its layout has the limit block and a projected amount, so the section mentions only those.
export const BillingCompanionSection = ({
    product,
    cardShowsBillingLimit,
    cardShowsProjection,
}: {
    product: BillingProductV2Type
    cardShowsBillingLimit: boolean
    cardShowsProjection: boolean
}): JSX.Element | null => {
    const { billing, isUnlicensedDebug } = useValues(billingLogic)
    const { noLimitCompanionAmounts, totalsIncludingCompanions, variantExpandedStates } = useValues(
        billingProductLogic({ product })
    )
    const { toggleVariantExpanded } = useActions(billingProductLogic({ product }))

    if (isUnlicensedDebug || noLimitCompanionAmounts.length === 0) {
        return null
    }

    const periodLabel = `${billing?.billing_period?.interval || 'month'}-to-date`

    return (
        <div className="border-t border-primary px-8 py-4" data-attr={`billing-companions-${product.type}`}>
            <h4 className="mb-0">Not covered by your billing limit</h4>
            {cardShowsBillingLimit && isBillingLimitShown(product, billing) && (
                <p className="text-sm text-secondary mb-0">
                    Your {billingProductDisplayName(product)} billing limit does not cap these charges.
                </p>
            )}
            <div className="space-y-4 mt-4">
                {noLimitCompanionAmounts.map(({ companion, currentAmount, projectedAmount }) => {
                    const isExpanded = !!variantExpandedStates?.[companion.type]
                    const displayName = billingProductRowDisplayName(companion)
                    return (
                        <div key={companion.type} data-attr={`billing-companion-${companion.type}`}>
                            <div className="grid grid-cols-[auto_1fr_130px_100px] gap-4 items-center">
                                <LemonButton
                                    icon={isExpanded ? <IconChevronDown /> : <IconChevronRight />}
                                    size="small"
                                    aria-label={`${isExpanded ? 'Hide' : 'Show'} ${displayName} details`}
                                    onClick={() => toggleVariantExpanded(companion.type)}
                                />
                                <h4 className="mb-0 font-bold">{displayName}</h4>
                                <div className="flex flex-col items-end">
                                    <span className="font-bold text-lg leading-5">
                                        {humanFriendlyCurrency(currentAmount)}
                                    </span>
                                    <span className="text-xs text-secondary">{capitalizeFirstLetter(periodLabel)}</span>
                                </div>
                                <div className="flex flex-col items-end">
                                    <span className="text-secondary text-lg leading-5">
                                        {humanFriendlyCurrency(projectedAmount)}
                                    </span>
                                    <span className="text-xs text-secondary">Projected</span>
                                </div>
                            </div>
                            {isExpanded && (
                                <div className="mt-4">
                                    <div className="ml-16">
                                        <BillingGauge items={createGaugeItems(companion)} product={companion} />
                                    </div>
                                    <BillingProductPricingTable product={companion} />
                                </div>
                            )}
                        </div>
                    )
                })}
            </div>
            {product.subscribed && cardShowsProjection && (
                <p className="text-sm mt-4 mb-0" data-attr={`billing-companions-total-${product.type}`}>
                    <span>Total including this section:&nbsp;</span>
                    <span translate="no">{humanFriendlyCurrency(totalsIncludingCompanions.currentTotal)}</span>
                    <span>{`\u00a0${periodLabel},\u00a0`}</span>
                    <span translate="no">{humanFriendlyCurrency(totalsIncludingCompanions.projectedTotal)}</span>
                    <span>&nbsp;projected</span>
                </p>
            )}
        </div>
    )
}