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.

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

import { billingLogic } from 'lib/logic/billing/billingLogic'

import type { OrganizationType } from '~/types'

import { billingAlertsEventsList } from 'products/billing/frontend/generated/api'
import type { PaginatedBillingAlertEventListApi } from 'products/billing/frontend/generated/api.schemas'

import { offsetFromPageLink } from './billingAlertUtils'

export interface BillingAlertHistoryLogicProps {
    alertId: string
}

const EMPTY_PAGE: PaginatedBillingAlertEventListApi = {
    count: 0,
    next: null,
    previous: null,
    results: [],
}

export const HISTORY_PAGE_SIZE = 50

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface billingAlertHistoryLogicValues {
    currentOrganization: OrganizationType | null // billingLogic
    currentPage: number
    eventsPage: PaginatedBillingAlertEventListApi
    eventsPageLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface billingAlertHistoryLogicActions {
    loadEvents: (_: any) => any
    loadEventsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadEventsSuccess: (
        eventsPage: PaginatedBillingAlertEventListApi,
        payload?: any
    ) => {
        eventsPage: PaginatedBillingAlertEventListApi
        payload?: any
    }
    loadMoreEvents: (_: any) => any
    loadMoreEventsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadMoreEventsSuccess: (
        eventsPage: PaginatedBillingAlertEventListApi,
        payload?: any
    ) => {
        eventsPage: PaginatedBillingAlertEventListApi
        payload?: any
    }
    loadPreviousEvents: (_: any) => any
    loadPreviousEventsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadPreviousEventsSuccess: (
        eventsPage: PaginatedBillingAlertEventListApi,
        payload?: any
    ) => {
        eventsPage: PaginatedBillingAlertEventListApi
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface billingAlertHistoryLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        currentPage: (eventsPage: PaginatedBillingAlertEventListApi) => number
    }
}

export type billingAlertHistoryLogicType = MakeLogicType<
    billingAlertHistoryLogicValues,
    billingAlertHistoryLogicActions,
    BillingAlertHistoryLogicProps,
    billingAlertHistoryLogicMeta
>

export const billingAlertHistoryLogic = kea<billingAlertHistoryLogicType>([
    path(['products', 'billingAlerts', 'frontend', 'billingAlertHistoryLogic']),
    props({} as BillingAlertHistoryLogicProps),
    key(({ alertId }) => alertId),
    connect({
        values: [billingLogic, ['currentOrganization']],
    }),
    loaders(({ props, values }) => ({
        eventsPage: [
            EMPTY_PAGE,
            {
                loadEvents: async (_, breakpoint) => {
                    if (!values.currentOrganization?.id) {
                        return EMPTY_PAGE
                    }
                    await breakpoint()
                    const page = await billingAlertsEventsList(values.currentOrganization.id, props.alertId, {
                        limit: HISTORY_PAGE_SIZE,
                    })
                    await breakpoint()
                    return page
                },
                loadMoreEvents: async (_, breakpoint) => {
                    if (!values.currentOrganization?.id || !values.eventsPage.next) {
                        return values.eventsPage
                    }
                    await breakpoint()
                    const nextPage = await billingAlertsEventsList(values.currentOrganization.id, props.alertId, {
                        limit: HISTORY_PAGE_SIZE,
                        offset: offsetFromPageLink(values.eventsPage.next),
                    })
                    await breakpoint()
                    return nextPage
                },
                loadPreviousEvents: async (_, breakpoint) => {
                    if (!values.currentOrganization?.id || !values.eventsPage.previous) {
                        return values.eventsPage
                    }
                    await breakpoint()
                    const previousPage = await billingAlertsEventsList(values.currentOrganization.id, props.alertId, {
                        limit: HISTORY_PAGE_SIZE,
                        offset: offsetFromPageLink(values.eventsPage.previous),
                    })
                    await breakpoint()
                    return previousPage
                },
            },
        ],
    })),
    selectors({
        currentPage: [
            (selectors) => [selectors.eventsPage],
            (eventsPage: PaginatedBillingAlertEventListApi): number =>
                eventsPage.previous ? offsetFromPageLink(eventsPage.previous) / HISTORY_PAGE_SIZE + 2 : 1,
        ],
    }),
    afterMount(({ actions }) => {
        actions.loadEvents(undefined)
    }),
])