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.

11.2 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import { router } from 'kea-router'

import api from 'lib/api'
import { healthSummaryLogic } from 'lib/components/HelpMenu/healthSummaryLogic'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'

import { Breadcrumb } from '~/types'

import { HEALTH_CATEGORY_CONFIG, kindsForCategory } from '../healthCategories'
import type { HealthIssueCategory } from '../healthCategories'
import type { CategoryConfig } from '../healthCategories'
import type { HealthIssuesResponse } from '../healthSceneLogic'
import { kindToLabel, worstSeverity as computeWorstSeverity } from '../healthUtils'
import type { HealthIssue, HealthIssueSeverity } from '../types'
import { CATEGORY_DETAIL_CONFIG } from './categoryDetailConfig'
import type { CategoryDetailConfig } from './categoryDetailConfig'

export interface HealthCategoryDetailLogicProps {
    category: string
}

export interface KindGroup {
    kind: string
    label: string
    issues: HealthIssue[]
    worstSeverity: HealthIssueSeverity
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthCategoryDetailLogicValues {
    currentTeamIdStrict: number | string // teamLogic
    breadcrumbs: Breadcrumb[]
    category: string
    categoryConfig: CategoryConfig
    categoryIssues: HealthIssue[]
    detailConfig: CategoryDetailConfig | undefined
    healthIssues: HealthIssuesResponse | null
    healthIssuesLoading: boolean
    isValidCategory: boolean
    issuesByKind: KindGroup[]
    showDismissed: boolean
    statusSummary: {
        count: number
        isHealthy: boolean
        worstSeverity: HealthIssueSeverity | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthCategoryDetailLogicActions {
    dismissIssue: (id: string) => {
        id: string
    }
    loadHealthIssues: () => any
    loadHealthIssuesFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadHealthIssuesSuccess: (
        healthIssues: HealthIssuesResponse | null,
        payload?: any
    ) => {
        healthIssues: HealthIssuesResponse | null
        payload?: any
    }
    refreshHealthData: () => {
        value: true
    }
    setShowDismissed: (show: boolean) => {
        show: boolean
    }
    snoozeIssue: (
        id: string,
        duration: string
    ) => {
        duration: string
        id: string
    }
    undismissIssue: (id: string) => {
        id: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface healthCategoryDetailLogicMeta {
    key: string
    __keaTypeGenInternalSelectorTypes: {
        category: (arg: any) => string
        isValidCategory: (category: string) => boolean
        categoryConfig: (category: string) => CategoryConfig
        detailConfig: (category: string) => CategoryDetailConfig | undefined
        categoryIssues: (healthIssues: HealthIssuesResponse | null, category: string) => HealthIssue[]
        issuesByKind: (categoryIssues: HealthIssue[]) => KindGroup[]
        statusSummary: (categoryIssues: HealthIssue[]) => {
            count: number
            isHealthy: boolean
            worstSeverity: HealthIssueSeverity | null
        }
        breadcrumbs: (categoryConfig: CategoryConfig) => Breadcrumb[]
    }
}

export type healthCategoryDetailLogicType = MakeLogicType<
    healthCategoryDetailLogicValues,
    healthCategoryDetailLogicActions,
    HealthCategoryDetailLogicProps,
    healthCategoryDetailLogicMeta
>

export const healthCategoryDetailLogic = kea<healthCategoryDetailLogicType>([
    path((key) => ['scenes', 'health', 'categoryDetail', 'healthCategoryDetailLogic', key]),
    props({} as HealthCategoryDetailLogicProps),
    key((props) => props.category),

    connect({
        values: [teamLogic, ['currentTeamIdStrict']],
    }),

    actions({
        setShowDismissed: (show: boolean) => ({ show }),
        snoozeIssue: (id: string, duration: string) => ({ id, duration }),
        dismissIssue: (id: string) => ({ id }),
        undismissIssue: (id: string) => ({ id }),
        refreshHealthData: true,
    }),

    reducers({
        showDismissed: [
            false,
            {
                setShowDismissed: (_, { show }) => show,
            },
        ],
    }),

    loaders(({ values }) => ({
        healthIssues: [
            null as HealthIssuesResponse | null,
            {
                loadHealthIssues: async (): Promise<HealthIssuesResponse | null> => {
                    const params: Record<string, string> = { status: 'active' }
                    if (!values.showDismissed) {
                        params.dismissed = 'false'
                    }

                    const queryString = new URLSearchParams(params).toString()
                    const url = `api/projects/${values.currentTeamIdStrict}/health_issues/?${queryString}`
                    try {
                        // nosemgrep: prefer-codegen-api -- Legacy raw API call with a URL built at runtime and an unchecked response type. Use a generated function if one covers this endpoint.
                        return await api.get(url)
                    } catch {
                        lemonToast.error('Failed to load health issues')
                        return values.healthIssues
                    }
                },
            },
        ],
    })),

    selectors({
        category: [() => [(_, props) => props.category], (category: string): string => category],

        isValidCategory: [(s) => [s.category], (category: string): boolean => category in HEALTH_CATEGORY_CONFIG],

        categoryConfig: [
            (s) => [s.category],
            (category: string) => HEALTH_CATEGORY_CONFIG[category as HealthIssueCategory],
        ],

        detailConfig: [
            (s) => [s.category],
            (category: string) => CATEGORY_DETAIL_CONFIG[category as HealthIssueCategory],
        ],

        categoryIssues: [
            (s) => [s.healthIssues, s.category],
            (healthIssues: HealthIssuesResponse | null, category: string): HealthIssue[] => {
                if (!healthIssues) {
                    return []
                }
                const kinds: Set<string> = new Set(kindsForCategory(category as HealthIssueCategory))
                return healthIssues.results.filter((issue) => kinds.has(issue.kind))
            },
        ],

        issuesByKind: [
            (s) => [s.categoryIssues],
            (categoryIssues: HealthIssue[]): KindGroup[] => {
                const grouped: Record<string, HealthIssue[]> = {}
                for (const issue of categoryIssues) {
                    if (!grouped[issue.kind]) {
                        grouped[issue.kind] = []
                    }
                    grouped[issue.kind].push(issue)
                }

                return Object.entries(grouped).map(([kind, issues]) => ({
                    kind,
                    label: kindToLabel(kind),
                    issues,
                    worstSeverity: computeWorstSeverity(issues),
                }))
            },
        ],

        statusSummary: [
            (s) => [s.categoryIssues],
            (
                categoryIssues: HealthIssue[]
            ): { count: number; worstSeverity: HealthIssueSeverity | null; isHealthy: boolean } => {
                if (categoryIssues.length === 0) {
                    return { count: 0, worstSeverity: null, isHealthy: true }
                }
                return {
                    count: categoryIssues.length,
                    worstSeverity: computeWorstSeverity(categoryIssues),
                    isHealthy: false,
                }
            },
        ],

        breadcrumbs: [
            (s) => [s.categoryConfig],
            (categoryConfig: { label: string; description: string } | undefined): Breadcrumb[] => [
                {
                    key: Scene.Health,
                    name: 'Health',
                    path: urls.health(),
                },
                {
                    key: Scene.HealthCategoryDetail,
                    name: categoryConfig?.label ?? 'Detail',
                },
            ],
        ],
    }),

    listeners(({ actions, values }) => ({
        refreshHealthData: () => {
            actions.loadHealthIssues()
        },
        setShowDismissed: () => {
            actions.loadHealthIssues()
        },
        snoozeIssue: async ({ id, duration }) => {
            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.
                await api.update(`api/projects/${values.currentTeamIdStrict}/health_issues/${id}/`, {
                    snoozed_until: duration,
                })
                actions.loadHealthIssues()
                healthSummaryLogic.actions.loadHealthSummary()
            } catch {
                lemonToast.error("Couldn't snooze this issue. Try again.")
            }
        },
        dismissIssue: async ({ id }) => {
            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.
                await api.update(`api/projects/${values.currentTeamIdStrict}/health_issues/${id}/`, {
                    dismissed: true,
                })
                actions.loadHealthIssues()
                healthSummaryLogic.actions.loadHealthSummary()
            } catch {
                lemonToast.error('Failed to dismiss issue')
            }
        },
        undismissIssue: async ({ id }) => {
            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.
                await api.update(`api/projects/${values.currentTeamIdStrict}/health_issues/${id}/`, {
                    dismissed: false,
                })
                actions.loadHealthIssues()
                healthSummaryLogic.actions.loadHealthSummary()
            } catch {
                lemonToast.error('Failed to undismiss issue')
            }
        },
    })),

    afterMount(({ actions, props: logicProps }) => {
        if (!(logicProps.category in HEALTH_CATEGORY_CONFIG)) {
            router.actions.replace(urls.health())
            return
        }
        const redirectUrl = CATEGORY_DETAIL_CONFIG[logicProps.category as HealthIssueCategory]?.redirectUrl
        if (redirectUrl) {
            router.actions.replace(redirectUrl)
            return
        }
        actions.loadHealthIssues()
    }),
])