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.

4.3 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, key, path, props, reducers } from 'kea'
import { loaders } from 'kea-loaders'

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

import { DataWarehouseManagedViewsetKind } from '~/queries/schema/schema-general'

import type { TeamPublicType, TeamType } from '../../../types'
import {
    DisableDataWarehouseManagedViewsetModalLogicProps,
    disableDataWarehouseManagedViewsetModalLogic,
} from './disableDataWarehouseManagedViewsetModalLogic'

export interface DataWarehouseManagedViewsetsLogicProps {
    type: DisableDataWarehouseManagedViewsetModalLogicProps['type']
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataWarehouseManagedViewsetsLogicValues {
    currentTeam: TeamPublicType | TeamType | null // teamLogic
    toggleResult: void | null
    toggleResultLoading: boolean
    togglingViewset: DataWarehouseManagedViewsetKind | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataWarehouseManagedViewsetsLogicActions {
    openModal: (kind: 'revenue_analytics') => {
        kind: 'revenue_analytics'
    } // disableDataWarehouseManagedViewsetModalLogic
    loadCurrentTeam: () => any // teamLogic
    toggleViewset: (
        kind: DataWarehouseManagedViewsetKind,
        enabled: boolean
    ) => {
        enabled: boolean
        kind: 'revenue_analytics'
    }
    toggleViewsetFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    toggleViewsetSuccess: (
        toggleResult: void,
        payload?: {
            enabled: boolean
            kind: 'revenue_analytics'
        }
    ) => {
        toggleResult: void
        payload?: {
            enabled: boolean
            kind: 'revenue_analytics'
        }
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface dataWarehouseManagedViewsetsLogicMeta {
    key: string
}

export type dataWarehouseManagedViewsetsLogicType = MakeLogicType<
    dataWarehouseManagedViewsetsLogicValues,
    dataWarehouseManagedViewsetsLogicActions,
    DataWarehouseManagedViewsetsLogicProps,
    dataWarehouseManagedViewsetsLogicMeta
>

export const dataWarehouseManagedViewsetsLogic = kea<dataWarehouseManagedViewsetsLogicType>([
    props({ type: 'root' } as DataWarehouseManagedViewsetsLogicProps),
    key(({ type }) => `dataWarehouseManagedViewsetsLogic-${type}`),
    path((key) => ['scenes', 'data-management', 'managed-viewsets', key]),
    connect((props: DataWarehouseManagedViewsetsLogicProps) => ({
        values: [teamLogic, ['currentTeam']],
        actions: [
            teamLogic,
            ['loadCurrentTeam'],
            disableDataWarehouseManagedViewsetModalLogic({ type: props.type }),
            ['openModal'],
        ],
    })),
    actions({
        toggleViewset: (kind: DataWarehouseManagedViewsetKind, enabled: boolean) => ({ kind, enabled }),
    }),
    reducers({
        togglingViewset: [
            null as DataWarehouseManagedViewsetKind | null,
            {
                toggleViewset: (_, { kind }) => kind,
            },
        ],
    }),
    loaders(({ actions }) => ({
        toggleResult: [
            null as void | null,
            {
                toggleViewset: async ({ kind, enabled }) => {
                    // If disabling, show confirmation modal instead
                    if (!enabled) {
                        actions.openModal(kind)
                        return
                    }

                    // If enabling, proceed directly
                    try {
                        await api.dataWarehouseManagedViewsets.toggle(kind, true)
                        if (kind === 'revenue_analytics') {
                            await api.revenueAnalyticsJoins.sync(true)
                        }
                        lemonToast.success(`Viewset enabled successfully`)
                        actions.loadCurrentTeam()
                    } catch (error: any) {
                        lemonToast.error(`Failed to enable viewset: ${error.message || 'Unknown error'}`)
                        throw error
                    }
                },
            },
        ],
    })),
])