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.

4.1 KB
Raw Blame
import { MakeLogicType, actions, connect, events, kea, path, reducers, selectors } from 'kea'
import { router, urlToAction } from 'kea-router'

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

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface appLogicValues {
    featureFlagsTimedOut: boolean
    isLoginPage: boolean
    showApp: boolean | PreflightStatus | null
    showingDelayedSpinner: boolean
    showingDevTools: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface appLogicActions {
    enableDelayedSpinner: () => {
        value: true
    }
    ignoreFeatureFlags: () => {
        value: true
    }
    showDevTools: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface appLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        isLoginPage: (location: { hash: string; pathname: string; search: string }) => boolean
        showApp: (
            userLoading: boolean,
            user: null | import('../types').UserType,
            receivedFeatureFlags: boolean,
            featureFlagsTimedOut: boolean,
            preflightLoading: boolean,
            preflight: PreflightStatus | null,
            isLoginPage: boolean
        ) => boolean | PreflightStatus | null
    }
}

export type appLogicType = MakeLogicType<appLogicValues, appLogicActions, Record<string, any>, appLogicMeta>

export const appLogic = kea<appLogicType>([
    path(['scenes', 'App']),

    connect([teamLogic, organizationLogic, preflightLogic]),
    actions({
        enableDelayedSpinner: true,
        ignoreFeatureFlags: true,
        showDevTools: true,
    }),
    reducers({
        showingDelayedSpinner: [false, { enableDelayedSpinner: () => true }],
        featureFlagsTimedOut: [false, { ignoreFeatureFlags: () => true }],
        showingDevTools: [false, { showDevTools: () => true }],
    }),
    selectors({
        isLoginPage: [() => [router.selectors.location], ({ pathname }): boolean => pathname === '/login'],
        showApp: [
            (s) => [
                userLogic.selectors.userLoading,
                userLogic.selectors.user,
                featureFlagLogic.selectors.receivedFeatureFlags,
                s.featureFlagsTimedOut,
                preflightLogic.selectors.preflightLoading,
                preflightLogic.selectors.preflight,
                s.isLoginPage,
            ],
            (
                userLoading: boolean,
                user: null | import('../types').UserType,
                receivedFeatureFlags: boolean,
                featureFlagsTimedOut: boolean,
                preflightLoading: boolean,
                preflight: null | import('../types').PreflightStatus,
                isLoginPage: boolean
            ) => {
                // Signup reads maintenance redirect flags when it mounts, so only login can bypass this wait.
                const flagsReady = receivedFeatureFlags || featureFlagsTimedOut || (!user && isLoginPage)
                return (!userLoading || user) && flagsReady && (!preflightLoading || preflight)
            },
        ],
    }),
    events(({ actions, cache }) => ({
        afterMount: () => {
            cache.disposables.add(() => {
                const timerId = window.setTimeout(() => actions.enableDelayedSpinner(), 1000)
                return () => clearTimeout(timerId)
            }, 'spinnerTimeout')
            cache.disposables.add(() => {
                const timerId = window.setTimeout(() => actions.ignoreFeatureFlags(), 3000)
                return () => clearTimeout(timerId)
            }, 'featureFlagTimeout')
        },
    })),
    urlToAction(({ actions }) => ({
        '*': (_, __, hash) => {
            if ('kea' in hash) {
                actions.showDevTools()
            }
        },
    })),
])