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

import { signalsSourceSpend } from 'products/signals/frontend/generated/api'
import type { SourceSpendApi } from 'products/signals/frontend/generated/api.schemas'

export interface SourceSpendLogicProps {
    teamId: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sourceSpendLogicValues {
    failed: boolean
    sources: SourceSpendApi[] | null
    sourcesLoading: boolean
    spendByProduct: Map<string, number | null>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sourceSpendLogicActions {
    loadSources: () => any
    loadSourcesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSourcesSuccess: (
        sources: SourceSpendApi[],
        payload?: any
    ) => {
        sources: SourceSpendApi[]
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface sourceSpendLogicMeta {
    key: number
    __keaTypeGenInternalSelectorTypes: {
        spendByProduct: (sources: SourceSpendApi[] | null) => Map<string, number | null>
    }
}

export type sourceSpendLogicType = MakeLogicType<
    sourceSpendLogicValues,
    sourceSpendLogicActions,
    SourceSpendLogicProps,
    sourceSpendLogicMeta
>

export const sourceSpendLogic = kea<sourceSpendLogicType>([
    props({} as SourceSpendLogicProps),
    key((props) => props.teamId),
    path((key) => ['products', 'signals', 'sourceSpendLogic', key]),
    loaders(({ props }) => ({
        sources: [
            null as SourceSpendApi[] | null,
            { loadSources: async () => signalsSourceSpend(String(props.teamId)) },
        ],
    })),
    reducers({
        failed: [false, { loadSources: () => false, loadSourcesSuccess: () => false, loadSourcesFailure: () => true }],
    }),
    selectors({
        spendByProduct: [
            (s) => [s.sources],
            (sources: SourceSpendApi[] | null): Map<string, number | null> => {
                const totals = new Map<string, number | null>()
                for (const source of sources ?? []) {
                    const previous = totals.get(source.source_product)
                    const amount = source.spend.total_spend
                    totals.set(
                        source.source_product,
                        previous == null && amount == null ? null : (previous ?? 0) + (amount ?? 0)
                    )
                }
                return totals
            },
        ],
    }),
    afterMount(({ actions }) => actions.loadSources()),
])