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.

fix(projects): count projects for the paywall and bound project creation (#115640)

Rafael Audibert056b22ca3929
diff --git a/frontend/src/lib/components/Account/NewAccountMenu.tsx b/frontend/src/lib/components/Account/NewAccountMenu.tsx
index 6a24df012a15..001533817f5e 100644
--- a/frontend/src/lib/components/Account/NewAccountMenu.tsx
+++ b/frontend/src/lib/components/Account/NewAccountMenu.tsx
@@ -73,7 +73,7 @@ export function NewAccountMenu({
const { pendingInvites } = useValues(pendingInvitesLogic)
const hasPendingInvites = pendingInvites.length > 0
const { preflight } = useValues(preflightLogic)
- const { currentOrganization } = useValues(organizationLogic)
+ const { currentOrganization, currentOrganizationProjectCount } = useValues(organizationLogic)
const { billingEntryUrl } = useValues(billingLogic)
const { guardAvailableFeature } = useValues(upgradeModalLogic)
const { showCreateProjectModal } = useActions(globalModalsLogic)
@@ -169,7 +169,7 @@ export function NewAccountMenu({
setAccountMenuOpen(false)
showCreateProjectModal()
},
- { currentUsage: currentOrganization?.teams?.length }
+ { currentUsage: currentOrganizationProjectCount }
)
}}
>
diff --git a/frontend/src/lib/components/Account/ProjectCombobox.tsx b/frontend/src/lib/components/Account/ProjectCombobox.tsx
index bb3ffa9abec3..4fba144f084b 100644
--- a/frontend/src/lib/components/Account/ProjectCombobox.tsx
+++ b/frontend/src/lib/components/Account/ProjectCombobox.tsx
@@ -26,7 +26,8 @@ export function ProjectCombobox(): JSX.Element | null {
const { guardAvailableFeature } = useValues(upgradeModalLogic)
const { showCreateProjectModal } = useActions(globalModalsLogic)
const { currentTeam } = useValues(teamLogic)
- const { currentOrganization, projectCreationForbiddenReason } = useValues(organizationLogic)
+ const { currentOrganization, currentOrganizationProjectCount, projectCreationForbiddenReason } =
+ useValues(organizationLogic)
const { pendingInvites } = useValues(pendingInvitesLogic)
if (!isAuthenticatedTeam(currentTeam)) {
@@ -181,7 +182,7 @@ export function ProjectCombobox(): JSX.Element | null {
return
}
guardAvailableFeature(AvailableFeature.ORGANIZATIONS_PROJECTS, showCreateProjectModal, {
- currentUsage: currentOrganization?.teams?.length,
+ currentUsage: currentOrganizationProjectCount,
})
}}
>
diff --git a/frontend/src/lib/components/Account/ProjectSwitcher.tsx b/frontend/src/lib/components/Account/ProjectSwitcher.tsx
index 511167664cfb..56eb0a9fef29 100644
--- a/frontend/src/lib/components/Account/ProjectSwitcher.tsx
+++ b/frontend/src/lib/components/Account/ProjectSwitcher.tsx
@@ -29,7 +29,7 @@ export function ProjectSwitcher({ dialog = true }: { dialog?: boolean }): JSX.El
const { guardAvailableFeature } = useValues(upgradeModalLogic)
const { showCreateProjectModal } = useActions(globalModalsLogic)
const { currentTeam } = useValues(teamLogic)
- const { currentOrganization, projectCreationForbiddenReason } = useValues(organizationLogic)
+ const { currentOrganizationProjectCount, projectCreationForbiddenReason } = useValues(organizationLogic)
const { closeProjectSwitcher, setAccountMenuOpen, setProjectSearchValue } = useActions(newAccountMenuLogic)
const {
projectSearchValue: searchValue,
@@ -57,7 +57,7 @@ export function ProjectSwitcher({ dialog = true }: { dialog?: boolean }): JSX.El
setAccountMenuOpen(false)
},
{
- currentUsage: currentOrganization?.teams?.length,
+ currentUsage: currentOrganizationProjectCount,
}
)
closeProjectSwitcher()
@@ -77,7 +77,7 @@ export function ProjectSwitcher({ dialog = true }: { dialog?: boolean }): JSX.El
closeProjectSwitcher,
guardAvailableFeature,
showCreateProjectModal,
- currentOrganization?.teams?.length,
+ currentOrganizationProjectCount,
setAccountMenuOpen,
canCreateProject,
]
diff --git a/frontend/src/scenes/oauth/OAuthAuthorize.tsx b/frontend/src/scenes/oauth/OAuthAuthorize.tsx
index 03bbd0c69c4d..4238f2ac6a32 100644
--- a/frontend/src/scenes/oauth/OAuthAuthorize.tsx
+++ b/frontend/src/scenes/oauth/OAuthAuthorize.tsx
@@ -172,11 +172,18 @@ export const OAuthAuthorize = (): JSX.Element => {
const { isReadOnly: isImpersonationReadOnly, isImpersonated } = useValues(impersonationNoticeLogic)
const { guardAvailableFeature } = useValues(upgradeModalLogic)
- const { currentOrganization, projectCreationForbiddenReason } = useValues(organizationLogic)
+ const { currentOrganization, currentOrganizationProjectCount, projectCreationForbiddenReason } =
+ useValues(organizationLogic)
+ // Plan and role data is only loaded for the current organization, so the backend decides for any other one
+ const isCurrentOrganizationSelected = selectedOrganization === currentOrganization?.id
const handleShowCreateProject = (): void => {
+ if (!isCurrentOrganizationSelected) {
+ setShowCreateProject(true)
+ return
+ }
guardAvailableFeature(AvailableFeature.ORGANIZATIONS_PROJECTS, () => setShowCreateProject(true), {
- currentUsage: currentOrganization?.teams?.length,
+ currentUsage: currentOrganizationProjectCount,
})
}
@@ -388,7 +395,9 @@ export const OAuthAuthorize = (): JSX.Element => {
disabledReason={
!selectedOrganization
? 'Select an organization first'
- : (projectCreationForbiddenReason ?? undefined)
+ : isCurrentOrganizationSelected
+ ? (projectCreationForbiddenReason ?? undefined)
+ : undefined
}
onClick={handleShowCreateProject}
/>
diff --git a/frontend/src/scenes/organizationLogic.test.ts b/frontend/src/scenes/organizationLogic.test.ts
index d133a1a203a3..6de320e1578d 100644
--- a/frontend/src/scenes/organizationLogic.test.ts
+++ b/frontend/src/scenes/organizationLogic.test.ts
@@ -44,6 +44,26 @@ describe('organizationLogic', () => {
})
})
+ describe('currentOrganizationProjectCount', () => {
+ test.each([
+ ['no teams', [], 0],
+ ['one environment per project', [{ project_id: 1 }, { project_id: 2 }], 2],
+ ['several environments in one project', [{ project_id: 1 }, { project_id: 1 }, { project_id: 2 }], 2],
+ ['a demo project', [{ project_id: 1 }, { project_id: 3, is_demo: true }], 1],
+ ])('counts distinct non-demo projects with %s', async (_, teams, expected) => {
+ const organization = {
+ ...MOCK_DEFAULT_ORGANIZATION,
+ teams: teams.map((team, index) => ({ id: index + 1, is_demo: false, ...team })),
+ }
+ window.POSTHOG_APP_CONTEXT = { current_user: { organization } } as unknown as AppContext
+ initKeaTests()
+ logic = organizationLogic()
+ logic.mount()
+ await expectLogic(logic).toDispatchActions(['loadCurrentOrganizationSuccess'])
+ expect(logic.values.currentOrganizationProjectCount).toBe(expected)
+ })
+ })
+
describe('if POSTHOG_APP_CONTEXT is undefined', () => {
// Should not happen in production, but the app should still not break.
// We use initKeaTests(false) to set up the kea environment, then reset the context to undefined
diff --git a/frontend/src/scenes/organizationLogic.tsx b/frontend/src/scenes/organizationLogic.tsx
index e4b3d9964ba2..d0acba84d0ad 100644
--- a/frontend/src/scenes/organizationLogic.tsx
+++ b/frontend/src/scenes/organizationLogic.tsx
@@ -79,6 +79,7 @@ export interface organizationLogicValues {
currentOrganizationBlockPage: string | null
currentOrganizationId: string
currentOrganizationLoading: boolean
+ currentOrganizationProjectCount: number
isAdminOrOwner: boolean | null
isCurrentOrganizationNew: boolean
isCurrentOrganizationUnavailable: boolean
@@ -190,6 +191,7 @@ export interface organizationLogicMeta {
currentOrganization: OrganizationType | null,
currentOrganizationLoading: boolean
) => boolean
+ currentOrganizationProjectCount: (currentOrganization: OrganizationType | null) => number
projectCreationForbiddenReason: (
currentOrganization: OrganizationType | null,
hasAvailableFeature: (feature: AvailableFeature, currentUsage?: number | undefined) => boolean
@@ -318,6 +320,14 @@ export const organizationLogic = kea<organizationLogicType>([
(currentOrganization: OrganizationType | null, currentOrganizationLoading: boolean): boolean =>
!currentOrganization?.membership_level && !currentOrganizationLoading,
],
+ // Mirrors PremiumMultiProjectPermission, which counts distinct non-demo projects, not environments
+ currentOrganizationProjectCount: [
+ (s) => [s.currentOrganization],
+ (currentOrganization: OrganizationType | null): number =>
+ new Set(
+ (currentOrganization?.teams ?? []).filter((team) => !team.is_demo).map((team) => team.project_id)
+ ).size,
+ ],
projectCreationForbiddenReason: [
(s) => [s.currentOrganization, s.hasAvailableFeature],
(
diff --git a/frontend/src/scenes/projectLogic.ts b/frontend/src/scenes/projectLogic.ts
index c18e8dacaf71..f52f1cfb1488 100644
--- a/frontend/src/scenes/projectLogic.ts
+++ b/frontend/src/scenes/projectLogic.ts
@@ -18,6 +18,8 @@ import { organizationLogic } from './organizationLogic'
import { urls } from './urls'
import { userLogic } from './userLogic'
+const CREATE_PROJECT_TIMEOUT_MS = 60_000
+
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectLogicValues {
currentOrganizationId: string // organizationLogic
@@ -267,8 +269,19 @@ export const projectLogic = kea<projectLogicType>([
// Let failures (e.g. a 403 for non-admins) propagate: kea-loaders surfaces the API
// error toast and clears the loading state, and createProjectSuccess never fires — so we
// don't switch into a project that wasn't created or leave the modal stuck open.
- // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
- return await api.create('api/projects/', { name })
+ const signal = AbortSignal.timeout(CREATE_PROJECT_TIMEOUT_MS)
+ try {
+ // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
+ return await api.create('api/projects/', { name }, { signal })
+ } catch (error) {
+ if (signal.aborted) {
+ actions.loadCurrentOrganization()
+ lemonToast.error(
+ 'Creating the project is taking longer than expected. Refresh the page to see if it was created before you try again.'
+ )
+ }
+ throw error
+ }
},
cancelProjectDeletion: async () => {
if (!values.currentProject) {
@@ -357,7 +370,7 @@ export const projectLogic = kea<projectLogicType>([
},
createProjectSuccess: ({ currentProject }) => {
if (currentProject) {
- actions.switchTeam(currentProject.id, urls.projectHomepage())
+ actions.switchTeam(currentProject.id)
}
},