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

import { newAccountMenuLogic } from 'lib/components/Account/newAccountMenuLogic'
import { upgradeModalLogic } from 'lib/components/UpgradeModal/upgradeModalLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'
import { getProjectSwitchTargetUrl } from 'lib/utils/kea-router'
import { organizationLogic } from 'scenes/organizationLogic'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { globalModalsLogic } from '~/layout/globalModalsLogic'
import { AvailableFeature } from '~/types'

import type { GuardAvailableFeatureFn } from '../../lib/components/UpgradeModal/upgradeModalLogic'
import type { PreflightStatus, TeamPublicType, TeamType } from '../../types'

export type ProjectMenuActionKey = 'project-settings' | 'new-project'

export interface ProjectMenuAction {
    key: ProjectMenuActionKey
    disabledReason: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayProjectMenuLogicValues {
    currentOrganizationProjectCount: number // organizationLogic
    projectCreationForbiddenReason: string | null // organizationLogic
    preflight: PreflightStatus | null // preflightLogic
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    guardAvailableFeature: GuardAvailableFeatureFn // upgradeModalLogic
    projectMenuActions: ProjectMenuAction[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayProjectMenuLogicActions {
    showCreateProjectModal: () => {
        value: true
    } // globalModalsLogic
    closeProjectSwitcher: () => {
        value: true
    } // newAccountMenuLogic
    runProjectMenuAction: (key: ProjectMenuActionKey) => {
        key: ProjectMenuActionKey
    }
    switchProject: (
        teamId: number,
        projectId: number
    ) => {
        projectId: number
        teamId: number
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayProjectMenuLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        projectMenuActions: (
            currentTeam: TeamPublicType | TeamType | null,
            preflight: PreflightStatus | null,
            projectCreationForbiddenReason: string | null
        ) => ProjectMenuAction[]
    }
}

export type todayProjectMenuLogicType = MakeLogicType<
    todayProjectMenuLogicValues,
    todayProjectMenuLogicActions,
    Record<string, any>,
    todayProjectMenuLogicMeta
>

/** The project switcher: switching projects, and the actions at the end of the list. */
export const todayProjectMenuLogic = kea<todayProjectMenuLogicType>([
    path(['layout', 'today', 'todayProjectMenuLogic']),
    connect(() => ({
        values: [
            teamLogic,
            ['currentTeam'],
            organizationLogic,
            ['currentOrganizationProjectCount', 'projectCreationForbiddenReason'],
            preflightLogic,
            ['preflight'],
            upgradeModalLogic,
            ['guardAvailableFeature'],
        ],
        actions: [newAccountMenuLogic, ['closeProjectSwitcher'], globalModalsLogic, ['showCreateProjectModal']],
    })),
    actions({
        runProjectMenuAction: (key: ProjectMenuActionKey) => ({ key }),
        switchProject: (teamId: number, projectId: number) => ({ teamId, projectId }),
    }),
    selectors({
        projectMenuActions: [
            (s) => [s.currentTeam, s.preflight, s.projectCreationForbiddenReason],
            (
                currentTeam: TeamPublicType | TeamType | null,
                preflight: PreflightStatus | null,
                projectCreationForbiddenReason: string | null
            ): ProjectMenuAction[] => [
                ...(currentTeam ? [{ key: 'project-settings' as const, disabledReason: null }] : []),
                ...(preflight?.can_create_org
                    ? [
                          {
                              key: 'new-project' as const,
                              disabledReason: projectCreationForbiddenReason || null,
                          },
                      ]
                    : []),
            ],
        ],
    }),
    listeners(({ actions, values }) => ({
        switchProject: ({ teamId, projectId }) => {
            if (teamId === values.currentTeam?.id) {
                return
            }
            actions.closeProjectSwitcher()
            window.location.href = getProjectSwitchTargetUrl(
                location.pathname,
                teamId,
                values.currentTeam?.project_id,
                projectId
            )
        },
        runProjectMenuAction: ({ key }) => {
            if (key === 'project-settings' && values.currentTeam) {
                actions.closeProjectSwitcher()
                router.actions.push(urls.project(values.currentTeam.id, urls.settings('project')))
            } else if (key === 'new-project') {
                values.guardAvailableFeature(
                    AvailableFeature.ORGANIZATIONS_PROJECTS,
                    () => {
                        actions.closeProjectSwitcher()
                        actions.showCreateProjectModal()
                    },
                    { currentUsage: values.currentOrganizationProjectCount }
                )
            }
        },
    })),
])