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

import api from 'lib/api'
import { getAppContext } from 'lib/utils/getAppContext'

import { UserType } from '~/types'

import type { OrganizationType } from '../types'
import { organizationLogic } from './organizationLogic'
import { projectLogic } from './projectLogic'
import { teamLogic } from './teamLogic'
import { userLogic } from './userLogic'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface appContextLogicActions {
    loadCurrentOrganizationSuccess: (
        currentOrganization: OrganizationType | null,
        payload?: any
    ) => {
        currentOrganization: OrganizationType | null
        payload?: any
    } // organizationLogic
    loadCurrentProject: () => any // projectLogic
    loadCurrentTeam: () => any // teamLogic
    loadUserSuccess: (
        user: UserType | null,
        payload?:
            | {
                  resetOnFailure: boolean | undefined
              }
            | undefined
    ) => {
        payload?: {
            resetOnFailure: boolean | undefined
        }
        user: UserType | null
    } // userLogic
}

export type appContextLogicType = MakeLogicType<{}, appContextLogicActions>

export const appContextLogic = kea<appContextLogicType>([
    path(['scenes', 'appContextLogic']),
    connect(() => ({
        actions: [
            userLogic,
            ['loadUserSuccess'],
            organizationLogic,
            ['loadCurrentOrganizationSuccess'],
            teamLogic,
            ['loadCurrentTeam'],
            projectLogic,
            ['loadCurrentProject'],
        ],
    })),
    afterMount(({ actions }) => {
        const appContext = getAppContext()
        const preloadedUser = appContext?.current_user

        if (appContext && preloadedUser) {
            // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. Use usersRetrieve() from '~/generated/core/api' instead.
            void api.get('api/users/@me/').then((remoteUser: UserType) => {
                if (remoteUser.uuid !== preloadedUser.uuid) {
                    console.error(`Preloaded user ${preloadedUser.uuid} does not match remote user ${remoteUser.uuid}`)
                    posthog.captureException(
                        new Error(`Preloaded user ${preloadedUser.uuid} does not match remote user ${remoteUser.uuid}`),
                        {
                            posthog_app_context: JSON.stringify(getAppContext()),
                            remote_user: JSON.stringify(remoteUser),
                        }
                    )

                    // NOTE: This doesn't fix the issue but removes the confusion of seeing incorrect user info in the UI
                    actions.loadUserSuccess(remoteUser)
                    actions.loadCurrentOrganizationSuccess(remoteUser.organization)
                    actions.loadCurrentProject()
                    actions.loadCurrentTeam()
                }
            })
        }
    }),
])