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.6 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'
import { Suspense, useEffect } from 'react'

import { LemonSkeleton, LemonTabs } from '@posthog/lemon-ui'

import { AccessDenied } from 'lib/components/AccessDenied'
import { lazyWithRetry } from 'lib/utils/retryImport'
import { SceneContentFallback } from 'scenes/SceneContentFallback'
import { urls } from 'scenes/urls'

import { SceneTitleSection } from '~/layout/scenes/components/SceneTitleSection'
import { AccessControlResourceType } from '~/types'

import { AlertType } from '../types'
import { AlertsTab, getActiveAlertsTab, getAlertsTabs, hasEffectiveResourceAccess } from '../utils'

const loadInsightAlerts = (): Promise<{ default: typeof import('./InsightAlerts').InsightAlerts }> =>
    import('./InsightAlerts').then((module) => ({ default: module.InsightAlerts }))
const loadLogsAlertingSection = (): Promise<{
    default: typeof import('products/logs/frontend/components/LogsAlerting/LogsAlertingSection').LogsAlertingSection
}> =>
    import('products/logs/frontend/components/LogsAlerting/LogsAlertingSection').then((module) => ({
        default: module.LogsAlertingSection,
    }))

const InsightAlerts = lazyWithRetry(loadInsightAlerts)
const LogsAlertingSection = lazyWithRetry(loadLogsAlertingSection)

interface AlertsProps {
    alertId: AlertType['id'] | null
}

const ALERTS_DESCRIPTION: Record<AlertsTab, string> = {
    [AlertsTab.INSIGHTS]: 'Monitor insight metrics and get notified when conditions are met.',
    [AlertsTab.LOGS]: 'Monitor matching logs and get notified when they cross a threshold.',
}

function AlertsPanelSkeleton(): JSX.Element {
    return (
        <div className="space-y-4 p-4">
            <LemonSkeleton className="h-10 w-full" />
            <LemonSkeleton className="h-32 w-full" />
            <LemonSkeleton className="h-32 w-full" />
        </div>
    )
}

export function Alerts({ alertId }: AlertsProps): JSX.Element {
    const { push } = useActions(router)
    const { searchParams } = useValues(router)
    const canViewInsightAlerts = hasEffectiveResourceAccess(AccessControlResourceType.Insight)
    const canViewLogAlerts = hasEffectiveResourceAccess(AccessControlResourceType.Logs)

    useEffect(() => {
        void loadInsightAlerts()
        void loadLogsAlertingSection()
    }, [])

    const activeTab = getActiveAlertsTab({
        alertId,
        requestedTab: typeof searchParams.alert_type === 'string' ? searchParams.alert_type : undefined,
        canViewInsightAlerts,
        canViewLogAlerts,
    })

    if (activeTab === null) {
        return <AccessDenied />
    }

    const tabs = getAlertsTabs({ canViewInsightAlerts, canViewLogAlerts })

    const switchTab = (tab: AlertsTab): void => {
        const nextSearchParams = { ...searchParams }
        delete nextSearchParams.alert_id
        nextSearchParams.alert_type = tab
        push(urls.alerts(), nextSearchParams)
    }

    return (
        <>
            <SceneTitleSection
                name="Alerts"
                description={ALERTS_DESCRIPTION[activeTab]}
                resourceType={{ type: 'inbox' }}
            />
            <LemonTabs<AlertsTab> activeKey={activeTab} onChange={switchTab} tabs={tabs} sceneInset />
            <Suspense
                fallback={
                    <SceneContentFallback>
                        <AlertsPanelSkeleton />
                    </SceneContentFallback>
                }
            >
                {activeTab === AlertsTab.LOGS ? <LogsAlertingSection /> : <InsightAlerts alertId={alertId} />}
            </Suspense>
        </>
    )
}