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.

8.1 KB
Raw Blame
import { useActions, useValues } from 'kea'
import { router } from 'kea-router'

import { Link } from '@posthog/lemon-ui'

import { ProductIntroduction } from 'lib/components/ProductIntroduction/ProductIntroduction'
import { UserActivityIndicator } from 'lib/components/UserActivityIndicator/UserActivityIndicator'
import { FEATURE_FLAGS } from 'lib/constants'
import { LemonButton } from 'lib/lemon-ui/LemonButton'
import { LemonModal } from 'lib/lemon-ui/LemonModal'
import { Spinner } from 'lib/lemon-ui/Spinner'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { pluralize } from 'lib/utils/strings'
import { urls } from 'scenes/urls'

import { InsightShortId } from '~/types'

import { AlertStateIndicator } from '../components/AlertDefinition'
import { buildAlertSummary } from '../components/alertSummary'
import { AlertSummaryBanner } from '../components/AlertSummaryBanner'
import { InsightAlertsLogicProps, alertsUnsupportedReason, insightAlertsLogic } from '../logic/insightAlertsLogic'
import { AlertType } from '../types'
import { InsightAlertPreview } from './InsightAlertPreview'

interface AlertListItemProps {
    alert: AlertType
    destinationCount?: number
    destinationsLoading?: boolean
    onClick: () => void
}

export function AlertListItem({
    alert,
    destinationCount = 0,
    destinationsLoading = false,
    onClick,
}: AlertListItemProps): JSX.Element {
    const summary = buildAlertSummary(alert, alert.subscribed_users?.length ?? 0, destinationCount)
    if (destinationsLoading) {
        summary.notifies = 'Loading…'
    }

    return (
        <LemonButton onClick={onClick} data-attr="alert-list-item" fullWidth>
            <AlertSummaryBanner
                summary={summary}
                header={
                    <div className="flex items-center justify-between gap-3">
                        <span className="min-w-0 truncate">{alert.name}</span>
                        <AlertStateIndicator alert={alert} />
                    </div>
                }
                footer={<UserActivityIndicator prefix="Created" at={alert.created_at} by={alert.created_by} />}
            />
        </LemonButton>
    )
}

interface ManageAlertsModalProps extends InsightAlertsLogicProps {
    isOpen: boolean
    insightShortId: InsightShortId
    canCreateAlertForInsight: boolean
    /** The insight's query, so the unsupported-reason copy can be specific (e.g. time-to-convert funnels). */
    insightQuery?: Record<string, any> | null
    onClose?: () => void
    onCreateAlert?: () => void
    onEditAlert?: (alertId: AlertType['id']) => void
}

export function ManageAlertsModal(props: ManageAlertsModalProps): JSX.Element {
    const { push } = useActions(router)
    const logic = insightAlertsLogic(props)

    const { alerts, alertsLoading, alertDestinationCounts, alertDestinationCountsLoading } = useValues(logic)

    const { featureFlags, receivedFeatureFlags } = useValues(featureFlagLogic)
    const isEmpty = alerts.length === 0
    const isExperimentEligible = props.isOpen && !alertsLoading && isEmpty && props.canCreateAlertForInsight
    const showListSpinner = props.isOpen && alertsLoading
    const showVisualEmptyState =
        isExperimentEligible &&
        receivedFeatureFlags &&
        featureFlags[FEATURE_FLAGS.INSIGHT_ALERTS_EMPTY_STATE_EXPERIMENT] === 'test'
    const createAlertDisabledReason = !props.canCreateAlertForInsight
        ? alertsUnsupportedReason({}, props.insightQuery)
        : undefined
    const openAlert = (alertId: AlertType['id']): void => {
        if (props.onEditAlert) {
            props.onClose?.()
            props.onEditAlert(alertId)
            return
        }

        push(urls.insightAlert(props.insightShortId, alertId))
    }
    const createAlert = (): void => {
        if (props.onCreateAlert) {
            props.onClose?.()
            props.onCreateAlert()
            return
        }

        push(urls.insightAlert(props.insightShortId, 'new'))
    }

    return (
        <LemonModal onClose={props.onClose} isOpen={props.isOpen} width={600} simple title="">
            <LemonModal.Header>
                <h3 className="!m-0">Manage alerts</h3>
            </LemonModal.Header>
            <LemonModal.Content>
                {!showVisualEmptyState && (
                    <div className="mb-4">
                        With alerts, PostHog monitors your insight at a recurring interval and notifies you when
                        conditions are met.
                        <br />
                        <Link to={urls.alerts()} target="_blank">
                            View all your alerts here
                        </Link>
                    </div>
                )}

                {showListSpinner ? (
                    <div className="flex justify-center p-8">
                        <Spinner />
                    </div>
                ) : null}
                {!showListSpinner && alerts.length > 0 && (
                    <div className="deprecated-space-y-2">
                        <div>
                            <strong>{alerts?.length}</strong> {pluralize(alerts.length || 0, 'alert', 'alerts', false)}
                        </div>

                        {alerts.map((alert) => (
                            <AlertListItem
                                key={alert.id}
                                alert={alert}
                                destinationCount={alertDestinationCounts[alert.id] ?? 0}
                                destinationsLoading={alertDestinationCountsLoading}
                                onClick={() => openAlert(alert.id)}
                            />
                        ))}
                    </div>
                )}
                {!showListSpinner && isEmpty && !showVisualEmptyState && (
                    <div className="flex flex-col p-4 items-center text-center">
                        <h3>There are no alerts for this insight</h3>
                        <p>Once alerts are created they will display here.</p>
                    </div>
                )}
                {showVisualEmptyState && (
                    <ProductIntroduction
                        thingName="alert"
                        titleOverride="Get notified when it matters"
                        description="Catch drops in signups, conversion, or usage."
                        customHog={InsightAlertPreview}
                        hogLayout="vertical"
                        hogClassName="w-full sm:w-full lg:w-full max-w-96 mb-0"
                        className="border-0 p-0 m-0"
                        contentClassName="max-w-full"
                        actionElementOverride={
                            <LemonButton
                                type="primary"
                                onClick={createAlert}
                                disabledReason={createAlertDisabledReason}
                                data-attr="insight-empty-state-new-alert"
                            >
                                New alert
                            </LemonButton>
                        }
                        docsURL="https://posthog.com/docs/alerts"
                    />
                )}
            </LemonModal.Content>

            <LemonModal.Footer>
                {showVisualEmptyState && (
                    <LemonButton type="tertiary" to={urls.alerts()} targetBlank className="mr-auto">
                        View all alerts
                    </LemonButton>
                )}
                <LemonButton type="secondary" onClick={props.onClose}>
                    Close
                </LemonButton>
                {!showVisualEmptyState && (
                    <LemonButton
                        type="primary"
                        onClick={createAlert}
                        disabledReason={createAlertDisabledReason}
                        loading={showListSpinner}
                        data-attr={isEmpty ? 'insight-empty-state-new-alert' : undefined}
                    >
                        New alert
                    </LemonButton>
                )}
            </LemonModal.Footer>
        </LemonModal>
    )
}