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.

4.5 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, listeners, path, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import api from 'lib/api'
import { runWhenIdle } from 'lib/utils/async'
import type { HealthIssueSummary } from 'scenes/health/types'
import { teamLogic } from 'scenes/teamLogic'

const REFRESH_INTERVAL = 60 * 1000 * 5

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthSummaryLogicValues {
    currentTeamIdStrict: number | string // teamLogic
    criticalCount: number
    healthSummary: HealthIssueSummary | null
    healthSummaryLoading: boolean
    snoozedCount: number
    unsnoozedCount: number
    warningCount: number
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthSummaryLogicActions {
    loadHealthSummary: () => any
    loadHealthSummaryFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadHealthSummarySuccess: (
        healthSummary: HealthIssueSummary | null,
        payload?: any
    ) => {
        healthSummary: HealthIssueSummary | null
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthSummaryLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        unsnoozedCount: (healthSummary: HealthIssueSummary | null) => number
        snoozedCount: (healthSummary: HealthIssueSummary | null) => number
        criticalCount: (healthSummary: HealthIssueSummary | null) => number
        warningCount: (healthSummary: HealthIssueSummary | null) => number
    }
}

export type healthSummaryLogicType = MakeLogicType<
    healthSummaryLogicValues,
    healthSummaryLogicActions,
    Record<string, any>,
    healthSummaryLogicMeta
>

export const healthSummaryLogic = kea<healthSummaryLogicType>([
    path(['lib', 'components', 'HelpMenu', 'healthSummaryLogic']),
    connect(() => ({ values: [teamLogic, ['currentTeamIdStrict']] })),
    loaders(({ values }) => ({
        healthSummary: [
            null as HealthIssueSummary | null,
            {
                loadHealthSummary: async (): Promise<HealthIssueSummary | null> => {
                    try {
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                        return await api.get(`api/projects/${values.currentTeamIdStrict}/health_issues/summary/`)
                    } catch {
                        return null
                    }
                },
            },
        ],
    })),
    selectors({
        // The badge counts only unsnoozed issues. Snoozing is meant to quiet it without hiding
        // anything, so the snoozed group stays available for the copy around the badge.
        unsnoozedCount: [
            (s) => [s.healthSummary],
            (summary: HealthIssueSummary | null): number => summary?.unsnoozed?.total ?? 0,
        ],
        snoozedCount: [
            (s) => [s.healthSummary],
            (summary: HealthIssueSummary | null): number => summary?.snoozed?.total ?? 0,
        ],
        criticalCount: [
            (s) => [s.healthSummary],
            (summary: HealthIssueSummary | null): number => summary?.unsnoozed?.by_severity?.critical ?? 0,
        ],
        warningCount: [
            (s) => [s.healthSummary],
            (summary: HealthIssueSummary | null): number => summary?.unsnoozed?.by_severity?.warning ?? 0,
        ],
    }),
    listeners(({ actions, cache }) => ({
        loadHealthSummarySuccess: () => {
            cache.disposables.add(() => {
                const timerId = setTimeout(() => actions.loadHealthSummary(), REFRESH_INTERVAL)
                return () => clearTimeout(timerId)
            }, 'refreshTimeout')
        },
    })),
    afterMount(({ actions, cache }) => {
        // Only the help menu reads this data, so wait for an idle browser and let the page's own requests go first.
        // Disposables re-run setup when the tab shows again, so drop this one once it fires.
        // The refresh timer handles every load after the first.
        cache.disposables.add(
            () =>
                runWhenIdle(() => {
                    cache.disposables.dispose('idleLoad')
                    actions.loadHealthSummary()
                }),
            'idleLoad'
        )
    }),
])