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

import api from 'lib/api'

import { DataWarehouseManagedViewsetSavedQuery, ExternalDataSource } from '~/types'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface disableRevenueSourceModalLogicValues {
    source: ExternalDataSource | null
    views: DataWarehouseManagedViewsetSavedQuery[]
    viewsLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface disableRevenueSourceModalLogicActions {
    setSource: (source: ExternalDataSource | null) => {
        source: ExternalDataSource | null
    }
    setSourceFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    setSourceSuccess: (
        views: DataWarehouseManagedViewsetSavedQuery[],
        payload?: {
            source: ExternalDataSource | null
        }
    ) => {
        views: DataWarehouseManagedViewsetSavedQuery[]
        payload?: {
            source: ExternalDataSource | null
        }
    }
}

export type disableRevenueSourceModalLogicType = MakeLogicType<
    disableRevenueSourceModalLogicValues,
    disableRevenueSourceModalLogicActions
>

export const disableRevenueSourceModalLogic = kea<disableRevenueSourceModalLogicType>([
    path(['products', 'revenue_analytics', 'settings', 'disableRevenueSourceModalLogic']),

    actions({
        setSource: (source: ExternalDataSource | null) => ({ source }),
    }),

    reducers({
        source: [
            null as ExternalDataSource | null,
            {
                setSource: (_, { source }) => source,
            },
        ],
    }),

    loaders(() => ({
        views: [
            [] as DataWarehouseManagedViewsetSavedQuery[],
            {
                setSource: async ({ source }, breakpoint) => {
                    if (!source) {
                        return []
                    }

                    try {
                        const response = await api.dataWarehouseManagedViewsets.getViews('revenue_analytics')
                        await breakpoint(100)

                        // Filter views by source prefix (e.g., "stripe.my_prefix")
                        const sourcePrefix = source.prefix
                            ? `${source.source_type.toLowerCase()}.${source.prefix}`
                            : source.source_type.toLowerCase()

                        const views = response.views.filter((view) => view.name.startsWith(sourcePrefix))
                        return views
                    } catch (error) {
                        console.error('Failed to fetch views:', error)
                        return []
                    }
                },
            },
        ],
    })),
])