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): handle permission errors in project creation flow (#63235)

posthog[bot]ab38fa3bc897
diff --git a/frontend/src/lib/components/Account/ProjectCombobox.tsx b/frontend/src/lib/components/Account/ProjectCombobox.tsx
index 5083464c4d18..e7752ecc6252 100644
--- a/frontend/src/lib/components/Account/ProjectCombobox.tsx
+++ b/frontend/src/lib/components/Account/ProjectCombobox.tsx
@@ -174,11 +174,16 @@ export function ProjectCombobox(): JSX.Element | null {
{preflight?.can_create_org && (
<Combobox.Item
asChild
- onClick={() =>
+ onClick={() => {
+ // The button below is rendered disabled when creation is forbidden, but the
+ // Combobox still fires onClick/Enter — enforce the disabled state here too.
+ if (projectCreationForbiddenReason) {
+ return
+ }
guardAvailableFeature(AvailableFeature.ORGANIZATIONS_PROJECTS, showCreateProjectModal, {
currentUsage: currentOrganization?.teams?.length,
})
- }
+ }}
>
<ButtonPrimitive
menuItem
diff --git a/frontend/src/lib/components/Account/ProjectSwitcher.tsx b/frontend/src/lib/components/Account/ProjectSwitcher.tsx
index a258a719ef99..7cdf1f854695 100644
--- a/frontend/src/lib/components/Account/ProjectSwitcher.tsx
+++ b/frontend/src/lib/components/Account/ProjectSwitcher.tsx
@@ -116,9 +116,16 @@ export function ProjectSwitcher({ dialog = true }: { dialog?: boolean }): JSX.El
.sort((a, b) => a.invite.organization_name.localeCompare(b.invite.organization_name))
const createItem = filteredItems.find((p): p is CreateProjectItem => p.type === 'create')
+ const canCreateProject = preflight?.can_create_org !== false && !projectCreationForbiddenReason
+
const handleItemClick = useCallback(
(item: ListItem) => {
if (item.type === 'create') {
+ // The create row is rendered disabled when the user can't create projects, but the
+ // Combobox still fires onClick/Enter — enforce the disabled state here too.
+ if (!canCreateProject) {
+ return
+ }
guardAvailableFeature(
AvailableFeature.ORGANIZATIONS_PROJECTS,
() => {
@@ -151,6 +158,7 @@ export function ProjectSwitcher({ dialog = true }: { dialog?: boolean }): JSX.El
showCreateProjectModal,
currentOrganization?.teams?.length,
setAccountMenuOpen,
+ canCreateProject,
]
)
@@ -167,8 +175,6 @@ export function ProjectSwitcher({ dialog = true }: { dialog?: boolean }): JSX.El
return item.team.name
}, [])
- const canCreateProject = preflight?.can_create_org !== false && !projectCreationForbiddenReason
-
if (!isAuthenticatedTeam(currentTeam)) {
return null
}
diff --git a/frontend/src/scenes/project/Create/index.tsx b/frontend/src/scenes/project/Create/index.tsx
index cbb06b3a726f..517648d57c23 100644
--- a/frontend/src/scenes/project/Create/index.tsx
+++ b/frontend/src/scenes/project/Create/index.tsx
@@ -1,9 +1,11 @@
import { useValues } from 'kea'
+import { router } from 'kea-router'
import { LemonBanner } from 'lib/lemon-ui/LemonBanner'
import { organizationLogic } from 'scenes/organizationLogic'
import { SceneExport } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
+import { urls } from 'scenes/urls'
import { CreateProjectModal } from '../CreateProjectModal'
@@ -20,6 +22,7 @@ export function ProjectCreate(): JSX.Element {
{projectCreationForbiddenReason}
</LemonBanner>
) : (
- <CreateProjectModal isVisible inline />
+ // Give the inline scene a working exit (Cancel + close) so a failed create doesn't trap the user.
+ <CreateProjectModal isVisible inline onClose={() => router.actions.push(urls.projectHomepage())} />
)
}
diff --git a/frontend/src/scenes/project/CreateProjectModal.tsx b/frontend/src/scenes/project/CreateProjectModal.tsx
index 718b464a9ceb..2a943bfd53e7 100644
--- a/frontend/src/scenes/project/CreateProjectModal.tsx
+++ b/frontend/src/scenes/project/CreateProjectModal.tsx
@@ -1,5 +1,5 @@
import { useActions, useValues } from 'kea'
-import { useEffect, useState } from 'react'
+import { useEffect, useRef, useState } from 'react'
import { LemonButton, LemonInput, LemonModal, Link } from '@posthog/lemon-ui'
@@ -51,9 +51,15 @@ export function CreateProjectModal({
)
}
- // Anytime the project changes close the modal as it indicates we have created a new project
+ // Close the modal once a *new* project is created (the current project id changes). Guarding on the
+ // id rather than the object reference avoids closing on mount and avoids reacting to a failed create
+ // that left the existing project in place.
+ const previousProjectId = useRef(currentProject?.id)
useEffect(() => {
- closeModal()
+ if (currentProject && currentProject.id !== previousProjectId.current) {
+ previousProjectId.current = currentProject.id
+ closeModal()
+ }
}, [currentProject]) // oxlint-disable-line react-hooks/exhaustive-deps
return (
diff --git a/frontend/src/scenes/projectLogic.ts b/frontend/src/scenes/projectLogic.ts
index 7c67cc8f8fff..0655e5206461 100644
--- a/frontend/src/scenes/projectLogic.ts
+++ b/frontend/src/scenes/projectLogic.ts
@@ -93,12 +93,10 @@ export const projectLogic = kea<projectLogicType>([
return patchedProject
},
createProject: async ({ name }: { name: string }) => {
- try {
- return await api.create('api/projects/', { name })
- } catch {
- lemonToast.error('Failed to create project')
- return values.currentProject
- }
+ // 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.
+ return await api.create('api/projects/', { name })
},
},
],