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.

7.0 KB
Raw Blame
import { MakeLogicType, connect, kea, path, selectors } from 'kea'

import { pendingInvitesLogic } from 'lib/components/Account/pendingInvitesLogic'
import { healthSummaryLabel } from 'lib/components/HelpMenu/healthSummaryLabel'
import { healthSummaryLogic } from 'lib/components/HelpMenu/healthSummaryLogic'
import { helpMenuLogic } from 'lib/components/HelpMenu/helpMenuLogic'
import { posthogStatusLogic } from 'lib/components/HelpMenu/posthogStatusLogic'
import { BillingAlertConfig, billingLogic } from 'lib/logic/billing/billingLogic'
import { preflightLogic } from 'lib/logic/preflightLogic'

import type { HelpBadgeStatus } from '../../lib/components/HelpMenu/helpMenuLogic'
import type { PostHogStatusBadgeStatus } from '../../lib/components/HelpMenu/posthogStatusLogic'
import type { HealthIssueSummary } from '../../scenes/health/types'
import type { PreflightStatus } from '../../types'
import {
    TodayRailSeverity,
    severityFromBadgeStatus,
    severityFromBillingAlert,
    worstSeverity,
} from './todayRailSeverity'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayRailStatusLogicValues {
    effectiveBillingAlert: BillingAlertConfig | null // billingLogic
    healthSummary: HealthIssueSummary | null // healthSummaryLogic
    snoozedCount: number // healthSummaryLogic
    unsnoozedCount: number // healthSummaryLogic
    triggerBadgeStatus: HelpBadgeStatus // helpMenuLogic
    pendingInvitesLabel: string | null // pendingInvitesLogic
    postHogStatusBadgeStatus: PostHogStatusBadgeStatus // posthogStatusLogic
    postHogStatusTooltip: string | null // posthogStatusLogic
    preflight: PreflightStatus | null // preflightLogic
    billingSeverity: TodayRailSeverity
    billingText: string | null
    healthSeverity: TodayRailSeverity
    healthText: string | null
    moreLabel: string
    moreSeverity: TodayRailSeverity
    orgProblems: string[]
    orgSeverity: TodayRailSeverity
    statusSeverity: TodayRailSeverity
    statusText: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayRailStatusLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        statusSeverity: (postHogStatusBadgeStatus: PostHogStatusBadgeStatus) => TodayRailSeverity
        statusText: (postHogStatusTooltip: string | null, preflight: PreflightStatus | null) => string | null
        healthSeverity: (triggerBadgeStatus: HelpBadgeStatus) => TodayRailSeverity
        healthText: (
            healthSummary: HealthIssueSummary | null,
            unsnoozedCount: number,
            snoozedCount: number
        ) => string | null
        moreSeverity: (statusSeverity: TodayRailSeverity, healthSeverity: TodayRailSeverity) => TodayRailSeverity
        moreLabel: (
            statusSeverity: TodayRailSeverity,
            statusText: string | null,
            healthSeverity: TodayRailSeverity,
            healthText: string | null
        ) => string
        billingSeverity: (effectiveBillingAlert: BillingAlertConfig | null) => TodayRailSeverity
        billingText: (effectiveBillingAlert: BillingAlertConfig | null) => string | null
        orgSeverity: (billingSeverity: TodayRailSeverity, pendingInvitesLabel: string | null) => TodayRailSeverity
        orgProblems: (
            billingSeverity: TodayRailSeverity,
            billingText: string | null,
            pendingInvitesLabel: string | null
        ) => string[]
    }
}

export type todayRailStatusLogicType = MakeLogicType<
    todayRailStatusLogicValues,
    {},
    Record<string, any>,
    todayRailStatusLogicMeta
>

export const todayRailStatusLogic = kea<todayRailStatusLogicType>([
    path(['layout', 'today', 'todayRailStatusLogic']),
    connect(() => ({
        values: [
            posthogStatusLogic,
            ['postHogStatusTooltip', 'postHogStatusBadgeStatus'],
            helpMenuLogic,
            ['triggerBadgeStatus'],
            healthSummaryLogic,
            ['healthSummary', 'unsnoozedCount', 'snoozedCount'],
            preflightLogic,
            ['preflight'],
            billingLogic,
            ['effectiveBillingAlert'],
            pendingInvitesLogic,
            ['pendingInvitesLabel'],
        ],
    })),
    selectors({
        statusSeverity: [
            (s) => [s.postHogStatusBadgeStatus],
            (status: PostHogStatusBadgeStatus): TodayRailSeverity => severityFromBadgeStatus(status),
        ],
        statusText: [
            (s) => [s.postHogStatusTooltip, s.preflight],
            // Local dev never loads the status page summary, so it has no text to show.
            (tooltip: string | null, preflight: PreflightStatus | null): string | null =>
                tooltip ?? (preflight?.is_debug ? 'All systems operational' : null),
        ],
        healthSeverity: [
            (s) => [s.triggerBadgeStatus],
            (status: HelpBadgeStatus): TodayRailSeverity => severityFromBadgeStatus(status),
        ],
        healthText: [
            (s) => [s.healthSummary, s.unsnoozedCount, s.snoozedCount],
            (healthSummary: HealthIssueSummary | null, unsnoozedCount: number, snoozedCount: number): string | null =>
                healthSummary ? healthSummaryLabel(unsnoozedCount, snoozedCount) : null,
        ],
        moreSeverity: [
            (s) => [s.statusSeverity, s.healthSeverity],
            (statusSeverity: TodayRailSeverity, healthSeverity: TodayRailSeverity): TodayRailSeverity =>
                worstSeverity(statusSeverity, healthSeverity),
        ],
        moreLabel: [
            (s) => [s.statusSeverity, s.statusText, s.healthSeverity, s.healthText],
            (
                statusSeverity: TodayRailSeverity,
                statusText: string | null,
                healthSeverity: TodayRailSeverity,
                healthText: string | null
            ): string =>
                ['More', statusSeverity !== 'ok' && statusText, healthSeverity !== 'ok' && healthText]
                    .filter(Boolean)
                    .join(', '),
        ],
        billingSeverity: [
            (s) => [s.effectiveBillingAlert],
            (alert: BillingAlertConfig | null): TodayRailSeverity => severityFromBillingAlert(alert),
        ],
        billingText: [
            (s) => [s.effectiveBillingAlert],
            (alert: BillingAlertConfig | null): string | null => alert?.title ?? null,
        ],
        orgSeverity: [
            (s) => [s.billingSeverity, s.pendingInvitesLabel],
            (billingSeverity: TodayRailSeverity, pendingInvitesLabel: string | null): TodayRailSeverity =>
                worstSeverity(billingSeverity, pendingInvitesLabel ? 'notice' : 'none'),
        ],
        orgProblems: [
            (s) => [s.billingSeverity, s.billingText, s.pendingInvitesLabel],
            (
                billingSeverity: TodayRailSeverity,
                billingText: string | null,
                pendingInvitesLabel: string | null
            ): string[] =>
                [billingSeverity !== 'none' && billingText, pendingInvitesLabel].filter(
                    (problem): problem is string => !!problem
                ),
        ],
    }),
])