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

import { ApiConfig } from 'lib/api'

import { engineeringAnalyticsDeliveryComparison } from '../generated/api'
import type { DeliveryComparisonApi } from '../generated/api.schemas'
import { engineeringAnalyticsFiltersLogic } from './engineeringAnalyticsFiltersLogic'

export interface DeliveryComparisonLogicProps {
    author: string
    sourceId: string | null
    /** A pull request by the author: a team it asked to review is the team to compare with. */
    prNumber?: number
    repo?: string
    /** A fixed window. Without it the comparison follows the page's date filter. */
    window?: { dateFrom: string; dateTo: string | null }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface deliveryComparisonLogicValues {
    dateFrom: string | null // engineeringAnalyticsFiltersLogic
    dateTo: string | null // engineeringAnalyticsFiltersLogic
    comparison: DeliveryComparisonApi | null
    comparisonFailed: boolean
    comparisonLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface deliveryComparisonLogicActions {
    loadComparison: () => any
    loadComparisonFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadComparisonSuccess: (
        comparison: DeliveryComparisonApi,
        payload?: any
    ) => {
        comparison: DeliveryComparisonApi
        payload?: any
    }
}

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

export type deliveryComparisonLogicType = MakeLogicType<
    deliveryComparisonLogicValues,
    deliveryComparisonLogicActions,
    DeliveryComparisonLogicProps,
    deliveryComparisonLogicMeta
>

export const deliveryComparisonLogic = kea<deliveryComparisonLogicType>([
    path(['products', 'engineering_analytics', 'frontend', 'scenes', 'deliveryComparisonLogic']),
    props({} as DeliveryComparisonLogicProps),
    key((props) =>
        [
            props.author,
            props.sourceId ?? '',
            props.repo ?? '',
            props.prNumber ?? '',
            props.window?.dateFrom ?? '',
            props.window?.dateTo ?? '',
        ].join('@')
    ),

    connect(() => ({
        values: [engineeringAnalyticsFiltersLogic, ['dateFrom', 'dateTo']],
    })),

    loaders(({ props, values }) => ({
        comparison: [
            null as DeliveryComparisonApi | null,
            {
                loadComparison: async () =>
                    await engineeringAnalyticsDeliveryComparison(String(ApiConfig.getCurrentProjectId()), {
                        author: props.author,
                        pr_number: props.prNumber,
                        repo: props.repo,
                        date_from: (props.window ? props.window.dateFrom : values.dateFrom) ?? undefined,
                        date_to: (props.window ? props.window.dateTo : values.dateTo) ?? undefined,
                        source_id: props.sourceId ?? undefined,
                    }),
            },
        ],
    })),

    reducers({
        comparisonFailed: [false, { loadComparison: () => false, loadComparisonFailure: () => true }],
    }),

    listeners(({ actions, props }) => ({
        [engineeringAnalyticsFiltersLogic.actionTypes.setDateRange]: () => {
            if (!props.window) {
                actions.loadComparison()
            }
        },
    })),

    afterMount(({ actions }) => {
        actions.loadComparison()
    }),
])