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.2 KB
Raw Blame
import { MakeLogicType, connect, kea, path, selectors } from 'kea'

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

import { AvailableFeature } from '~/types'

import type { PreflightStatus, TeamPublicType, TeamType } from '../../types'

export enum GroupsAccessStatus {
    AlreadyUsing,
    HasAccess,
    HasGroupTypes,
    NoAccess,
    Hidden,
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface groupsAccessLogicValues {
    preflight: PreflightStatus | null // preflightLogic
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean // userLogic
    canStartUsingGroups: boolean
    groupsAccessStatus: GroupsAccessStatus
    groupsEnabled: boolean
    needsUpgradeForGroups: boolean
    shouldShowGroupsIntroduction: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface groupsAccessLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        groupsEnabled: (
            hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean
        ) => boolean
        groupsAccessStatus: (
            groupsEnabled: boolean,
            currentTeam: TeamPublicType | TeamType | null,
            preflight: PreflightStatus | null
        ) => GroupsAccessStatus
        needsUpgradeForGroups: (groupsAccessStatus: GroupsAccessStatus) => boolean
        canStartUsingGroups: (groupsAccessStatus: GroupsAccessStatus) => boolean
        shouldShowGroupsIntroduction: (groupsAccessStatus: GroupsAccessStatus) => boolean
    }
}

export type groupsAccessLogicType = MakeLogicType<
    groupsAccessLogicValues,
    {},
    Record<string, any>,
    groupsAccessLogicMeta
>

export const groupsAccessLogic = kea<groupsAccessLogicType>([
    path(['lib', 'introductions', 'groupsAccessLogic']),
    connect(() => ({
        values: [teamLogic, ['currentTeam'], preflightLogic, ['preflight'], userLogic, ['hasAvailableFeature']],
    })),
    selectors({
        groupsEnabled: [
            (s) => [s.hasAvailableFeature],
            (hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean) =>
                hasAvailableFeature(AvailableFeature.GROUP_ANALYTICS),
        ],
        // Used to toggle various introduction views related to groups
        groupsAccessStatus: [
            (s) => [s.groupsEnabled, s.currentTeam, s.preflight],
            (
                groupsEnabled: boolean,
                currentTeam: null | import('~/types').TeamPublicType | import('~/types').TeamType,
                preflight: null | import('~/types').PreflightStatus
            ): GroupsAccessStatus => {
                const hasGroups = currentTeam?.has_group_types
                if (preflight?.instance_preferences?.disable_paid_fs) {
                    return GroupsAccessStatus.Hidden
                } else if (groupsEnabled && hasGroups) {
                    return GroupsAccessStatus.AlreadyUsing
                } else if (groupsEnabled) {
                    return GroupsAccessStatus.HasAccess
                } else if (hasGroups) {
                    return GroupsAccessStatus.HasGroupTypes
                }
                return GroupsAccessStatus.NoAccess
            },
        ],
        needsUpgradeForGroups: [
            (s) => [s.groupsAccessStatus],
            (groupsAccessStatus: GroupsAccessStatus) =>
                [GroupsAccessStatus.NoAccess, GroupsAccessStatus.HasGroupTypes].includes(groupsAccessStatus),
        ],
        canStartUsingGroups: [
            (s) => [s.groupsAccessStatus],
            (groupsAccessStatus: GroupsAccessStatus) => groupsAccessStatus === GroupsAccessStatus.HasAccess,
        ],
        shouldShowGroupsIntroduction: [
            (s) => [s.groupsAccessStatus],
            (groupsAccessStatus: GroupsAccessStatus) =>
                [GroupsAccessStatus.HasAccess, GroupsAccessStatus.HasGroupTypes, GroupsAccessStatus.NoAccess].includes(
                    groupsAccessStatus
                ),
        ],
    }),
])