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.9 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path } from 'kea'
import { loaders } from 'kea-loaders'

import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'
import type { ProductSetupStatus } from 'lib/components/ProductEmptyState/types'
import { productToursLogic } from 'scenes/product-tours/productToursLogic'
import { teamLogic } from 'scenes/teamLogic'

import { ProductKey } from '~/queries/schema/schema-general'

import { productToursList } from 'products/product_tours/frontend/generated/api'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface productToursSetupLogicValues {
    setupStatus: ProductSetupStatus // productSetupStatusLogic
    currentProjectId: number | string // teamLogic
    tourCount: number | null
    tourCountLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface productToursSetupLogicActions {
    setDetectedStatus: (status: ProductSetupStatus) => {
        status: ProductSetupStatus
    } // productSetupStatusLogic
    createTour: (name: string) => {
        name: string
    } // productToursLogic
    loadTourCount: (_: void) => void
    loadTourCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadTourCountSuccess: (
        tourCount: number,
        payload?: void
    ) => {
        tourCount: number
        payload?: void
    }
}

export type productToursSetupLogicType = MakeLogicType<productToursSetupLogicValues, productToursSetupLogicActions>

/**
 * Setup detection for the product tours empty state. Tours are a creation-first
 * product, so "set up" simply means the project has at least one tour - no event
 * or traffic signal involved. Re-checks on every mount (the scene gate mounts it),
 * which covers returning from the tour editor.
 *
 * The empty state's primary action dispatches `productToursLogic.createTour`, the same
 * action the list scene's modal submits. `/product_tours/new` has no working save path,
 * so a tour has to be created before the editor opens, and going through the shared
 * action keeps first-tour creations in the `product tour created` event stream.
 */
export const productToursSetupLogic = kea<productToursSetupLogicType>([
    path(['products', 'product_tours', 'frontend', 'emptyState', 'productToursSetupLogic']),
    connect(() => ({
        actions: [
            productSetupStatusLogic({ productKey: ProductKey.PRODUCT_TOURS }),
            ['setDetectedStatus'],
            productToursLogic,
            ['createTour'],
        ],
        values: [
            productSetupStatusLogic({ productKey: ProductKey.PRODUCT_TOURS }),
            ['status as setupStatus'],
            teamLogic,
            ['currentProjectId'],
        ],
    })),
    loaders(({ values }) => ({
        tourCount: [
            null as number | null,
            {
                loadTourCount: async (_: void, breakpoint): Promise<number> => {
                    const response = await productToursList(String(values.currentProjectId), { limit: 1 })
                    breakpoint()
                    return response.count
                },
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        loadTourCountSuccess: ({ tourCount }) => {
            actions.setDetectedStatus(tourCount !== null && tourCount > 0 ? 'has-data' : 'needs-setup')
        },
        loadTourCountFailure: () => {
            // Never strand the gate on its spinner: if nothing has answered yet, fail
            // open to the real scene. Don't downgrade an existing answer on a blip.
            if (values.setupStatus === 'loading') {
                actions.setDetectedStatus('unknown')
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadTourCount()
    }),
])