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.7 KB
Raw Blame
import { ReactNode } from 'react'

import { IconCalendar, IconClock } from '@posthog/icons'
import { LemonBanner, LemonTag, Link } from '@posthog/lemon-ui'

import { AlertState } from '~/queries/schema/schema-general'

import { AlertType } from '../types'

interface AlertDefinitionRowProps {
    label?: ReactNode
    children: ReactNode
    className?: string
}

export function AlertDefinitionRow({ label, children, className }: AlertDefinitionRowProps): JSX.Element {
    return (
        <div className={`flex flex-wrap gap-x-3 gap-y-2 items-center${className ? ` ${className}` : ''}`}>
            {label ? <div>{label}</div> : null}
            {children}
        </div>
    )
}

export function AlertStateIndicator({ alert }: { alert: AlertType }): JSX.Element {
    if (!alert.enabled) {
        return <LemonTag type="muted">Disabled</LemonTag>
    }

    switch (alert.state) {
        case AlertState.FIRING:
            return <LemonTag type="danger">Firing</LemonTag>
        case AlertState.ERRORED:
            return <LemonTag type="danger">Errored</LemonTag>
        case AlertState.SNOOZED:
            return <LemonTag type="completion">Snoozed</LemonTag>
        case AlertState.NOT_FIRING:
            return <LemonTag type="success">Not firing</LemonTag>
    }
}

export function AlertErrorBanner({ alert }: { alert: AlertType }): JSX.Element | null {
    if (alert.state !== AlertState.ERRORED) {
        return null
    }

    const error = alert.checks?.find((check) => check.error?.message)?.error
    if (!error?.message) {
        return null
    }

    if (error.code === 'invalid_configuration') {
        return (
            <LemonBanner type="error" data-attr="alert-error-banner">
                <strong>Alert disabled.</strong> {error.message} Fix the alert or insight settings, then turn the alert
                back on.
            </LemonBanner>
        )
    }

    if (error.code !== 'email_unavailable') {
        return (
            <LemonBanner type="error" data-attr="alert-error-banner">
                <strong>{alert.enabled ? 'Alert error.' : 'Alert disabled.'}</strong> {error.message}
            </LemonBanner>
        )
    }

    return (
        <LemonBanner type="error" data-attr="alert-error-banner">
            <strong>Alert disabled.</strong> {error.message} To resume it,{' '}
            <Link to="https://posthog.com/docs/self-host/configure/email" target="_blank" targetBlankIcon>
                configure email settings
            </Link>
        </LemonBanner>
    )
}

interface AlertNextEvaluationStatusProps {
    children: ReactNode
    loading?: boolean
}

export function AlertNextEvaluationStatus({ children, loading = false }: AlertNextEvaluationStatusProps): JSX.Element {
    return (
        <div className="text-sm text-muted flex flex-wrap items-center gap-x-2 gap-y-0">
            <IconClock
                className={`size-4 shrink-0 text-muted motion-reduce:animate-none${loading ? ' animate-spin' : ''}`}
                aria-hidden
            />
            <span className="shrink-0">Next planned evaluation:</span>
            {children}
        </div>
    )
}

interface AlertTimezoneNoticeProps {
    timezone: string
    settingsUrl: string
}

export function AlertTimezoneNotice({ timezone, settingsUrl }: AlertTimezoneNoticeProps): JSX.Element {
    return (
        <div className="text-muted text-sm flex flex-wrap items-start gap-2">
            <IconCalendar className="size-4 shrink-0 text-muted mt-0.5" aria-hidden />
            <span className="min-w-0">
                Times use your project timezone ({timezone}).{' '}
                <Link to={settingsUrl} target="_blank" targetBlankIcon={false}>
                    Change in settings
                </Link>
            </span>
        </div>
    )
}