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, afterMount, connect, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'

import { preflightLogic } from 'lib/logic/preflightLogic'
import { organizationLogic } from 'scenes/organizationLogic'
import { teamLogic } from 'scenes/teamLogic'

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

import type { OrganizationType } from '../../../types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface navPanelProductPushLogicValues {
    currentOrganization: OrganizationType | null // organizationLogic
    isCloudOrDev: boolean | undefined // preflightLogic
    currentTeamId: number | null // teamLogic
    activeCampaign: ProductPushCampaignApi | null
    activeCampaignLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface navPanelProductPushLogicActions {
    loadActiveCampaign: () => any
    loadActiveCampaignFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadActiveCampaignSuccess: (
        activeCampaign: ProductPushCampaignApi | null,
        payload?: any
    ) => {
        activeCampaign: ProductPushCampaignApi | null
        payload?: any
    }
}

export type navPanelProductPushLogicType = MakeLogicType<
    navPanelProductPushLogicValues,
    navPanelProductPushLogicActions
>

export const navPanelProductPushLogic = kea<navPanelProductPushLogicType>([
    path(['lib', 'components', 'NavPanelAdvertisement', 'navPanelProductPushLogic']),
    connect(() => ({
        values: [
            organizationLogic,
            ['currentOrganization'],
            preflightLogic,
            ['isCloudOrDev'],
            teamLogic,
            ['currentTeamId'],
        ],
    })),
    loaders(({ values }) => ({
        activeCampaign: [
            null as ProductPushCampaignApi | null,
            {
                loadActiveCampaign: async (): Promise<ProductPushCampaignApi | null> => {
                    if (!values.isCloudOrDev || !values.currentOrganization) {
                        return null
                    }

                    try {
                        // team_id lets the backend hide the campaign in projects that already
                        // use the product. 204 (nothing to show) resolves to undefined.
                        return (
                            (await api.productPushCampaignActiveRetrieve(
                                values.currentOrganization.id,
                                values.currentTeamId ? { team_id: values.currentTeamId } : undefined
                            )) ?? null
                        )
                    } catch {
                        // The promo card is best-effort — never surface an error toast for it
                        return null
                    }
                },
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        // The afterMount load may race preflight: its loader gate returns null until
        // preflight resolves, so retry once cloud-ness is actually known.
        [preflightLogic.actionTypes.loadPreflightSuccess]: () => {
            if (values.activeCampaign === null && !values.activeCampaignLoading) {
                actions.loadActiveCampaign()
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadActiveCampaign()
    }),
])