: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, kea, key, listeners, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'
import api, { PaginatedResponse } from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'
import { ActorsQuery, NodeKind } from '~/queries/schema/schema-general'
import { CohortType, PersonType } from '~/types'
import { cohortEditLogic } from './cohortEditLogic'
import { createCohortDataNodeLogicKey } from './cohortUtils'
export type AddPersonToCohortModalProps = {
id?: CohortType['id']
}
const DEFAULT_QUERY: ActorsQuery = {
kind: NodeKind.ActorsQuery,
fixedProperties: [],
select: ['id', 'person_display_name -- Person'],
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface addPersonToCohortModalLogicValues {
addPersonToCohortModalVisible: boolean
cohortPersons: PaginatedResponse<PersonType>
cohortPersonsLoading: boolean
isCohortUpdating: boolean
personsToAddToCohort: Record<string, boolean>
query: ActorsQuery
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface addPersonToCohortModalLogicActions {
addPerson: (personId: string) => {
personId: string
}
addPersonsToCohort: () => boolean
hideAddPersonToCohortModal: () => {
value: true
}
loadCohortPersons: () => any
loadCohortPersonsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadCohortPersonsSuccess: (
cohortPersons: PaginatedResponse<PersonType>,
payload?: any
) => {
cohortPersons: PaginatedResponse<PersonType>
payload?: any
}
removePerson: (personId: string) => {
personId: string
}
resetPersons: () => boolean
setCohortUpdating: (updating: boolean) => {
updating: boolean
}
setQuery: (query: ActorsQuery) => {
query: ActorsQuery
}
showAddPersonToCohortModal: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface addPersonToCohortModalLogicMeta {
key: string
}
export type addPersonToCohortModalLogicType = MakeLogicType<
addPersonToCohortModalLogicValues,
addPersonToCohortModalLogicActions,
AddPersonToCohortModalProps,
addPersonToCohortModalLogicMeta
>
export const addPersonToCohortModalLogic = kea<addPersonToCohortModalLogicType>([
props({} as AddPersonToCohortModalProps),
path(['scenes', 'cohorts', 'addPersonToCohortModalLogic']),
key((props) => (props.id === 'new' || !props.id ? 'new' : `${props.id}`)),
actions({
showAddPersonToCohortModal: true,
hideAddPersonToCohortModal: true,
setQuery: (query: ActorsQuery) => ({ query }),
addPersonsToCohort: () => true,
setCohortUpdating: (updating: boolean) => ({ updating }),
addPerson: (personId: string) => ({ personId }),
removePerson: (personId: string) => ({ personId }),
resetPersons: () => true,
}),
reducers({
query: [
DEFAULT_QUERY,
{
setQuery: (_state, { query }) => query,
hideAddPersonToCohortModal: () => DEFAULT_QUERY,
},
],
addPersonToCohortModalVisible: [
false,
{
showAddPersonToCohortModal: () => true,
hideAddPersonToCohortModal: () => false,
},
],
isCohortUpdating: [
false,
{
setCohortUpdating: (_state, { updating }) => updating,
},
],
personsToAddToCohort: [
{} as Record<string, boolean>,
{
addPerson: (state, { personId }) => ({
...state,
[personId]: true,
}),
removePerson: (state, { personId }) => {
const newState = { ...state }
delete newState[personId]
return newState
},
resetPersons: () => {
return {}
},
},
],
}),
loaders(({ props, values }) => ({
cohortPersons: [
{ next: null, previous: null, results: [] } as PaginatedResponse<PersonType>,
{
loadCohortPersons: async () => {
if (props.id == null || props.id === 'new') {
return values.cohortPersons
}
return await api.cohorts.getCohortPersons(props.id)
},
},
],
})),
listeners(({ props, actions, values }) => ({
addPersonsToCohort: async () => {
const cohortId = props.id
if (cohortId == null || cohortId === 'new') {
return
}
try {
actions.setCohortUpdating(true)
const ids = Object.keys(values.personsToAddToCohort)
const response = await api.cohorts.addPersonsToStaticCohort(cohortId, ids)
await actions.loadCohortPersons()
if (response) {
lemonToast.success('Users added to cohort')
const mountedCohortEditLogic = cohortEditLogic.findMounted({ id: cohortId })
await mountedCohortEditLogic?.actions.updateCohortCount()
const mountedDataNodeLogic = dataNodeLogic.findMounted({
key: createCohortDataNodeLogicKey(cohortId),
})
mountedDataNodeLogic?.actions.loadData('force_blocking')
}
actions.hideAddPersonToCohortModal()
} catch (error) {
console.error('Failed to add person to cohort:', error)
lemonToast.error('Unable to add person to cohort')
} finally {
actions.setCohortUpdating(false)
}
},
showAddPersonToCohortModal: () => {
actions.loadCohortPersons()
},
hideAddPersonToCohortModal: () => {
actions.resetPersons()
},
})),
])