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.

4.6 KB
Raw Blame
import { MakeLogicType, connect, kea, key, path, props, selectors } from 'kea'

import { dataNodeLogic } from '~/queries/nodes/DataNode/dataNodeLogic'
import { ActorsQuery, ActorsQueryResponse, DataTableNode } from '~/queries/schema/schema-general'
import { CohortType } from '~/types'

import type {
    ErrorTrackingQueryResponse,
    HogQLAutocompleteResponse,
    HogQLMetadataResponse,
    HogQLQueryResponse,
    HogQueryResponse,
    LogAttributesQueryResponse,
    LogValuesQueryResponse,
    MetricsQueryResponse,
    SessionsQueryResponse,
    TraceSpansAggregationQueryResponse,
    TraceSpansAttributeBreakdownQueryResponse,
    TraceSpansQueryResponse,
} from '../../queries/schema/schema-general'
import type { TraceSpansTreeQueryResponse } from '../../queries/schema/schema-general'

export type CohortCountWarningLogicProps = {
    cohort: CohortType
    query: DataTableNode
    dataNodeLogicKey: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortCountWarningLogicValues {
    response:
        | ErrorTrackingQueryResponse
        | HogQLAutocompleteResponse
        | HogQLMetadataResponse
        | HogQLQueryResponse<any[]>
        | HogQueryResponse
        | LogAttributesQueryResponse
        | LogValuesQueryResponse
        | MetricsQueryResponse
        | Record<string, any>
        | SessionsQueryResponse
        | TraceSpansAggregationQueryResponse
        | TraceSpansAttributeBreakdownQueryResponse
        | TraceSpansQueryResponse
        | TraceSpansTreeQueryResponse
        | null // dataNodeLogic
    shouldShowCountWarning: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface cohortCountWarningLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        shouldShowCountWarning: (
            response:
                | ErrorTrackingQueryResponse
                | HogQLAutocompleteResponse
                | HogQLMetadataResponse
                | HogQLQueryResponse<any[]>
                | HogQueryResponse
                | LogAttributesQueryResponse
                | LogValuesQueryResponse
                | MetricsQueryResponse
                | Record<string, any>
                | SessionsQueryResponse
                | TraceSpansAggregationQueryResponse
                | TraceSpansAttributeBreakdownQueryResponse
                | TraceSpansQueryResponse
                | TraceSpansTreeQueryResponse
                | null,
            query: DataTableNode
        ) => boolean
    }
}

export type cohortCountWarningLogicType = MakeLogicType<
    cohortCountWarningLogicValues,
    {},
    CohortCountWarningLogicProps,
    cohortCountWarningLogicMeta
>

export const cohortCountWarningLogic = kea<cohortCountWarningLogicType>([
    props({} as CohortCountWarningLogicProps),
    key((props) => `cohort-count-warning-${props.cohort.id}-${props.dataNodeLogicKey}`),
    path(['scenes', 'cohorts', 'cohortCountWarningLogic']),

    connect((props: CohortCountWarningLogicProps) => ({
        values: [
            dataNodeLogic({
                key: props.dataNodeLogicKey,
                query: props.query,
                // A draft cohort has no id to filter on, so the query would list every person.
                doNotLoad: typeof props.cohort.id !== 'number',
            }),
            ['response'],
        ],
    })),

    selectors(({ props }) => ({
        shouldShowCountWarning: [
            (s, p) => [s.response, p.query],
            (response: ActorsQueryResponse, query: DataTableNode): boolean => {
                const { cohort } = props

                if (!cohort.count || cohort.is_calculating || cohort.id === 'new' || cohort.is_static) {
                    return false
                }

                if (!response) {
                    return false
                }

                if (response.hasMore) {
                    return false
                }

                const source = query.source as ActorsQuery
                if (source.search) {
                    return false
                }

                if (source.properties) {
                    if (Array.isArray(source.properties) && source.properties.length > 0) {
                        return false
                    }

                    if (!Array.isArray(source.properties) && source.properties.values?.length > 0) {
                        return false
                    }
                }

                const displayedCount = (response as any)?.results?.length || 0
                return displayedCount !== cohort.count
            },
        ],
    })),
])