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.

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

import api from 'lib/api'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { teamLogic } from 'scenes/teamLogic'

import { CustomerProfileConfigType, CustomerProfileScope } from '~/types'

export interface CustomerProfileConfigLogicProps {
    scope?: CustomerProfileScope
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface customerProfileConfigLogicValues {
    currentTeamId: number | null // teamLogic
    configs: CustomerProfileConfigType[]
    configsLoading: boolean
    customerProfileConfig: CustomerProfileConfigType | undefined
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface customerProfileConfigLogicActions {
    createConfig: (config: Partial<CustomerProfileConfigType>) => {
        config: Partial<CustomerProfileConfigType>
    }
    createConfigFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    createConfigSuccess: (
        configs: CustomerProfileConfigType[],
        payload?: {
            config: Partial<CustomerProfileConfigType>
        }
    ) => {
        configs: CustomerProfileConfigType[]
        payload?: {
            config: Partial<CustomerProfileConfigType>
        }
    }
    deleteConfig: (id: CustomerProfileConfigType['id']) => {
        id: string
    }
    deleteConfigFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    deleteConfigSuccess: (
        configs: CustomerProfileConfigType[],
        payload?: {
            id: string
        }
    ) => {
        configs: CustomerProfileConfigType[]
        payload?: {
            id: string
        }
    }
    loadConfigs: () => {
        value: true
    }
    loadConfigsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadConfigsSuccess: (
        configs: CustomerProfileConfigType[],
        payload?: {
            value: true
        }
    ) => {
        configs: CustomerProfileConfigType[]
        payload?: {
            value: true
        }
    }
    setConfigs: (configs: CustomerProfileConfigType[]) => {
        configs: CustomerProfileConfigType[]
    }
    updateConfig: (
        id: CustomerProfileConfigType['id'],
        config: Partial<CustomerProfileConfigType>
    ) => {
        config: Partial<CustomerProfileConfigType>
        id: string
    }
    updateConfigFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    updateConfigSuccess: (
        configs: CustomerProfileConfigType[],
        payload?: {
            config: Partial<CustomerProfileConfigType>
            id: string
        }
    ) => {
        configs: CustomerProfileConfigType[]
        payload?: {
            config: Partial<CustomerProfileConfigType>
            id: string
        }
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface customerProfileConfigLogicMeta {
    key: CustomerProfileScope | 'all'
    __keaTypeGenInternalSelectorTypes: {
        customerProfileConfig: (configs: CustomerProfileConfigType[], arg: any) => CustomerProfileConfigType | undefined
    }
}

export type customerProfileConfigLogicType = MakeLogicType<
    customerProfileConfigLogicValues,
    customerProfileConfigLogicActions,
    CustomerProfileConfigLogicProps,
    customerProfileConfigLogicMeta
>

export const customerProfileConfigLogic = kea<customerProfileConfigLogicType>([
    path(['products', 'customer_analytics', 'customerProfileConfigLogic']),
    props({} as CustomerProfileConfigLogicProps),
    key((props) => props.scope || 'all'),

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

    actions({
        loadConfigs: true,
        createConfig: (config: Partial<CustomerProfileConfigType>) => ({ config }),
        updateConfig: (id: CustomerProfileConfigType['id'], config: Partial<CustomerProfileConfigType>) => ({
            id,
            config,
        }),
        deleteConfig: (id: CustomerProfileConfigType['id']) => ({ id }),
        setConfigs: (configs: CustomerProfileConfigType[]) => ({ configs }),
    }),

    loaders(({ props, values }) => ({
        configs: [
            [] as CustomerProfileConfigType[],
            {
                loadConfigs: async () => {
                    try {
                        const params = props.scope ? { scope: props.scope } : {}
                        const response = await api.customerProfileConfigs.list(params)
                        return response.results
                    } catch (error) {
                        lemonToast.error('Failed to load customer profile configs')
                        throw error
                    }
                },
                createConfig: async ({ config }) => {
                    try {
                        const newConfig = await api.customerProfileConfigs.create(config)
                        lemonToast.success('Customer profile config created successfully')
                        return [...values.configs, newConfig]
                    } catch (error) {
                        lemonToast.error('Failed to create customer profile config')
                        throw error
                    }
                },
                updateConfig: async ({ id, config }) => {
                    try {
                        const updatedConfig = await api.customerProfileConfigs.update(id, config)
                        lemonToast.success('Customer profile config updated successfully')
                        return values.configs.map((c) => (c.id === id ? updatedConfig : c))
                    } catch (error) {
                        lemonToast.error('Failed to update customer profile config')
                        throw error
                    }
                },
                deleteConfig: async ({ id }) => {
                    try {
                        await api.customerProfileConfigs.delete(id)
                        lemonToast.success('Customer profile config deleted successfully')
                        return values.configs.filter((c) => c.id !== id)
                    } catch (error) {
                        lemonToast.error('Failed to delete customer profile config')
                        throw error
                    }
                },
            },
        ],
    })),

    selectors({
        customerProfileConfig: [
            (s) => [s.configs, (_, props) => props.scope],
            (configs: CustomerProfileConfigType[], scope): CustomerProfileConfigType | undefined =>
                configs.find((c) => c.scope === scope),
        ],
    }),

    listeners(({ actions }) => ({
        createConfigSuccess: () => {
            actions.loadConfigs()
        },
        updateConfigSuccess: () => {
            actions.loadConfigs()
        },
        deleteConfigSuccess: () => {
            actions.loadConfigs()
        },
    })),

    afterMount(({ actions }) => {
        actions.loadConfigs()
    }),
])