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.

14.2 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { router, urlToAction } from 'kea-router'
import posthog from 'posthog-js'

import { PaginationManual, Sorting } from '@posthog/lemon-ui'

import api, { CountedPaginatedResponse } from 'lib/api'
import { exportsLogic } from 'lib/components/ExportButton/exportsLogic'
import { trackedActionToUrl } from 'lib/logic/scenes/trackedActionToUrl'
import { deleteWithUndo } from 'lib/utils/deleteWithUndo'
import { objectsEqual } from 'lib/utils/objects'
import { processCohort } from 'scenes/cohorts/processCohort'
import { urls } from 'scenes/urls'

import { SIDE_PANEL_CONTEXT_KEY, SidePanelSceneContext } from '~/layout/navigation-3000/sidepanel/types'
import { deleteFromTree, refreshTreeItem } from '~/layout/panel-layout/ProjectTree/projectTreeLogic'
import { ActivityScope, Breadcrumb, CohortType, ExporterFormat } from '~/types'

import { personsLogic } from 'products/persons/frontend/logics/personsLogic'

import type { TriggerExportProps } from '../../lib/components/ExportButton/exporter'

export interface CohortFilters {
    search?: string
    page?: number
    type?: 'static' | 'dynamic'
    created_by_id?: number
}

const POLL_TIMEOUT = 5000

const DEFAULT_COHORT_FILTERS: CohortFilters = {
    search: undefined,
    page: 1,
    type: undefined,
    created_by_id: undefined,
}

const COHORTS_PER_PAGE = 100

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortsSceneLogicValues {
    breadcrumbs: Breadcrumb[]
    cohortFilters: CohortFilters
    cohortSorting: Sorting | null
    cohorts: CountedPaginatedResponse<CohortType>
    cohortsLoadError: string | null
    cohortsLoading: boolean
    count: number
    pagination: PaginationManual
    paramsFromFilters: {
        created_by_id?: number | undefined
        limit: number
        offset: number
        page?: number | undefined
        search?: string | undefined
        type?: 'dynamic' | 'static' | undefined
    }
    shouldShowEmptyState: boolean
    sidePanelContext: SidePanelSceneContext
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortsSceneLogicActions {
    startExport: (exportData: TriggerExportProps) => {
        exportData: TriggerExportProps
    } // exportsLogic
    deleteCohort: (cohort: Partial<CohortType>) => {
        cohort: Partial<CohortType>
    }
    exportCohortPersons: (
        id: CohortType['id'],
        columns?: string[]
    ) => {
        columns: string[] | undefined
        id: number | 'new'
    }
    loadCohorts: () => any
    loadCohortsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadCohortsSuccess: (
        cohorts: {
            count: number
            results: CohortType[]
        },
        payload?: any
    ) => {
        cohorts: {
            count: number
            results: CohortType[]
        }
        payload?: any
    }
    setCohortFilters: (
        filters: Partial<CohortFilters>,
        replace?: boolean,
        debounce?: boolean
    ) => {
        debounce: boolean
        filters: Partial<CohortFilters>
        replace: boolean | undefined
    }
    setCohortSorting: (sorting: Sorting | null) => {
        sorting: Sorting | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortsSceneLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        count: (cohorts: CountedPaginatedResponse<CohortType>) => number
        paramsFromFilters: (cohortFilters: CohortFilters) => {
            created_by_id?: number | undefined
            limit: number
            offset: number
            page?: number | undefined
            search?: string | undefined
            type?: 'dynamic' | 'static' | undefined
        }
        pagination: (cohortFilters: CohortFilters, count: number) => PaginationManual
        shouldShowEmptyState: (
            cohorts: CountedPaginatedResponse<CohortType>,
            cohortFilters: CohortFilters,
            cohortsLoadError: string | null
        ) => boolean
    }
}

export type cohortsSceneLogicType = MakeLogicType<
    cohortsSceneLogicValues,
    cohortsSceneLogicActions,
    Record<string, any>,
    cohortsSceneLogicMeta
>

export const cohortsSceneLogic = kea<cohortsSceneLogicType>([
    path(['scenes', 'cohorts', 'cohortsSceneLogic']),
    connect(() => ({
        actions: [exportsLogic, ['startExport']],
    })),
    actions(() => ({
        setCohortFilters: (filters: Partial<CohortFilters>, replace?: boolean, debounce: boolean = true) => ({
            filters,
            replace,
            debounce,
        }),
        deleteCohort: (cohort: Partial<CohortType>) => ({ cohort }),
        exportCohortPersons: (id: CohortType['id'], columns?: string[]) => ({ id, columns }),
        setCohortSorting: (sorting: Sorting | null) => ({ sorting }),
    })),
    reducers({
        cohortFilters: [
            DEFAULT_COHORT_FILTERS,
            {
                setCohortFilters: (state, { filters, replace }) => {
                    if (replace) {
                        return { ...DEFAULT_COHORT_FILTERS, ...filters }
                    }
                    return { ...state, ...filters }
                },
            },
        ],
        cohortSorting: [
            null as Sorting | null,
            {
                setCohortSorting: (_, { sorting }) => {
                    return sorting
                },
            },
        ],
        cohorts: [
            { count: 0, results: [] },
            {
                deleteCohort: (state, { cohort }) => {
                    if (!cohort.id) {
                        return state
                    }
                    return {
                        ...state,
                        results: state.results.filter((c) => c.id !== cohort.id),
                    }
                },
            },
        ],
    }),
    loaders(({ values }) => ({
        cohorts: [
            {
                count: 0,
                results: [],
                filters: DEFAULT_COHORT_FILTERS,
                offset: 0,
            } as CountedPaginatedResponse<CohortType>,
            {
                loadCohorts: async () => {
                    const response = await api.cohorts.listPaginated({
                        ...values.paramsFromFilters,
                    })
                    personsLogic.findMounted({ syncWithUrl: true })?.actions.loadCohorts()
                    return {
                        count: response.count,
                        results: response.results.map((cohort) => processCohort(cohort)),
                    }
                },
            },
        ],
    })),
    reducers({
        cohortsLoadError: [
            null as string | null,
            {
                loadCohorts: () => null,
                loadCohortsSuccess: () => null,
                loadCohortsFailure: (_, { error, errorObject }) => {
                    if (errorObject && 'detail' in errorObject) {
                        return errorObject.detail ?? 'Error loading cohorts'
                    }
                    return error ?? 'Error loading cohorts'
                },
            },
        ],
    }),
    selectors({
        breadcrumbs: [
            () => [],
            (): Breadcrumb[] => {
                return [
                    {
                        key: 'cohorts',
                        name: 'Cohorts',
                        path: urls.cohorts(),
                        iconType: 'cohort',
                    },
                ]
            },
        ],
        [SIDE_PANEL_CONTEXT_KEY]: [
            () => [],
            (): SidePanelSceneContext => ({
                activity_scope: ActivityScope.COHORT,
            }),
        ],
        count: [(selectors) => [selectors.cohorts], (cohorts: CountedPaginatedResponse<CohortType>) => cohorts.count],
        paramsFromFilters: [
            (s) => [s.cohortFilters],
            (filters: CohortFilters) => ({
                ...filters,
                limit: COHORTS_PER_PAGE,
                offset: filters.page ? (filters.page - 1) * COHORTS_PER_PAGE : 0,
            }),
        ],
        pagination: [
            (s) => [s.cohortFilters, s.count],
            (filters: CohortFilters, count: number): PaginationManual => {
                return {
                    controlled: true,
                    pageSize: COHORTS_PER_PAGE,
                    currentPage: filters.page || 1,
                    entryCount: count,
                }
            },
        ],
        shouldShowEmptyState: [
            (s) => [s.cohorts, s.cohortFilters, s.cohortsLoadError],
            (
                cohorts: CountedPaginatedResponse<CohortType>,
                filters: CohortFilters,
                cohortsLoadError: string | null
            ): boolean => {
                return (
                    !cohortsLoadError && cohorts.results.length === 0 && objectsEqual(filters, DEFAULT_COHORT_FILTERS)
                )
            },
        ],
    }),
    listeners(({ actions, cache, values }) => ({
        loadCohortsSuccess: async ({ cohorts }: { cohorts: CountedPaginatedResponse<CohortType> }) => {
            const is_calculating = cohorts.results.filter((cohort) => cohort.is_calculating).length > 0
            if (!is_calculating || !router.values.location.pathname.includes(urls.cohorts())) {
                return
            }
            cache.disposables.add(() => {
                const timerId = window.setTimeout(actions.loadCohorts, POLL_TIMEOUT)
                return () => clearTimeout(timerId)
            }, 'pollTimeout')
        },
        setCohortFilters: async ({ debounce }, breakpoint) => {
            if (debounce) {
                await breakpoint(300)
            }
            actions.loadCohorts()
        },
        deleteCohort: async ({ cohort }) => {
            await deleteWithUndo({
                endpoint: api.cohorts.determineDeleteEndpoint(),
                object: cohort,
                callback: (undo) => {
                    actions.loadCohorts()
                    if (cohort.id && cohort.id !== 'new') {
                        if (undo) {
                            refreshTreeItem('cohort', String(cohort.id))
                        } else {
                            deleteFromTree('cohort', String(cohort.id))
                        }
                    }
                },
                // The reducer already removed the row, so reload to show the cohort the server kept.
                onError: () => actions.loadCohorts(),
            })
        },
        exportCohortPersons: async ({ id, columns }) => {
            const cohort = values.cohorts.results.find((c) => c.id === id)
            const exportCommand = {
                export_format: ExporterFormat.CSV,
                export_context: {
                    path: `/api/cohort/${id}/persons`,
                    columns,
                    filename: cohort?.name ? `cohort-${cohort.name}` : 'cohort',
                } as { path: string; columns?: string[]; filename?: string },
            }
            if (columns && columns.length > 0) {
                exportCommand.export_context['columns'] = columns
            }
            actions.startExport(exportCommand)
        },
    })),
    trackedActionToUrl(({ values }) => ({
        setCohortFilters: () => {
            const searchParams: Record<string, any> = { ...router.values.searchParams }

            if (values.cohortFilters.page != null) {
                searchParams['page'] = values.cohortFilters.page
            } else {
                delete searchParams['page']
            }

            if (values.cohortFilters.search != null) {
                searchParams['search'] = values.cohortFilters.search
            } else {
                delete searchParams['search']
            }

            return [router.values.location.pathname, searchParams, router.values.hashParams, { replace: true }]
        },
        setCohortSorting: () => {
            const searchParams: Record<string, any> = { ...router.values.searchParams }

            if (values.cohortSorting != null) {
                searchParams['sorting'] = JSON.stringify(values.cohortSorting)
            } else {
                delete searchParams['sorting']
            }

            return [router.values.location.pathname, searchParams, router.values.hashParams, { replace: true }]
        },
    })),
    urlToAction(({ actions, values }) => ({
        [urls.cohorts()]: (_, searchParams) => {
            const { page, search, type, created_by_id, sorting } = searchParams
            const filtersFromUrl: Partial<CohortFilters> = {
                search,
                type,
            }

            if (page !== undefined) {
                filtersFromUrl.page = parseInt(page)
            }
            if (created_by_id !== undefined) {
                filtersFromUrl.created_by_id = parseInt(created_by_id)
            }

            const nextFilters = { ...DEFAULT_COHORT_FILTERS, ...filtersFromUrl }
            // Skip the echo of our own URL write, or it sends a second request
            if (!objectsEqual(nextFilters, values.cohortFilters)) {
                actions.setCohortFilters(nextFilters, true, false)
            }

            let currentSorting = values.cohortSorting

            if (sorting != null) {
                try {
                    const parsedSorting = JSON.parse(sorting)
                    if (parsedSorting) {
                        currentSorting = parsedSorting
                    }
                } catch (error: any) {
                    console.error('Failed to parse sorting', error, { sorting })
                    posthog.captureException(error, {
                        extra: {
                            context: 'Failed to parse sorting',
                            sorting: sorting,
                        },
                    })
                }
            }

            actions.setCohortSorting(currentSorting)
        },
    })),
    afterMount(({ actions, values }) => {
        // urlToAction runs first and already loads when the URL carries filters
        if (!values.cohortsLoading) {
            actions.loadCohorts()
        }
    }),
])