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.

6.6 KB
Raw Blame
import {
    ActivityLogItem,
    ActivityLogUserName,
    HumanizedChange,
    activityLogSummary,
    defaultDescriber,
} from 'lib/components/ActivityLog/humanizeActivity'
import { Link } from 'lib/lemon-ui/Link'
import { urls } from 'scenes/urls'

const formattedName = (name?: string | null): string | JSX.Element => {
    const displayName = name

    return <strong>{displayName}</strong>
}

const getContextDescription = (context: any): JSX.Element | null => {
    if (!context) {
        return null
    }

    if (context.insight_id && context.insight_short_id) {
        return (
            <>
                {' '}
                for insight{' '}
                <Link to={urls.insightView(context.insight_short_id)}>
                    {context.insight_name || context.insight_short_id}
                </Link>
            </>
        )
    }

    return null
}

export function alertConfigurationActivityDescriber(
    logItem: ActivityLogItem,
    asNotification?: boolean
): HumanizedChange {
    if (logItem.scope != 'AlertConfiguration') {
        console.error('alert configuration describer received a non-alert-configuration activity')
        return { description: null }
    }

    if (logItem.activity == 'created') {
        const contextDesc = getContextDescription(logItem?.detail?.context)

        if (logItem.detail?.type === 'alert_subscription_change') {
            return {
                summary: activityLogSummary(
                    logItem,
                    <>
                        Added subscriber {logItem.detail.context?.subscriber_name} (
                        {logItem.detail.context?.subscriber_email})
                    </>,
                    <>
                        {formattedName(logItem.detail.context?.alert_name)}
                        {contextDesc}
                    </>
                ),
                description: (
                    <>
                        <ActivityLogUserName logItem={logItem} /> added{' '}
                        <strong>
                            {logItem?.detail?.context?.subscriber_name} ({logItem?.detail?.context?.subscriber_email})
                        </strong>{' '}
                        as a subscriber for alert {formattedName(logItem?.detail?.context?.alert_name)}
                        {contextDesc}
                    </>
                ),
            }
        }

        return {
            summary: activityLogSummary(
                logItem,
                'Created the alert',
                <>
                    {formattedName(logItem.detail.name)}
                    {contextDesc}
                </>
            ),
            description: (
                <>
                    <ActivityLogUserName logItem={logItem} /> created the alert {formattedName(logItem?.detail.name)}
                    {contextDesc}
                </>
            ),
        }
    }

    if (logItem.activity == 'deleted') {
        const contextDesc = getContextDescription(logItem?.detail?.context)

        if (logItem.detail?.type === 'alert_subscription_change') {
            return {
                summary: activityLogSummary(
                    logItem,
                    <>
                        Removed subscriber {logItem.detail.context?.subscriber_name} (
                        {logItem.detail.context?.subscriber_email})
                    </>,
                    <>
                        {formattedName(logItem.detail.context?.alert_name)}
                        {contextDesc}
                    </>
                ),
                description: (
                    <>
                        <ActivityLogUserName logItem={logItem} /> removed{' '}
                        <strong>
                            {logItem?.detail?.context?.subscriber_name} ({logItem?.detail?.context?.subscriber_email})
                        </strong>{' '}
                        as a subscriber from alert {formattedName(logItem?.detail?.context?.alert_name)}
                        {contextDesc}
                    </>
                ),
            }
        }

        const displayName = logItem.detail.name || 'Alert Configuration'
        return {
            summary: activityLogSummary(
                logItem,
                'Deleted the alert',
                <>
                    {displayName}
                    {contextDesc}
                </>
            ),
            description: (
                <>
                    <ActivityLogUserName logItem={logItem} /> deleted the alert: {displayName}
                    {contextDesc}
                </>
            ),
        }
    }

    if (logItem.activity == 'took_ownership') {
        const contextDesc = getContextDescription(logItem?.detail?.context)
        return {
            summary: activityLogSummary(
                logItem,
                'Took ownership of the alert',
                <>
                    {formattedName(logItem.detail.name)}
                    {contextDesc}
                </>
            ),
            description: (
                <>
                    <ActivityLogUserName logItem={logItem} /> took ownership of the alert{' '}
                    {formattedName(logItem?.detail.name)}
                    {contextDesc}
                </>
            ),
        }
    }

    if (logItem.activity == 'updated') {
        const contextDesc = getContextDescription(logItem?.detail?.context)

        if (logItem.detail?.type === 'threshold_change') {
            return {
                summary: activityLogSummary(
                    logItem,
                    'Updated the threshold',
                    <>
                        {formattedName(logItem.detail.context?.alert_name)}
                        {contextDesc}
                    </>
                ),
                description: (
                    <>
                        <ActivityLogUserName logItem={logItem} /> updated the <strong>threshold</strong> for alert{' '}
                        {formattedName(logItem?.detail?.context?.alert_name)}
                        {contextDesc}
                    </>
                ),
            }
        }

        return {
            summary: activityLogSummary(
                logItem,
                'Updated the alert',
                <>
                    {formattedName(logItem.detail.name)}
                    {contextDesc}
                </>
            ),
            description: (
                <>
                    <ActivityLogUserName logItem={logItem} /> updated the alert {formattedName(logItem?.detail.name)}
                    {contextDesc}
                </>
            ),
        }
    }

    return defaultDescriber(logItem, asNotification, formattedName(logItem?.detail.name))
}