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.

2.9 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'

import { teamLogic } from 'scenes/teamLogic'

import { experimentsList } from 'products/experiments/frontend/generated/api'
import type {
    ExperimentBasicApi,
    PaginatedExperimentBasicListApi,
} from 'products/experiments/frontend/generated/api.schemas'

export interface EventExperimentsLogicProps {
    event: string
}

export const EXPERIMENTS_PER_PAGE = 10

const EMPTY_RESULT: PaginatedExperimentBasicListApi = { count: 0, next: null, previous: null, results: [] }

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventExperimentsLogicValues {
    currentProjectId: number | string // teamLogic
    experiments: PaginatedExperimentBasicListApi
    experimentsLoading: boolean
    page: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventExperimentsLogicActions {
    loadExperiments: () => any
    loadExperimentsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadExperimentsSuccess: (
        experiments: PaginatedExperimentBasicListApi,
        payload?: any
    ) => {
        experiments: PaginatedExperimentBasicListApi
        payload?: any
    }
    setPage: (page: number) => {
        page: number
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventExperimentsLogicMeta {
    key: string
}

export type eventExperimentsLogicType = MakeLogicType<
    eventExperimentsLogicValues,
    eventExperimentsLogicActions,
    EventExperimentsLogicProps,
    eventExperimentsLogicMeta
>

export const eventExperimentsLogic = kea<eventExperimentsLogicType>([
    path(['scenes', 'data-management', 'events', 'eventExperimentsLogic']),
    props({} as EventExperimentsLogicProps),
    key(({ event }) => event),
    connect(() => ({
        values: [teamLogic, ['currentProjectId']],
    })),
    actions({
        setPage: (page: number) => ({ page }),
    }),
    reducers({
        page: [1, { setPage: (_, { page }) => page }],
    }),
    loaders(({ values, props }) => ({
        experiments: [
            EMPTY_RESULT,
            {
                loadExperiments: async (): Promise<PaginatedExperimentBasicListApi> => {
                    return await experimentsList(String(values.currentProjectId), {
                        event: props.event,
                        limit: EXPERIMENTS_PER_PAGE,
                        offset: Math.max(0, (values.page - 1) * EXPERIMENTS_PER_PAGE),
                    })
                },
            },
        ],
    })),
    listeners(({ actions }) => ({
        setPage: () => {
            actions.loadExperiments()
        },
    })),
    afterMount(({ actions }) => {
        actions.loadExperiments()
    }),
])

export type { ExperimentBasicApi }