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

import { signalsSpendOverview } from 'products/signals/frontend/generated/api'
import type { SpendOverviewApi } from 'products/signals/frontend/generated/api.schemas'

export interface SelfDrivingSpendLogicProps {
    teamId: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface selfDrivingSpendLogicValues {
    averageSpendByScout: Map<string, number | null>
    failed: boolean
    overview: SpendOverviewApi | null
    overviewLoading: boolean
    spendByScout: Map<string, number | null>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface selfDrivingSpendLogicActions {
    loadOverview: () => any
    loadOverviewFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadOverviewSuccess: (
        overview: SpendOverviewApi,
        payload?: any
    ) => {
        overview: SpendOverviewApi
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface selfDrivingSpendLogicMeta {
    key: number
    __keaTypeGenInternalSelectorTypes: {
        averageSpendByScout: (overview: SpendOverviewApi | null) => Map<string, number | null>
        spendByScout: (overview: SpendOverviewApi | null) => Map<string, number | null>
    }
}

export type selfDrivingSpendLogicType = MakeLogicType<
    selfDrivingSpendLogicValues,
    selfDrivingSpendLogicActions,
    SelfDrivingSpendLogicProps,
    selfDrivingSpendLogicMeta
>

export const selfDrivingSpendLogic = kea<selfDrivingSpendLogicType>([
    props({} as SelfDrivingSpendLogicProps),
    key((props) => props.teamId),
    path((key) => ['products', 'signals', 'selfDrivingSpendLogic', key]),
    loaders(({ props }) => ({
        overview: [
            null as SpendOverviewApi | null,
            { loadOverview: async () => signalsSpendOverview(String(props.teamId)) },
        ],
    })),
    reducers({
        failed: [
            false,
            { loadOverview: () => false, loadOverviewSuccess: () => false, loadOverviewFailure: () => true },
        ],
    }),
    selectors({
        averageSpendByScout: [
            (s) => [s.overview],
            (overview: SpendOverviewApi | null): Map<string, number | null> =>
                new Map(overview?.scouts.map((scout) => [scout.skill_name, scout.average_spend])),
        ],
        spendByScout: [
            (s) => [s.overview],
            (overview: SpendOverviewApi | null) =>
                new Map(overview?.scouts.map((scout) => [scout.skill_name, scout.spend.total_spend])),
        ],
    }),
    afterMount(({ actions }) => actions.loadOverview()),
])