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

import { organizationLogic } from 'scenes/organizationLogic'

import type { OrganizationType } from '~/types'

import { teamsDataFreshnessRetrieve } from 'products/platform_features/frontend/generated/api'
import type {
    DataFreshnessProjectApi,
    OrganizationDataFreshnessApi,
} from 'products/platform_features/frontend/generated/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectDataFreshnessLogicValues {
    currentOrganization: OrganizationType | null // organizationLogic
    dataFreshness: OrganizationDataFreshnessApi | null
    dataFreshnessLoading: boolean
    freshnessByTeamId: Record<number, DataFreshnessProjectApi>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectDataFreshnessLogicActions {
    loadDataFreshness: () => any
    loadDataFreshnessFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadDataFreshnessSuccess: (
        dataFreshness: OrganizationDataFreshnessApi,
        payload?: any
    ) => {
        dataFreshness: OrganizationDataFreshnessApi
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface projectDataFreshnessLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        freshnessByTeamId: (
            dataFreshness: OrganizationDataFreshnessApi | null
        ) => Record<number, DataFreshnessProjectApi>
    }
}

export type projectDataFreshnessLogicType = MakeLogicType<
    projectDataFreshnessLogicValues,
    projectDataFreshnessLogicActions,
    Record<string, any>,
    projectDataFreshnessLogicMeta
>

export const projectDataFreshnessLogic = kea<projectDataFreshnessLogicType>([
    path(['lib', 'components', 'Account', 'projectDataFreshnessLogic']),
    connect(() => ({
        values: [organizationLogic, ['currentOrganization']],
    })),
    loaders(({ values }) => ({
        dataFreshness: [
            null as OrganizationDataFreshnessApi | null,
            {
                loadDataFreshness: async (): Promise<OrganizationDataFreshnessApi> =>
                    await teamsDataFreshnessRetrieve(values.currentOrganization!.id),
            },
        ],
    })),
    selectors({
        freshnessByTeamId: [
            (s) => [s.dataFreshness],
            (dataFreshness: OrganizationDataFreshnessApi | null): Record<number, DataFreshnessProjectApi> =>
                Object.fromEntries((dataFreshness?.results ?? []).map((result) => [result.team_id, result])),
        ],
    }),
    listeners(({ actions }) => ({
        // The switcher can mount before the organization has loaded, and the endpoint is
        // organization-scoped, so the load is gated on whichever of the two lands last.
        [organizationLogic.actionTypes.loadCurrentOrganizationSuccess]: () => {
            actions.loadDataFreshness()
        },
    })),
    afterMount(({ actions, values }) => {
        if (values.currentOrganization) {
            actions.loadDataFreshness()
        }
    }),
])