: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { useActions, useValues } from 'kea'
import { useEffect, useRef, useState } from 'react'
import { LemonButton, LemonInput, LemonModal, Link } from '@posthog/lemon-ui'
import { LemonField } from 'lib/lemon-ui/LemonField'
import { eventUsageLogic } from 'lib/utils/eventUsageLogic'
import { projectLogic } from 'scenes/projectLogic'
import { organizationLogic } from '../organizationLogic'
import { isProjectNameTaken } from './isProjectNameTaken'
const MOCK_PRODUCT_NAMES = [
'Lemonify',
'Pineapplify',
'Bananify',
'Mangofy',
'Peachify',
'Plumify',
'Cherryfy',
'Raspberryfy',
]
export function CreateProjectModal({
isVisible,
onClose,
inline = false,
}: {
isVisible: boolean
onClose?: () => void
inline?: boolean
}): JSX.Element {
const { currentProject, currentProjectLoading } = useValues(projectLogic)
const { createProject } = useActions(projectLogic)
const { currentOrganization, projectCreationForbiddenReason } = useValues(organizationLogic)
const { reportProjectCreationSubmitted } = useActions(eventUsageLogic)
const [name, setName] = useState<string>('')
const isNameTaken = isProjectNameTaken(name, currentOrganization?.projects)
const closeModal: () => void = () => {
if (onClose) {
onClose()
if (name) {
setName('')
}
}
}
const handleSubmit = (): void => {
// Also guards Enter-key submission, which bypasses the button's disabledReason
if (!name || isNameTaken || currentProjectLoading || projectCreationForbiddenReason) {
return
}
createProject({ name })
reportProjectCreationSubmitted(
currentOrganization?.projects ? currentOrganization.projects.length : 0,
name.length
)
}
// 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(() => {
if (currentProject && currentProject.id !== previousProjectId.current) {
previousProjectId.current = currentProject.id
closeModal()
}
}, [currentProject]) // oxlint-disable-line react-hooks/exhaustive-deps
return (
<LemonModal
width={560}
title={currentOrganization ? `Create a project within ${currentOrganization.name}` : 'Create a project'}
description={
<>
<p>
Use projects to isolate products that share nothing at all. Both data and setup (such as
dashboards or taxonomy) is separate between projects.
</p>
<p>
<strong>Tip:</strong> We recommend using the same project for both your website and app to track
conversion fully.{' '}
<Link to="https://posthog.com/docs/settings/projects" target="_blank">
Learn more in PostHog docs.
</Link>
</p>
{currentOrganization?.projects?.some(
(project) => project.name.toLowerCase() === 'default project'
) && (
<p>
<strong>Bonus tip:</strong> You can always rename your "Default project".
</p>
)}
</>
}
footer={
<>
{onClose && (
<LemonButton
type="secondary"
onClick={onClose}
disabledReason={currentProjectLoading ? 'Creating project...' : undefined}
>
Cancel
</LemonButton>
)}
<LemonButton
type="primary"
onClick={handleSubmit}
loading={currentProjectLoading}
disabledReason={
projectCreationForbiddenReason
? projectCreationForbiddenReason
: !name
? 'Think of a name!'
: isNameTaken
? 'There is already a project with this name in this organization. Choose a different name.'
: null
}
>
Create project
</LemonButton>
</>
}
isOpen={isVisible}
onClose={onClose}
inline={inline}
closable={!currentProjectLoading}
>
<LemonField.Pure label="Project name">
<LemonInput
placeholder={`E.g. ${MOCK_PRODUCT_NAMES[Math.floor(Math.random() * MOCK_PRODUCT_NAMES.length)]}`}
maxLength={64}
autoFocus
value={name}
onChange={(value) => setName(value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleSubmit()
}
}}
disabled={currentProjectLoading}
/>
</LemonField.Pure>
</LemonModal>
)
}