: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 { MakeLogicType, actions, connect, kea, path, reducers, selectors } from 'kea'
import { organizationLogic } from 'scenes/organizationLogic'
import { teamLogic } from 'scenes/teamLogic'
import type { OrganizationType, TeamPublicType, TeamType } from '../../../types'
export interface ProjectListItem {
type: 'project'
id: number
team: OrganizationType['teams'][number]
isCurrent: boolean
}
export interface CreateProjectItem {
type: 'create'
id: 'create-new-project'
label: string
}
export type ListItem = ProjectListItem | CreateProjectItem
/** Whether a name matches what the user typed in an account switcher's search field. */
export function matchesSwitcherSearch(name: string, search: string): boolean {
return name.toLowerCase().includes(search.trim().toLowerCase())
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface newAccountMenuLogicValues {
currentOrganization: OrganizationType | null // organizationLogic
currentTeam: TeamPublicType | TeamType | null // teamLogic
filteredProjectItems: ListItem[]
hasProjectGroups: boolean
isAccountMenuOpen: boolean
isOrgSwitcherOpen: boolean
isProjectSwitcherOpen: boolean
projectGroups: {
name: string | null
projects: ProjectListItem[]
}[]
projectSearchValue: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface newAccountMenuLogicActions {
closeOrgSwitcher: () => {
value: true
}
closeProjectSwitcher: () => {
value: true
}
openOrgSwitcher: () => {
value: true
}
openProjectSwitcher: () => {
value: true
}
setAccountMenuOpen: (isOpen: boolean) => {
isOpen: boolean
}
setProjectSearchValue: (searchValue: string) => {
searchValue: string
}
toggleAccountMenu: () => {
value: true
}
toggleOrgSwitcher: () => {
value: true
}
toggleProjectSwitcher: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface newAccountMenuLogicMeta {
__keaTypeGenInternalSelectorTypes: {
hasProjectGroups: (currentOrganization: OrganizationType | null) => boolean
filteredProjectItems: (
currentOrganization: OrganizationType | null,
currentTeam: TeamPublicType | TeamType | null,
projectSearchValue: string
) => ListItem[]
projectGroups: (filteredProjectItems: ListItem[]) => {
name: string | null
projects: ProjectListItem[]
}[]
}
}
export type newAccountMenuLogicType = MakeLogicType<
newAccountMenuLogicValues,
newAccountMenuLogicActions,
Record<string, any>,
newAccountMenuLogicMeta
>
export const newAccountMenuLogic = kea<newAccountMenuLogicType>([
path(['lib', 'components', 'Account', 'accountMenuLogic']),
connect({
values: [organizationLogic, ['currentOrganization'], teamLogic, ['currentTeam']],
}),
actions({
setProjectSearchValue: (searchValue: string) => ({ searchValue }),
setAccountMenuOpen: (isOpen: boolean) => ({ isOpen }),
toggleAccountMenu: true,
// Project switcher modal
openProjectSwitcher: true,
closeProjectSwitcher: true,
toggleProjectSwitcher: true,
// Org switcher modal
openOrgSwitcher: true,
closeOrgSwitcher: true,
toggleOrgSwitcher: true,
}),
reducers({
projectSearchValue: [
'',
{
setProjectSearchValue: (_, { searchValue }) => searchValue,
setAccountMenuOpen: () => '',
toggleAccountMenu: () => '',
closeProjectSwitcher: () => '',
openProjectSwitcher: () => '',
toggleProjectSwitcher: () => '',
},
],
isAccountMenuOpen: [
false,
{
setAccountMenuOpen: (_, { isOpen }) => isOpen,
toggleAccountMenu: (state) => !state,
},
],
isProjectSwitcherOpen: [
false,
{
openProjectSwitcher: () => true,
closeProjectSwitcher: () => false,
toggleProjectSwitcher: (state) => !state,
},
],
isOrgSwitcherOpen: [
false,
{
openOrgSwitcher: () => true,
closeOrgSwitcher: () => false,
toggleOrgSwitcher: (state) => !state,
},
],
}),
selectors({
hasProjectGroups: [
(s) => [s.currentOrganization],
(organization: OrganizationType | null): boolean =>
organization?.teams.some((team) => !!team.project_group) ?? false,
],
filteredProjectItems: [
(s) => [s.currentOrganization, s.currentTeam, s.projectSearchValue],
(
organization: OrganizationType | null,
currentTeam: TeamPublicType | TeamType | null,
searchValue: string
): ListItem[] => {
const projects: ProjectListItem[] = (organization?.teams ?? [])
.filter((team) => matchesSwitcherSearch(team.name, searchValue))
.map((team) => ({ type: 'project', id: team.id, team, isCurrent: team.id === currentTeam?.id }))
const createItem: CreateProjectItem = {
type: 'create',
id: 'create-new-project',
label: 'New project',
// TODO: Uncomment this when we have a way to create projects with a name
// label: searchValue.trim() ? `Create '${searchValue.trim()}'` : 'New project',
}
return [...projects, createItem]
},
],
projectGroups: [
(s) => [s.filteredProjectItems],
(items: ListItem[]): { name: string | null; projects: ProjectListItem[] }[] => {
const groups = new Map<string | null, ProjectListItem[]>()
for (const item of items) {
if (item.type !== 'project') {
continue
}
const name = item.team.project_group ?? null
const projects = groups.get(name) ?? []
projects.push(item)
groups.set(name, projects)
}
return [...groups.entries()]
.sort(([a], [b]) => (a === null ? 1 : b === null ? -1 : a.localeCompare(b)))
.map(([name, projects]) => ({
name,
projects: projects.sort((a, b) => a.team.name.localeCompare(b.team.name)),
}))
},
],
}),
])