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.

6.8 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import posthog from 'posthog-js'

import { billingLogic } from 'lib/logic/billing/billingLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { userLogic } from 'scenes/userLogic'

import { AvailableFeature } from '~/types'

import type { BillingFeatureType, BillingType, PreflightStatus } from '../../../types'

export type GuardAvailableFeatureFn = (
    featureKey?: AvailableFeature,
    featureAvailableCallback?: () => void,
    options?: {
        guardOnCloud?: boolean
        guardOnSelfHosted?: boolean
        currentUsage?: number
        isGrandfathered?: boolean
    }
) => boolean

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface upgradeModalLogicValues {
    billing: BillingType | null // billingLogic
    preflight: PreflightStatus | null // preflightLogic
    availableFeature: (feature: AvailableFeature) => BillingFeatureType | null | undefined // userLogic
    hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogic
    guardAvailableFeature: GuardAvailableFeatureFn
    projectLimit: number
    shouldShowPlatformAddonMessage: boolean
    upgradeModalFeatureKey: AvailableFeature | null
    upgradeModalFeatureUsage: number | null
    upgradeModalIsGrandfathered: boolean | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface upgradeModalLogicActions {
    hideUpgradeModal: () => {
        value: true
    }
    showUpgradeModal: (
        featureKey: AvailableFeature,
        currentUsage?: number,
        isGrandfathered?: boolean
    ) => {
        currentUsage: number | undefined
        featureKey: AvailableFeature
        isGrandfathered: boolean | undefined
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface upgradeModalLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        projectLimit: (availableFeature: (feature: AvailableFeature) => BillingFeatureType | null | undefined) => number
        shouldShowPlatformAddonMessage: (
            upgradeModalFeatureKey: AvailableFeature | null,
            billing: BillingType | null
        ) => boolean
        guardAvailableFeature: (
            preflight: PreflightStatus | null,
            hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean
        ) => GuardAvailableFeatureFn
    }
}

export type upgradeModalLogicType = MakeLogicType<
    upgradeModalLogicValues,
    upgradeModalLogicActions,
    Record<string, any>,
    upgradeModalLogicMeta
>

export const upgradeModalLogic = kea<upgradeModalLogicType>([
    path(['lib', 'components', 'UpgradeModal', 'upgradeModalLogic']),
    connect(() => ({
        values: [
            preflightLogic,
            ['preflight'],
            billingLogic,
            ['billing'],
            userLogic,
            ['hasAvailableFeature', 'availableFeature'],
        ],
    })),
    actions({
        showUpgradeModal: (featureKey: AvailableFeature, currentUsage?: number, isGrandfathered?: boolean) => ({
            featureKey,
            currentUsage,
            isGrandfathered,
        }),
        hideUpgradeModal: true,
    }),
    reducers({
        upgradeModalFeatureKey: [
            null as AvailableFeature | null,
            {
                showUpgradeModal: (_, { featureKey }) => featureKey,
                hideUpgradeModal: () => null,
            },
        ],
        upgradeModalFeatureUsage: [
            null as number | null,
            {
                showUpgradeModal: (_, { currentUsage }) => currentUsage ?? null,
                hideUpgradeModal: () => null,
            },
        ],
        upgradeModalIsGrandfathered: [
            null as boolean | null,
            {
                showUpgradeModal: (_, { isGrandfathered }) => isGrandfathered ?? null,
                hideUpgradeModal: () => null,
            },
        ],
    }),
    selectors(({ actions }) => ({
        projectLimit: [
            (s) => [s.availableFeature],
            (
                availableFeature: (feature: AvailableFeature) => null | import('~/types').BillingFeatureType | undefined
            ) => availableFeature(AvailableFeature.ORGANIZATIONS_PROJECTS)?.limit ?? 6,
        ],
        shouldShowPlatformAddonMessage: [
            (s) => [s.upgradeModalFeatureKey, s.billing],
            (upgradeModalFeatureKey: AvailableFeature | null, billing: null | import('~/types').BillingType) => {
                if (upgradeModalFeatureKey !== AvailableFeature.ORGANIZATIONS_PROJECTS) {
                    return false
                }

                const platformAndSupportProduct = billing?.products?.find(
                    (product) => product.type === 'platform_and_support'
                )
                const hasPlatformAddon = platformAndSupportProduct?.addons?.some((addon) => addon.subscribed) ?? false

                return billing?.subscription_level === 'paid' && !hasPlatformAddon
            },
        ],
        guardAvailableFeature: [
            (s) => [s.preflight, s.hasAvailableFeature],
            (
                preflight: null | import('~/types').PreflightStatus,
                hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean
            ): GuardAvailableFeatureFn => {
                return (featureKey, featureAvailableCallback, options): boolean => {
                    if (!featureKey) {
                        featureAvailableCallback?.()
                        return true
                    }
                    const {
                        guardOnCloud = true,
                        guardOnSelfHosted = true,
                        currentUsage,
                        isGrandfathered,
                    } = options || {}
                    let featureAvailable: boolean
                    if (!preflight) {
                        featureAvailable = false
                    } else if (!guardOnCloud && preflight.cloud) {
                        featureAvailable = true
                    } else if (!guardOnSelfHosted && !preflight.cloud) {
                        featureAvailable = true
                    } else {
                        featureAvailable = hasAvailableFeature(featureKey, currentUsage)
                    }

                    if (!featureAvailable) {
                        actions.showUpgradeModal(featureKey, currentUsage, isGrandfathered)
                    } else {
                        featureAvailableCallback?.()
                    }

                    return featureAvailable
                }
            },
        ],
    })),
    listeners(() => ({
        showUpgradeModal: ({ featureKey }) => {
            posthog.capture('upgrade modal shown', { featureName: featureKey })
        },
    })),
])