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.4 KB
Raw Blame
import { MakeLogicType, afterMount, kea, path } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'

export interface SurveyQuestionLabel {
    surveyId: string
    surveyName: string
    questionText: string
    questionIndex: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface surveyQuestionLabelsLogicValues {
    surveyQuestionLabels: Record<string, SurveyQuestionLabel>
    surveyQuestionLabelsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface surveyQuestionLabelsLogicActions {
    loadSurveyQuestionLabels: () => any
    loadSurveyQuestionLabelsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSurveyQuestionLabelsSuccess: (
        surveyQuestionLabels: Record<string, SurveyQuestionLabel>,
        payload?: any
    ) => {
        surveyQuestionLabels: Record<string, SurveyQuestionLabel>
        payload?: any
    }
}

export type surveyQuestionLabelsLogicType = MakeLogicType<
    surveyQuestionLabelsLogicValues,
    surveyQuestionLabelsLogicActions
>

export const surveyQuestionLabelsLogic = kea<surveyQuestionLabelsLogicType>([
    path(['scenes', 'surveys', 'surveyQuestionLabelsLogic']),
    loaders(({ values }) => ({
        surveyQuestionLabels: [
            {} as Record<string, SurveyQuestionLabel>,
            {
                loadSurveyQuestionLabels: async () => {
                    // The endpoint returns one entry per question with an ID, across all the team's surveys.
                    // The payload is intentionally slim (≈100 bytes per question) so this scales to thousands.
                    const response = await api.surveys.questionLabels()
                    const labels: Record<string, SurveyQuestionLabel> = { ...values.surveyQuestionLabels }
                    for (const entry of response.labels) {
                        labels[entry.question_id] = {
                            surveyId: entry.survey_id,
                            surveyName: entry.survey_name,
                            questionText: entry.question_text,
                            questionIndex: entry.question_index,
                        }
                    }
                    return labels
                },
            },
        ],
    })),
    afterMount(({ actions }) => {
        actions.loadSurveyQuestionLabels()
    }),
])