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.

7.9 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, selectors } from 'kea'
import posthog from 'posthog-js'

import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { isFileSystemImportFlagEnabled } from 'lib/utils/fileSystemImportFlags'
import { addProductIntent } from 'lib/utils/product-intents'
import { isExternalLink } from 'lib/utils/url'

import { getTreeItemsProducts } from '~/products'
import { FileSystemImport, ProductIntentContext, ProductKey } from '~/queries/schema/schema-general'

import type { ProductPushCampaignApi } from 'products/growth/frontend/generated/api.schemas'

import type { FeatureFlagsSet } from '../../logic/featureFlagLogic'
import { NAV_PANEL_CARD_TYPE, type ProductPushDisplay } from './navPanelAdShared'
import { navPanelAdvertisementLogic } from './NavPanelAdvertisementLogic'
import { getProductPushDisplay } from './navPanelProductPushDisplay'
import { navPanelProductPushWelcomeLogic } from './navPanelProductPushWelcomeLogic'

export type NavPanelProductPushAdLogicProps = {
    campaign: ProductPushCampaignApi
}

// The dismiss state lives in the shared advertisement logic (its keyed instance also backs the
// card's dismiss button), so key it the same way here to read the same `hidden` flag.
const dismissKey = (campaign: ProductPushCampaignApi): string => `product-push-${campaign.id}`

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface navPanelProductPushAdLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    hidden: boolean // navPanelAdvertisementLogic
    destination: string | null
    display: ProductPushDisplay
    flagGated: boolean
    label: string | null
    productInfo: FileSystemImport | undefined
    shouldRender: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface navPanelProductPushAdLogicActions {
    setPendingWelcome: (welcome: import('./navPanelProductPushWelcomeVisibility').PendingProductPushWelcome) => {
        welcome: import('./navPanelProductPushWelcomeVisibility').PendingProductPushWelcome
    } // navPanelProductPushWelcomeLogic
    reportAdClicked: () => {
        value: true
    }
    reportAdDismissed: () => {
        value: true
    }
    reportAdShown: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface navPanelProductPushAdLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        productInfo: (arg: any) => FileSystemImport | undefined
        display: (arg: any) => ProductPushDisplay
        flagGated: (productInfo: FileSystemImport | undefined, featureFlags: FeatureFlagsSet) => boolean
        destination: (display: ProductPushDisplay, productInfo: FileSystemImport | undefined) => string | null
        label: (display: ProductPushDisplay, productInfo: FileSystemImport | undefined) => string | null
        shouldRender: (hidden: boolean, flagGated: boolean, destination: string | null) => boolean
    }
}

export type navPanelProductPushAdLogicType = MakeLogicType<
    navPanelProductPushAdLogicValues,
    navPanelProductPushAdLogicActions,
    NavPanelProductPushAdLogicProps,
    navPanelProductPushAdLogicMeta
>

export const navPanelProductPushAdLogic = kea<navPanelProductPushAdLogicType>([
    path(['lib', 'components', 'NavPanelAdvertisement', 'navPanelProductPushAdLogic']),
    props({} as NavPanelProductPushAdLogicProps),
    key(({ campaign }) => campaign.id),
    connect((props: NavPanelProductPushAdLogicProps) => ({
        values: [
            featureFlagLogic,
            ['featureFlags'],
            navPanelAdvertisementLogic({ dismissKey: dismissKey(props.campaign) }),
            ['hidden'],
        ],
        actions: [navPanelProductPushWelcomeLogic, ['setPendingWelcome']],
    })),
    actions({
        reportAdShown: true,
        reportAdClicked: true,
        reportAdDismissed: true,
    }),
    selectors({
        productInfo: [
            () => [(_, props) => props.campaign],
            (campaign: ProductPushCampaignApi): FileSystemImport | undefined =>
                getTreeItemsProducts().find((p: FileSystemImport) => p.path === campaign.product_path),
        ],
        display: [
            () => [(_, props) => props.campaign],
            (campaign: ProductPushCampaignApi): ProductPushDisplay => getProductPushDisplay(campaign.product_key),
        ],
        // Never advertise a product this user can't open (mirrors the sidebar's flag filtering)
        flagGated: [
            (s) => [s.productInfo, s.featureFlags],
            (
                productInfo: FileSystemImport | undefined,
                featureFlags: import('lib/logic/featureFlagLogic').FeatureFlagsSet
            ): boolean => !!productInfo && !isFileSystemImportFlagEnabled(productInfo, featureFlags),
        ],
        // A surface links to its own href/label; a catalog product resolves them from its catalog entry.
        destination: [
            (s) => [s.display, s.productInfo],
            (display: ProductPushDisplay, productInfo: FileSystemImport | undefined): string | null =>
                display.href ?? productInfo?.href ?? null,
        ],
        label: [
            (s) => [s.display, s.productInfo],
            (display: ProductPushDisplay, productInfo: FileSystemImport | undefined): string | null =>
                display.label ?? productInfo?.displayLabel ?? productInfo?.path ?? null,
        ],
        // flagGated is always false for a surface (no catalog entry, so no flag), so this covers both.
        shouldRender: [
            (s) => [s.hidden, s.flagGated, s.destination],
            (hidden: boolean, flagGated: boolean, destination: string | null): boolean =>
                !hidden && !!destination && !flagGated,
        ],
    }),
    listeners(({ props, values, actions }) => {
        const eventProperties = {
            campaign_id: props.campaign.id,
            product_key: props.campaign.product_key,
            card_type: NAV_PANEL_CARD_TYPE.PRODUCT_PUSH,
        }
        return {
            reportAdShown: () => {
                posthog.capture('nav panel product push shown', eventProperties)
            },
            reportAdClicked: () => {
                posthog.capture('nav panel product push clicked', eventProperties)
                if (Object.values(ProductKey).includes(props.campaign.product_key as ProductKey)) {
                    const productKey = props.campaign.product_key as ProductKey
                    addProductIntent({
                        product_type: productKey,
                        intent_context: ProductIntentContext.NAV_PANEL_ADVERTISEMENT_CLICKED,
                        metadata: { campaign_id: props.campaign.id },
                    })
                    // The welcome opens on the pushed product's own scene, so only a click that can
                    // reach one queues it. An external destination opens a new tab and leaves this one
                    // where it is. A surface such as Slack or Self-driving has no catalog entry, so no
                    // scene identifies it and no setup gate covers it, which would leave its welcome
                    // queued and never opened.
                    if (values.label && values.productInfo && !isExternalLink(values.destination)) {
                        actions.setPendingWelcome({
                            campaignId: props.campaign.id,
                            productKey,
                            label: values.label,
                            text: props.campaign.reason_text || values.display.tagline,
                        })
                    }
                }
            },
            reportAdDismissed: () => {
                posthog.capture('nav panel product push dismissed', eventProperties)
            },
        }
    }),
    afterMount(({ actions, values }) => {
        if (values.shouldRender) {
            actions.reportAdShown()
        }
    }),
])