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

import api from 'lib/api'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { hogql } from 'lib/utils/hogql'
import { sceneLogic } from 'scenes/sceneLogic'

const CHECK_INTERVAL_MS = 1000 * 60 * 10 // 10 minutes

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface reverseProxyCheckerLogicValues {
    hasReverseProxy: boolean | null
    hasReverseProxyLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface reverseProxyCheckerLogicActions {
    loadHasReverseProxy: () => any
    loadHasReverseProxyFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadHasReverseProxySuccess: (
        hasReverseProxy: boolean | null,
        payload?: any
    ) => {
        hasReverseProxy: boolean | null
        payload?: any
    }
}

export type reverseProxyCheckerLogicType = MakeLogicType<
    reverseProxyCheckerLogicValues,
    reverseProxyCheckerLogicActions
>

export const reverseProxyCheckerLogic = kea<reverseProxyCheckerLogicType>([
    path(['components', 'ReverseProxyChecker', 'reverseProxyCheckerLogic']),
    loaders(({ values, cache }) => ({
        hasReverseProxy: [
            // null until the detection query resolves — consumers must distinguish "not yet
            // checked" from a confirmed `false` so they don't act before the result is in.
            null as boolean | null,
            {
                loadHasReverseProxy: async () => {
                    if (cache.lastCheckedTimestamp > Date.now() - CHECK_INTERVAL_MS) {
                        return values.hasReverseProxy
                    }

                    cache.lastCheckedTimestamp = Date.now()

                    const query = hogql`
                        SELECT DISTINCT properties.$lib_custom_api_host AS lib_custom_api_host
                        FROM events
                        WHERE timestamp >= now() - INTERVAL 1 DAY
                        AND timestamp <= now()
                        AND properties.$lib_custom_api_host IS NOT NULL
                        AND event IN ('$pageview', '$screen')
                        LIMIT 10`

                    const currentScene = sceneLogic.findMounted()?.values.activeSceneId ?? 'Onboarding'
                    try {
                        const res = await api.queryHogQL(query, {
                            scene: currentScene,
                            productKey: 'platform_and_support',
                        })
                        return !!res.results?.find((x) => !!x[0])
                    } catch (error) {
                        // This check is advisory (used only to auto-complete a setup task).
                        // Swallow errors so kea-loaders does not surface a user-visible toast
                        // on every scene that mounts ProductSetupButton.
                        posthog.captureException(error, {
                            posthog_source: 'reverseProxyCheckerLogic.loadHasReverseProxy',
                        })
                        return values.hasReverseProxy
                    }
                },
            },
        ],
    })),
    listeners(({ values }) => ({
        loadHasReverseProxySuccess: () => {
            if (values.hasReverseProxy) {
                globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.SetUpReverseProxy)
            }
        },
    })),
    afterMount(({ actions }) => {
        actions.loadHasReverseProxy()
    }),
])