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.

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

import { teamLogic } from 'scenes/teamLogic'

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

/** One page is plenty — search narrows the rest server-side, so we never paginate the dropdown. */
const EXPERIMENT_OPTIONS_LIMIT = 50
const SEARCH_DEBOUNCE_MS = 300

export type ExperimentPickerLogicProps = { pickerKey: string }

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentPickerLogicValues {
    currentProjectId: number | string // teamLogic
    experimentOptions: ExperimentBasicApi[]
    experimentOptionsLoading: boolean
    hasLoadedOptions: boolean
    search: string
    selectedExperiment: ExperimentApi | null
    selectedExperimentLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface experimentPickerLogicActions {
    ensureOptionsLoaded: () => {
        value: true
    }
    ensureSelectedLoaded: (experimentId: number) => {
        experimentId: number
    }
    loadOptions: ({ debounce }?: { debounce: boolean }) => {
        debounce: boolean
    }
    loadOptionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadOptionsSuccess: (
        experimentOptions: ExperimentBasicApi[],
        payload?: {
            debounce: boolean
        }
    ) => {
        experimentOptions: ExperimentBasicApi[]
        payload?: {
            debounce: boolean
        }
    }
    loadSelectedExperiment: ({ experimentId }: { experimentId: number }) => {
        experimentId: number
    }
    loadSelectedExperimentFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSelectedExperimentSuccess: (
        selectedExperiment: ExperimentApi,
        payload?: {
            experimentId: number
        }
    ) => {
        selectedExperiment: ExperimentApi
        payload?: {
            experimentId: number
        }
    }
    setSearch: (search: string) => {
        search: string
    }
}

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

export type experimentPickerLogicType = MakeLogicType<
    experimentPickerLogicValues,
    experimentPickerLogicActions,
    ExperimentPickerLogicProps,
    experimentPickerLogicMeta
>

export const experimentPickerLogic = kea<experimentPickerLogicType>([
    path((key) => ['products', 'dashboards', 'widgets', 'experiments', 'experimentPickerLogic', key]),
    props({} as ExperimentPickerLogicProps),
    key((props) => props.pickerKey),

    connect(() => ({
        values: [teamLogic, ['currentProjectId']],
    })),

    actions({
        setSearch: (search: string) => ({ search }),
        ensureOptionsLoaded: true,
        ensureSelectedLoaded: (experimentId: number) => ({ experimentId }),
    }),

    reducers({
        search: ['', { setSearch: (_: string, { search }: { search: string }) => search }],
        hasLoadedOptions: [false, { loadOptionsSuccess: () => true }],
    }),

    loaders(({ values }) => ({
        experimentOptions: [
            [] as ExperimentBasicApi[],
            {
                loadOptions: async (
                    { debounce }: { debounce: boolean } = { debounce: false },
                    breakpoint
                ): Promise<ExperimentBasicApi[]> => {
                    // Debounce keystroke-driven searches; the initial focus load should open the dropdown immediately.
                    if (debounce) {
                        await breakpoint(SEARCH_DEBOUNCE_MS)
                    }
                    const response = await experimentsList(String(values.currentProjectId), {
                        limit: EXPERIMENT_OPTIONS_LIMIT,
                        order: '-created_at',
                        search: values.search || undefined,
                    })
                    breakpoint()
                    return response.results ?? []
                },
            },
        ],
        selectedExperiment: [
            null as ExperimentApi | null,
            {
                loadSelectedExperiment: async ({ experimentId }: { experimentId: number }): Promise<ExperimentApi> =>
                    experimentsRetrieve(String(values.currentProjectId), experimentId),
            },
        ],
    })),

    listeners(({ actions, values }) => ({
        setSearch: () => {
            actions.loadOptions({ debounce: true })
        },
        ensureOptionsLoaded: () => {
            if (!values.hasLoadedOptions && !values.experimentOptionsLoading) {
                actions.loadOptions({ debounce: false })
            }
        },
        ensureSelectedLoaded: ({ experimentId }) => {
            if (values.selectedExperiment?.id === experimentId || values.selectedExperimentLoading) {
                return
            }
            actions.loadSelectedExperiment({ experimentId })
        },
    })),
])