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.3 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 { teamLogic } from 'scenes/teamLogic'

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

import { endpointsList } from 'products/endpoints/frontend/generated/api'

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface endpointsSetupLogicActions {
    setDetectedStatus: (status: ProductSetupStatus) => {
        status: ProductSetupStatus
    } // productSetupStatusLogic
    loadEndpointCount: (_: void) => void
    loadEndpointCountFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadEndpointCountSuccess: (
        endpointCount: number,
        payload?: void
    ) => {
        endpointCount: number
        payload?: void
    }
}

export type endpointsSetupLogicType = MakeLogicType<endpointsSetupLogicValues, endpointsSetupLogicActions>

/**
 * Setup detection for the endpoints empty state. Endpoints are a creation-first
 * product, so "set up" simply means the project has at least one endpoint - no
 * event or traffic signal involved. Re-checks on every mount (the scene gate
 * mounts it), which covers returning from the SQL editor after saving one.
 */
export const endpointsSetupLogic = kea<endpointsSetupLogicType>([
    path(['products', 'endpoints', 'frontend', 'emptyState', 'endpointsSetupLogic']),
    connect(() => ({
        actions: [productSetupStatusLogic({ productKey: ProductKey.ENDPOINTS }), ['setDetectedStatus']],
        values: [
            productSetupStatusLogic({ productKey: ProductKey.ENDPOINTS }),
            ['status as setupStatus'],
            teamLogic,
            ['currentProjectId'],
        ],
    })),
    loaders(({ values }) => ({
        endpointCount: [
            null as number | null,
            {
                loadEndpointCount: async (_: void, breakpoint): Promise<number> => {
                    const response = await endpointsList(String(values.currentProjectId), { limit: 1, offset: 0 })
                    breakpoint()
                    return response.count
                },
            },
        ],
    })),
    listeners(({ actions, values }) => ({
        loadEndpointCountSuccess: ({ endpointCount }) => {
            actions.setDetectedStatus(endpointCount !== null && endpointCount > 0 ? 'has-data' : 'needs-setup')
        },
        loadEndpointCountFailure: () => {
            // 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.loadEndpointCount()
    }),
])