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.5 KB
Raw Blame
import {
    MakeLogicType,
    actions,
    afterMount,
    connect,
    kea,
    key,
    listeners,
    path,
    props,
    propsChanged,
    reducers,
} from 'kea'
import { loaders } from 'kea-loaders'

import { teamLogic } from 'scenes/teamLogic'

import { surveysList, surveysRetrieve } from 'products/surveys/frontend/generated/api'
import type { SurveyApi } from 'products/surveys/frontend/generated/api.schemas'

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

export type SurveyPickerLogicProps = {
    pickerKey: string
    /** When set, the logic resolves this survey's name on mount/change — lets read-only callers show it without a component effect. */
    ensureSurveyId?: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface surveyPickerLogicValues {
    currentProjectId: number | string // teamLogic
    hasLoadedOptions: boolean
    search: string
    selectedSurvey: SurveyApi | null
    selectedSurveyLoading: boolean
    surveyOptions: SurveyApi[]
    surveyOptionsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface surveyPickerLogicActions {
    ensureOptionsLoaded: () => {
        value: true
    }
    ensureSelectedLoaded: (surveyId: string) => {
        surveyId: string
    }
    loadOptions: ({ debounce }?: { debounce: boolean }) => {
        debounce: boolean
    }
    loadOptionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadOptionsSuccess: (
        surveyOptions: SurveyApi[],
        payload?: {
            debounce: boolean
        }
    ) => {
        surveyOptions: SurveyApi[]
        payload?: {
            debounce: boolean
        }
    }
    loadSelectedSurvey: ({ surveyId }: { surveyId: string }) => {
        surveyId: string
    }
    loadSelectedSurveyFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSelectedSurveySuccess: (
        selectedSurvey: SurveyApi,
        payload?: {
            surveyId: string
        }
    ) => {
        selectedSurvey: SurveyApi
        payload?: {
            surveyId: string
        }
    }
    setSearch: (search: string) => {
        search: string
    }
}

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

export type surveyPickerLogicType = MakeLogicType<
    surveyPickerLogicValues,
    surveyPickerLogicActions,
    SurveyPickerLogicProps,
    surveyPickerLogicMeta
>

export const surveyPickerLogic = kea<surveyPickerLogicType>([
    path((key) => ['products', 'dashboards', 'widgets', 'surveys', 'surveyPickerLogic', key]),
    props({} as SurveyPickerLogicProps),
    key((props) => props.pickerKey),

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

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

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

    loaders(({ values }) => ({
        surveyOptions: [
            [] as SurveyApi[],
            {
                loadOptions: async (
                    { debounce }: { debounce: boolean } = { debounce: false },
                    breakpoint
                ): Promise<SurveyApi[]> => {
                    // Debounce keystroke-driven searches; the initial focus load should open the dropdown immediately.
                    if (debounce) {
                        await breakpoint(SEARCH_DEBOUNCE_MS)
                    }
                    const response = await surveysList(String(values.currentProjectId), {
                        limit: SURVEY_OPTIONS_LIMIT,
                        search: values.search || undefined,
                    })
                    breakpoint()
                    return response.results ?? []
                },
            },
        ],
        selectedSurvey: [
            null as SurveyApi | null,
            {
                loadSelectedSurvey: async ({ surveyId }: { surveyId: string }): Promise<SurveyApi> =>
                    surveysRetrieve(String(values.currentProjectId), surveyId),
            },
        ],
    })),

    listeners(({ actions, values }) => ({
        setSearch: () => {
            actions.loadOptions({ debounce: true })
        },
        ensureOptionsLoaded: () => {
            if (!values.hasLoadedOptions && !values.surveyOptionsLoading) {
                actions.loadOptions({ debounce: false })
            }
        },
        ensureSelectedLoaded: ({ surveyId }) => {
            if (values.selectedSurvey?.id === surveyId || values.selectedSurveyLoading) {
                return
            }
            actions.loadSelectedSurvey({ surveyId })
        },
    })),

    afterMount(({ props, actions }) => {
        if (props.ensureSurveyId) {
            actions.ensureSelectedLoaded(props.ensureSurveyId)
        }
    }),

    propsChanged(({ props, actions }, oldProps) => {
        if (props.ensureSurveyId && props.ensureSurveyId !== oldProps.ensureSurveyId) {
            actions.ensureSelectedLoaded(props.ensureSurveyId)
        }
    }),
])