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

import api from 'lib/api'

import { DataWarehouseManagedViewsetSavedQuery } from '~/types'

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

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface deleteRevenueEventModalLogicActions {
    setEventName: (eventName: string | null) => {
        eventName: string | null
    }
    setEventNameFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    setEventNameSuccess: (
        views: DataWarehouseManagedViewsetSavedQuery[],
        payload?: {
            eventName: string | null
        }
    ) => {
        views: DataWarehouseManagedViewsetSavedQuery[]
        payload?: {
            eventName: string | null
        }
    }
}

export type deleteRevenueEventModalLogicType = MakeLogicType<
    deleteRevenueEventModalLogicValues,
    deleteRevenueEventModalLogicActions
>

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

    actions({
        setEventName: (eventName: string | null) => ({ eventName }),
    }),

    reducers({
        eventName: [
            null as string | null,
            {
                setEventName: (_, { eventName }) => eventName,
            },
        ],
    }),

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

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

                        const eventPrefix = eventName.replace(/\s+/g, '_').toLowerCase()
                        const views = response.views.filter((view) => view.name.includes(eventPrefix))
                        return views
                    } catch (error) {
                        console.error('Failed to fetch views:', error)
                        return []
                    }
                },
            },
        ],
    })),
])