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.

10.2 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { Form } from 'kea-forms'
import { useRef } from 'react'

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

import { LemonField } from 'lib/lemon-ui/LemonField'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { billingLogic } from 'lib/logic/billing/billingLogic'
import { billingProductDisplayName } from 'lib/logic/billing/billingProductDisplayName'

import { BillingProductV2Type } from '~/types'

import { isBillingLimitShown } from './billing-utils'
import { billingProductLogic } from './billingProductLogic'

export const BillingLimit = ({ product }: { product: BillingProductV2Type }): JSX.Element | null => {
    const limitInputRef = useRef<HTMLInputElement | null>(null)
    const { billing, billingLoading } = useValues(billingLogic)
    const {
        isEditingBillingLimit,
        customLimitUsd,
        hasCustomLimitSet,
        currentAndUpgradePlans,
        billingLimitNextPeriod,
        billingLimitConfig,
        removingBillingLimitNextPeriod,
    } = useValues(billingProductLogic({ product, billingLimitInputRef: limitInputRef }))
    const { setIsEditingBillingLimit, setBillingLimitInput, submitBillingLimitInput, removeBillingLimitNextPeriod } =
        useActions(billingProductLogic({ product }))

    const initialBillingLimit = currentAndUpgradePlans?.currentPlan?.initial_billing_limit
    const usingInitialBillingLimit = customLimitUsd === initialBillingLimit
    const hasBillingLimitNextPeriod = billingLimitNextPeriod !== null

    if (!isBillingLimitShown(product, billing)) {
        return null
    }

    return (
        <Form formKey="billingLimitInput" props={{ product: product }} logic={billingProductLogic} enableFormOnSubmit>
            <div
                className="border-t border-primary px-8 py-4"
                data-attr={`billing-limit-input-wrapper-${product.type}`}
            >
                <h4>Billing limit</h4>
                <div className="flex flex-col xl:flex-row w-full items-stretch xl:items-center justify-start xl:justify-between gap-2">
                    <div className="flex items-center gap-1">
                        {!isEditingBillingLimit ? (
                            <>
                                {hasCustomLimitSet ? (
                                    <>
                                        {usingInitialBillingLimit ? (
                                            <Tooltip title="Initial limits protect you from accidentally incurring large unexpected charges. Some features may stop working and data may be dropped if your usage exceeds your limit.">
                                                <span
                                                    className="text-sm"
                                                    data-attr={`default-billing-limit-${product.type}`}
                                                >
                                                    This product has a default initial billing limit of{' '}
                                                    <b>${initialBillingLimit}</b>.
                                                </span>
                                            </Tooltip>
                                        ) : (
                                            <Tooltip title="Set a billing limit to control your recurring costs. Some features may stop working and data may be dropped if your usage exceeds your limit.">
                                                <span
                                                    className="text-sm"
                                                    data-attr={`billing-limit-set-${product.type}`}
                                                >
                                                    You have a <b>${customLimitUsd?.toLocaleString()}</b> billing limit
                                                    set for {billingProductDisplayName(product)}.
                                                </span>
                                            </Tooltip>
                                        )}

                                        <LemonButton
                                            onClick={() => setIsEditingBillingLimit(true)}
                                            status="danger"
                                            size="small"
                                        >
                                            Edit limit
                                        </LemonButton>
                                    </>
                                ) : (
                                    <>
                                        <span className="text-sm" data-attr={`billing-limit-not-set-${product.type}`}>
                                            You do not have a billing limit set for {billingProductDisplayName(product)}
                                            .
                                        </span>
                                        <LemonButton
                                            onClick={() => setIsEditingBillingLimit(true)}
                                            status="danger"
                                            size="small"
                                        >
                                            Set a billing limit
                                        </LemonButton>
                                    </>
                                )}
                            </>
                        ) : (
                            <div className="flex items-start justify-start gap-2.5">
                                <LemonField name="input" className="max-w-52" help={billingLimitConfig.help}>
                                    {({ value, onChange, error }) => (
                                        <LemonInput
                                            inputRef={limitInputRef}
                                            type="number"
                                            fullWidth={false}
                                            status={error ? 'danger' : 'default'}
                                            value={value}
                                            data-attr={`billing-limit-input-${product.type}`}
                                            onChange={onChange}
                                            prefix={<b>$</b>}
                                            disabled={billingLoading}
                                            min={0}
                                            max={billingLimitConfig.max}
                                            step={1}
                                            suffix={<>/ {billing?.billing_period?.interval}</>}
                                            size="small"
                                        />
                                    )}
                                </LemonField>

                                <LemonButton
                                    loading={billingLoading}
                                    type="primary"
                                    size="small"
                                    htmlType="submit"
                                    data-attr={`save-billing-limit-${product.type}`}
                                >
                                    Save
                                </LemonButton>
                                <LemonButton
                                    onClick={() => {
                                        setIsEditingBillingLimit(false)
                                    }}
                                    disabled={billingLoading}
                                    type="secondary"
                                    size="small"
                                >
                                    Cancel
                                </LemonButton>
                                {hasCustomLimitSet ? (
                                    <LemonButton
                                        status="danger"
                                        size="small"
                                        data-attr={`remove-billing-limit-${product.type}`}
                                        tooltip="Remove billing limit"
                                        disabledReason={billingLimitConfig.removalDisabledReason}
                                        onClick={() => {
                                            setBillingLimitInput(null)
                                            submitBillingLimitInput()
                                        }}
                                    >
                                        Remove limit
                                    </LemonButton>
                                ) : null}
                            </div>
                        )}
                    </div>
                    {hasBillingLimitNextPeriod ? (
                        <div className="flex items-center gap-1">
                            <span className="text-sm xl:text-right">
                                Your limit for next period: <b>${billingLimitNextPeriod.toLocaleString()}</b>.
                            </span>
                            <LemonButton
                                size="small"
                                status="danger"
                                loading={removingBillingLimitNextPeriod}
                                tooltip="Remove billing limit for next period"
                                disabledReason={billingLimitConfig.removalDisabledReason}
                                onClick={() => removeBillingLimitNextPeriod(product.type)}
                                data-attr={`remove-billing-limit-next-period-${product.type}`}
                            >
                                Remove limit for next period
                            </LemonButton>
                        </div>
                    ) : null}
                </div>
                {billingLimitConfig.help && !isEditingBillingLimit ? (
                    <div className="text-xs text-secondary mt-2">{billingLimitConfig.help}</div>
                ) : null}
                {billingLimitConfig.currentAboveMaxNotice ? (
                    <div className="text-xs text-secondary mt-2">{billingLimitConfig.currentAboveMaxNotice}</div>
                ) : null}
            </div>
        </Form>
    )
}