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.6 KB
Raw Blame
import { LogicWrapper, MakeLogicType, afterMount, connect, kea, key, path, props } from 'kea'
import { loaders } from 'kea-loaders'

import { organizationLogic } from 'scenes/organizationLogic'
import { teamLogic } from 'scenes/teamLogic'

import * as api from 'products/product_analytics/frontend/generated/api'

export type EventsRetentionLevel = 'project' | 'organization'

export type EventsRetentionDeletionCountKind = EventsRetentionLevel | 'downgrade'

export interface EventsRetentionDeletionCountLogicProps {
    count: EventsRetentionDeletionCountKind
    months: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventsRetentionDeletionCountLogicValues {
    currentOrganizationId: string // organizationLogic
    currentTeamId: number | null // teamLogic
    eventsDeleted: number | null
    eventsDeletedLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface eventsRetentionDeletionCountLogicActions {
    loadEventsDeleted: () => any
    loadEventsDeletedFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadEventsDeletedSuccess: (
        eventsDeleted: number | null,
        payload?: any
    ) => {
        eventsDeleted: number | null
        payload?: any
    }
}

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

export type eventsRetentionDeletionCountLogicType = MakeLogicType<
    eventsRetentionDeletionCountLogicValues,
    eventsRetentionDeletionCountLogicActions,
    EventsRetentionDeletionCountLogicProps,
    eventsRetentionDeletionCountLogicMeta
>

export const eventsRetentionDeletionCountLogic: LogicWrapper<eventsRetentionDeletionCountLogicType> =
    kea<eventsRetentionDeletionCountLogicType>([
        props({} as EventsRetentionDeletionCountLogicProps),
        key(({ count, months }) => `${count}-${months}`),
        path((key) => [
            'products',
            'product_analytics',
            'frontend',
            'settings',
            'eventsRetentionDeletionCountLogic',
            key,
        ]),
        connect(() => ({
            values: [organizationLogic, ['currentOrganizationId'], teamLogic, ['currentTeamId']],
        })),
        loaders(({ props, values }) => ({
            eventsDeleted: [
                null as number | null,
                {
                    loadEventsDeleted: async () => {
                        const params = { retention_months: props.months }
                        const countEventsDeleted = {
                            project: () => api.eventsRetentionDeletionCount(String(values.currentTeamId), params),
                            organization: () =>
                                api.organizationEventsRetentionDeletionCount(values.currentOrganizationId, params),
                            downgrade: () =>
                                api.organizationEventsRetentionDowngradeDeletionCount(
                                    values.currentOrganizationId,
                                    params
                                ),
                        }[props.count]
                        try {
                            const response = await countEventsDeleted()
                            return response.events_deleted
                        } catch {
                            return null
                        }
                    },
                },
            ],
        })),
        afterMount(({ actions }) => {
            actions.loadEventsDeleted()
        }),
    ])