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.

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

import { NAV_PANEL_CARD_TYPE } from './navPanelAdShared'
import type { PendingProductPushWelcome } from './navPanelProductPushWelcomeVisibility'

/** Which control closed the welcome modal, reported as the `source` of `welcome closed`. */
export type ProductPushWelcomeCloseSource = 'start_exploring' | 'modal_close'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface navPanelProductPushWelcomeLogicValues {
    openFor: PendingProductPushWelcome | null
    pending: PendingProductPushWelcome | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface navPanelProductPushWelcomeLogicActions {
    closeWelcome: (source: ProductPushWelcomeCloseSource) => {
        source: ProductPushWelcomeCloseSource
    }
    openWelcome: (welcome: PendingProductPushWelcome) => {
        welcome: PendingProductPushWelcome
    }
    setPendingWelcome: (welcome: PendingProductPushWelcome) => {
        welcome: PendingProductPushWelcome
    }
}

export type navPanelProductPushWelcomeLogicType = MakeLogicType<
    navPanelProductPushWelcomeLogicValues,
    navPanelProductPushWelcomeLogicActions
>

/**
 * Carries a click on the nav product push card through to the product's own scene. Both values are
 * in-memory: the card links within the app, so the navigation preserves them, and a click the user
 * never followed through on should not resurface in a new tab.
 */
export const navPanelProductPushWelcomeLogic = kea<navPanelProductPushWelcomeLogicType>([
    path(['lib', 'components', 'NavPanelAdvertisement', 'navPanelProductPushWelcomeLogic']),
    actions({
        setPendingWelcome: (welcome: PendingProductPushWelcome) => ({ welcome }),
        openWelcome: (welcome: PendingProductPushWelcome) => ({ welcome }),
        closeWelcome: (source: ProductPushWelcomeCloseSource) => ({ source }),
    }),
    reducers({
        pending: [
            null as PendingProductPushWelcome | null,
            {
                setPendingWelcome: (_, { welcome }) => welcome,
                // Opening consumes the click, so one click can only ever raise one modal.
                openWelcome: () => null,
                closeWelcome: () => null,
            },
        ],
        openFor: [
            null as PendingProductPushWelcome | null,
            {
                openWelcome: (_, { welcome }) => welcome,
                closeWelcome: () => null,
                setPendingWelcome: () => null,
            },
        ],
    }),
    listeners(({ selectors }) => ({
        openWelcome: ({ welcome }) => {
            posthog.capture('nav panel product push welcome shown', {
                campaign_id: welcome.campaignId,
                product_key: welcome.productKey,
                card_type: NAV_PANEL_CARD_TYPE.PRODUCT_PUSH,
            })
        },
        // `openFor` is already cleared by the time this runs, so the card it described comes from
        // the state the close acted on.
        closeWelcome: ({ source }, _, __, previousState) => {
            const welcome = selectors.openFor(previousState)
            if (!welcome) {
                return
            }
            posthog.capture('nav panel product push welcome closed', {
                campaign_id: welcome.campaignId,
                product_key: welcome.productKey,
                card_type: NAV_PANEL_CARD_TYPE.PRODUCT_PUSH,
                source,
            })
        },
    })),
])