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.

5.4 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import posthog from 'posthog-js'

import { lemonToast } from '@posthog/lemon-ui'

import { mcpHintLogic } from 'lib/components/MCPHint/mcpHintLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
// eslint-disable-next-line import/no-cycle
import { userLogic } from 'scenes/userLogic'

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

export type FakeStatusOverride = 'none' | 'operational' | 'degraded_performance' | 'partial_outage' | 'major_outage'
export type FakeBillingAlert = 'none' | 'usage_limit_reached' | 'usage_limit_approaching' | 'trial'

// The PostHog Team cohort in PostHog's own project excludes people with this person property set to true.
export const EXCLUDED_FROM_POSTHOG_TEAM_COHORT_PROPERTY = 'excluded_from_posthog_team_cohort'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface superpowersLogicValues {
    dismissedSurfaces: Record<string, true> // mcpHintLogic
    effectiveOptOut: boolean // mcpHintLogic
    preflight: PreflightStatus | null // preflightLogic
    user: UserType | null // userLogic
    excludedFromPostHogTeamCohort: boolean
    fakeBillingAlert: FakeBillingAlert
    fakeStatusOverride: FakeStatusOverride
    isSuperpowersOpen: boolean
    mcpHintsDismissed: boolean
    superpowersEnabled: boolean | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface superpowersLogicActions {
    reenableMCPHints: () => {
        value: true
    } // mcpHintLogic
    closeSuperpowers: () => {
        value: true
    }
    openSuperpowers: () => {
        value: true
    }
    setExcludedFromPostHogTeamCohort: (excluded: boolean) => {
        excluded: boolean
    }
    setFakeBillingAlert: (alert: FakeBillingAlert) => {
        alert: FakeBillingAlert
    }
    setFakeStatusOverride: (status: FakeStatusOverride) => {
        status: FakeStatusOverride
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface superpowersLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        superpowersEnabled: (user: UserType | null, preflight: PreflightStatus | null) => boolean | undefined
        mcpHintsDismissed: (dismissedSurfaces: Record<string, true>, effectiveOptOut: boolean) => boolean
    }
}

export type superpowersLogicType = MakeLogicType<
    superpowersLogicValues,
    superpowersLogicActions,
    Record<string, any>,
    superpowersLogicMeta
>

export const superpowersLogic = kea<superpowersLogicType>([
    path(['lib', 'components', 'Superpowers', 'superpowersLogic']),
    connect(() => ({
        values: [
            userLogic,
            ['user'],
            preflightLogic,
            ['preflight'],
            mcpHintLogic,
            ['dismissedSurfaces', 'effectiveOptOut'],
        ],
        actions: [mcpHintLogic, ['reenable as reenableMCPHints']],
    })),
    actions({
        openSuperpowers: true,
        closeSuperpowers: true,
        setFakeStatusOverride: (status: FakeStatusOverride) => ({ status }),
        setFakeBillingAlert: (alert: FakeBillingAlert) => ({ alert }),
        setExcludedFromPostHogTeamCohort: (excluded: boolean) => ({ excluded }),
    }),
    reducers({
        isSuperpowersOpen: [
            false,
            {
                openSuperpowers: () => true,
                closeSuperpowers: () => false,
            },
        ],
        fakeStatusOverride: [
            'none' as FakeStatusOverride,
            {
                setFakeStatusOverride: (_, { status }) => status,
            },
        ],
        fakeBillingAlert: [
            'none' as FakeBillingAlert,
            {
                setFakeBillingAlert: (_, { alert }) => alert,
            },
        ],
        excludedFromPostHogTeamCohort: [
            false,
            { persist: true },
            {
                setExcludedFromPostHogTeamCohort: (_, { excluded }) => excluded,
            },
        ],
    }),
    listeners({
        setExcludedFromPostHogTeamCohort: ({ excluded }) => {
            // Set false instead of unsetting. unsetPersonProperties also drops the property from the
            // overrides posthog-js sends to /flags, so the flags reload reads the stored person, which
            // still has true until ingestion processes the $unset.
            posthog.setPersonProperties({ [EXCLUDED_FROM_POSTHOG_TEAM_COHORT_PROPERTY]: excluded })
            lemonToast.success(
                excluded
                    ? 'You left the PostHog Team cohort. Flags that target it are now off for you.'
                    : 'You rejoined the PostHog Team cohort. Flags that target it are back on for you.'
            )
        },
    }),
    selectors({
        superpowersEnabled: [
            (s) => [s.user, s.preflight],
            (
                user: null | import('../../../types').UserType,
                preflight: null | import('../../../types').PreflightStatus
            ) => {
                return user?.is_staff || preflight?.is_debug || preflight?.instance_preferences?.debug_queries
            },
        ],
        mcpHintsDismissed: [
            (s) => [s.dismissedSurfaces, s.effectiveOptOut],
            (dismissedSurfaces: Record<string, true>, effectiveOptOut: boolean): boolean =>
                effectiveOptOut || Object.keys(dismissedSurfaces).length > 0,
        ],
    }),
])