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.

7.3 KB
Raw Blame
import { MakeLogicType, connect, kea, path, selectors } from 'kea'

import { FEATURE_FLAGS } from 'lib/constants'
import { GroupsAccessStatus } from 'lib/introductions/groupsAccessLogic'
import { LemonTab } from 'lib/lemon-ui/LemonTabs'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { sceneConfigurations } from 'scenes/scenes'
import { Scene } from 'scenes/sceneTypes'
import { urls } from 'scenes/urls'

import { iconForType } from '~/layout/panel-layout/ProjectTree/defaultTree'
import { groupsModel } from '~/models/groupsModel'
import { groupTabs } from '~/models/groupTabs'
import { FileSystemIconType } from '~/queries/schema/schema-general'
import { ActivityTab } from '~/types'

import type { FeatureFlagsSet } from '../../lib/logic/featureFlagLogic'
import type { Noun } from '../../models/groupsModel'
import type { GroupType, GroupTypeIndex } from '../../types'

export type ActivitySceneTabKey = ActivityTab | 'persons' | 'cohorts' | `groups-${number}`

export interface ActivitySceneTab {
    key: ActivitySceneTabKey
    label: string
    url: string
    iconType: FileSystemIconType | undefined
    tooltipDocLink?: string
    // pinned: autocapture dashboards and Playwright select these tabs by data-attr
    dataAttr?: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface activitySceneTabsLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    aggregationLabel: (groupTypeIndex: number | null | undefined, deferToUserWording?: boolean) => Noun // groupsModel
    groupTypes: Map<GroupTypeIndex, GroupType> // groupsModel
    groupsAccessStatus: GroupsAccessStatus // groupsModel
    activityTabs: ActivitySceneTab[]
    groupTabs: ActivitySceneTab[]
    peopleTabs: ActivitySceneTab[]
    sceneTabs: ActivitySceneTab[]
    tabsForKey: (activeKey: ActivitySceneTabKey) => LemonTab<ActivitySceneTabKey>[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface activitySceneTabsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        groupTabs: (
            groupTypes: Map<GroupTypeIndex, GroupType>,
            groupsAccessStatus: GroupsAccessStatus,
            aggregationLabel: (groupTypeIndex: number | null | undefined, deferToUserWording?: boolean) => Noun // groupsModel
        ) => ActivitySceneTab[]
        sceneTabs: (groupTabs: ActivitySceneTab[], featureFlags: FeatureFlagsSet) => ActivitySceneTab[]
        activityTabs: (sceneTabs: ActivitySceneTab[]) => ActivitySceneTab[]
        peopleTabs: (sceneTabs: ActivitySceneTab[]) => ActivitySceneTab[]
        tabsForKey: (
            activityTabs: ActivitySceneTab[],
            peopleTabs: ActivitySceneTab[]
        ) => (activeKey: ActivitySceneTabKey) => LemonTab<ActivitySceneTabKey>[]
    }
}

export type activitySceneTabsLogicType = MakeLogicType<
    activitySceneTabsLogicValues,
    {},
    Record<string, any>,
    activitySceneTabsLogicMeta
>

export function isActivityTabKey(key: ActivitySceneTabKey): boolean {
    return (Object.values(ActivityTab) as string[]).includes(key)
}

function toLemonTab(tab: ActivitySceneTab): LemonTab<ActivitySceneTabKey> {
    return {
        key: tab.key,
        label: (
            <span className="flex items-center gap-1" data-attr={tab.dataAttr}>
                {tab.iconType && iconForType(tab.iconType)}
                {tab.label}
            </span>
        ),
        tooltipDocLink: tab.tooltipDocLink,
        link: tab.url,
    }
}

export const activitySceneTabsLogic = kea<activitySceneTabsLogicType>([
    path(['scenes', 'activity', 'activitySceneTabsLogic']),
    connect(() => ({
        values: [
            groupsModel,
            ['aggregationLabel', 'groupTypes', 'groupsAccessStatus'],
            featureFlagLogic,
            ['featureFlags'],
        ],
    })),
    selectors({
        groupTabs: [
            (s) => [s.groupTypes, s.groupsAccessStatus, s.aggregationLabel],
            (
                groupTypes: Map<GroupTypeIndex, GroupType>,
                groupsAccessStatus: GroupsAccessStatus,
                aggregationLabel: (groupTypeIndex: number | null | undefined, deferToUserWording?: boolean) => Noun
            ): ActivitySceneTab[] => groupTabs(groupTypes, groupsAccessStatus, aggregationLabel),
        ],
        sceneTabs: [
            (s) => [s.groupTabs, s.featureFlags],
            (groupTabs: ActivitySceneTab[], featureFlags: FeatureFlagsSet): ActivitySceneTab[] => {
                const tabs: ActivitySceneTab[] = [
                    {
                        key: ActivityTab.ExploreEvents,
                        label: 'Events',
                        url: urls.activity(ActivityTab.ExploreEvents),
                        iconType: sceneConfigurations[Scene.ExploreEvents].iconType,
                    },
                    {
                        key: ActivityTab.LiveEvents,
                        label: 'Live',
                        url: urls.activity(ActivityTab.LiveEvents),
                        iconType: sceneConfigurations[Scene.LiveEvents].iconType,
                    },
                    {
                        key: ActivityTab.ExploreSessions,
                        label: 'Sessions',
                        url: urls.activity(ActivityTab.ExploreSessions),
                        iconType: sceneConfigurations[Scene.ExploreSessions].iconType,
                    },
                    {
                        key: 'persons',
                        label: 'Persons',
                        url: urls.persons(),
                        iconType: sceneConfigurations[Scene.Persons].iconType,
                        tooltipDocLink: 'https://posthog.com/docs/data/persons',
                        dataAttr: 'persons-management-persons-tab',
                    },
                    {
                        key: 'cohorts',
                        label: 'Cohorts',
                        url: urls.cohorts(),
                        iconType: sceneConfigurations[Scene.Cohorts].iconType,
                        tooltipDocLink: 'https://posthog.com/docs/data/cohorts',
                        dataAttr: 'persons-management-cohorts-tab',
                    },
                    ...groupTabs,
                ]

                return featureFlags[FEATURE_FLAGS.LIVESTREAM_HOGQL]
                    ? tabs.filter((tab) => tab.key !== ActivityTab.LiveEvents)
                    : tabs
            },
        ],
        activityTabs: [
            (s) => [s.sceneTabs],
            (sceneTabs: ActivitySceneTab[]): ActivitySceneTab[] => sceneTabs.filter((tab) => isActivityTabKey(tab.key)),
        ],
        peopleTabs: [
            (s) => [s.sceneTabs],
            (sceneTabs: ActivitySceneTab[]): ActivitySceneTab[] =>
                sceneTabs.filter((tab) => !isActivityTabKey(tab.key)),
        ],
        tabsForKey: [
            (s) => [s.activityTabs, s.peopleTabs],
            (
                activityTabs: ActivitySceneTab[],
                peopleTabs: ActivitySceneTab[]
            ): ((activeKey: ActivitySceneTabKey) => LemonTab<ActivitySceneTabKey>[]) => {
                const activityLemonTabs = activityTabs.map(toLemonTab)
                const peopleLemonTabs = peopleTabs.map(toLemonTab)
                return (activeKey) => (isActivityTabKey(activeKey) ? activityLemonTabs : peopleLemonTabs)
            },
        ],
    }),
])