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.

3.1 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'

import { LemonSelectOptions } from '@posthog/lemon-ui'

import { teamLogic } from 'scenes/teamLogic'

import { TeamType } from '~/types'

export interface ProductConfigOptionBase {
    title: string
    description: string
    teamProperty: keyof TeamType
    visible: boolean
}

export interface ProductConfigurationToggle extends ProductConfigOptionBase {
    type: 'toggle'
    /** Sets the initial value. Use a team setting to reflect current state, or a static value to set a default. */
    value: boolean
    /** If true, the value is inverted when saving, used for 'opt_out' type settings */
    inverseToggle?: boolean
}

export interface ProductConfigurationSelect extends ProductConfigOptionBase {
    type: 'select'
    /** Sets the initial value. Use a team setting to reflect current state, or a static value to set a default. */
    value: string | number | null
    selectOptions: LemonSelectOptions<any>
    onChange?: (value: string | number | null) => Partial<TeamType>
}

export type ProductConfigOption = ProductConfigurationToggle | ProductConfigurationSelect

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface onboardingProductConfigurationLogicValues {
    configOptions: any[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface onboardingProductConfigurationLogicActions {
    updateCurrentTeam: (payload: Partial<TeamType>) => Partial<TeamType> // teamLogic
    saveConfiguration: () => {
        value: true
    }
    setConfigOptions: (configOptions: ProductConfigOption[]) => {
        configOptions: ProductConfigOption[]
    }
}

export type onboardingProductConfigurationLogicType = MakeLogicType<
    onboardingProductConfigurationLogicValues,
    onboardingProductConfigurationLogicActions
>

export const onboardingProductConfigurationLogic = kea<onboardingProductConfigurationLogicType>([
    path(() => ['scenes', 'onboarding', 'onboardingProductConfigurationLogic']),
    connect(() => ({
        actions: [teamLogic, ['updateCurrentTeam']],
    })),
    actions({
        setConfigOptions: (configOptions: ProductConfigOption[]) => ({ configOptions }),
        saveConfiguration: true,
    }),
    reducers(() => ({
        configOptions: [
            [],
            {
                setConfigOptions: (_, { configOptions }) => configOptions,
            },
        ],
    })),
    listeners(({ values, actions }) => ({
        saveConfiguration: async () => {
            const updateConfig: Record<string, any> = {}

            values.configOptions.forEach((configOption) => {
                if (configOption.onChange) {
                    const changes = configOption.onChange(configOption.value)
                    Object.assign(updateConfig, changes)
                } else {
                    const value = configOption.inverseToggle ? !configOption.value : configOption.value

                    updateConfig[configOption.teamProperty] = value
                }
            })

            actions.updateCurrentTeam(updateConfig as Partial<TeamType>)
        },
    })),
])