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

import api from 'lib/api'
import { productSetupStatusLogic } from 'lib/components/ProductEmptyState/productSetupStatusLogic'
import type { ProductSetupStatus } from 'lib/components/ProductEmptyState/types'
import { teamLogic } from 'scenes/teamLogic'

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

import { hogFunctionsList } from 'products/cdp/frontend/generated/api'

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface webScriptsSetupLogicActions {
    setDetectedStatus: (status: ProductSetupStatus) => {
        status: ProductSetupStatus
    } // productSetupStatusLogic
    loadScriptCount: (_: void) => void
    loadScriptCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadScriptCountSuccess: (
        scriptCount: number,
        payload?: void
    ) => {
        scriptCount: number
        payload?: void
    }
}

export type webScriptsSetupLogicType = MakeLogicType<webScriptsSetupLogicValues, webScriptsSetupLogicActions>

/**
 * Setup detection for the web scripts empty state. Web scripts are creation-first,
 * so "set up" simply means the project has at least one script the scene would list:
 * a `site_app` hog function, or a legacy plugin site app. Re-checks on every mount
 * (the scene gate mounts it), which covers returning from the creation page.
 */
export const webScriptsSetupLogic = kea<webScriptsSetupLogicType>([
    path(['products', 'cdp', 'frontend', 'emptyState', 'webScriptsSetupLogic']),
    connect(() => ({
        actions: [productSetupStatusLogic({ productKey: ProductKey.SITE_APPS }), ['setDetectedStatus']],
        values: [
            productSetupStatusLogic({ productKey: ProductKey.SITE_APPS }),
            ['status as setupStatus'],
            teamLogic,
            ['currentProjectId'],
        ],
    })),
    loaders(({ values }) => ({
        scriptCount: [
            null as number | null,
            {
                loadScriptCount: async (_: void, breakpoint): Promise<number> => {
                    // The scene lists plugin-backed site apps alongside hog functions, so counting
                    // only hog functions would tell a project still running legacy site apps to
                    // create its first one and hide the ones serving its site today.
                    const [hogFunctions, legacySiteApps] = await Promise.all([
                        hogFunctionsList(String(values.currentProjectId), { type: ['site_app'], limit: 1 }),
                        api.pipelineFrontendAppsConfigs.list({ limit: 1 }),
                    ])
                    breakpoint()
                    return hogFunctions.count + legacySiteApps.count
                },
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        loadScriptCountSuccess: ({ scriptCount }) => {
            actions.setDetectedStatus(scriptCount !== null && scriptCount > 0 ? 'has-data' : 'needs-setup')
        },
        loadScriptCountFailure: () => {
            // 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.loadScriptCount()
    }),
])