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.

2.4 KB
Raw Blame
import { useValues } from 'kea'

import { appMetricsLogic } from 'lib/components/AppMetrics/appMetricsLogic'
import { AppMetricsTrends } from 'lib/components/AppMetrics/AppMetricsTrends'

import { METRIC_COLORS } from '../Workflows/workflowMetricsSummaryLogic'
import { BroadcastPerformanceLogicProps, sendActivityParams } from './broadcastPerformanceLogic'

const METRIC_LABELS: Record<string, string> = {
    email_sent: 'Sent',
    email_delivered: 'Delivered',
    email_opened: 'Opened',
    email_link_clicked: 'Link clicked',
    push_sent: 'Sent',
    push_opened: 'Opened',
}

const SERIES_COLORS = Object.fromEntries(
    Object.entries(METRIC_LABELS).map(([metricName, label]) => [metricName, METRIC_COLORS[label]])
)

export function BroadcastSendActivity({
    runId,
    runStartedAt,
    channel,
    hideSingleBucket = false,
}: BroadcastPerformanceLogicProps & {
    hideSingleBucket?: boolean
}): JSX.Element | null {
    const { appMetricsTrends, appMetricsTrendsLoading } = useValues(
        appMetricsLogic({
            logicKey: `broadcast-send-activity-${runId}`,
            loadOnMount: true,
            loadOnChanges: true,
            loadPreviousPeriod: false,
            forceParams: sendActivityParams(runId, runStartedAt, undefined, channel),
        })
    )

    const hasActivity = !!appMetricsTrends?.series.some((series) => series.values.some((value) => value > 0))
    const activeBuckets = new Set(
        appMetricsTrends?.series.flatMap((series) =>
            series.values.flatMap((value, index) => (value > 0 ? [index] : []))
        )
    )
    if (hideSingleBucket && !appMetricsTrendsLoading && activeBuckets.size <= 1) {
        return null
    }

    return (
        <div className="flex flex-col gap-2">
            <h3 className="m-0 text-sm font-semibold">Activity over time</h3>
            {!appMetricsTrendsLoading && appMetricsTrends && !hasActivity ? (
                <span className="text-muted">
                    No {channel === 'push' ? 'push' : 'email'} activity recorded for this send yet.
                </span>
            ) : (
                <AppMetricsTrends
                    appMetricsTrends={appMetricsTrends}
                    loading={appMetricsTrendsLoading}
                    metricLabels={METRIC_LABELS}
                    seriesColors={SERIES_COLORS}
                    className="h-80"
                />
            )}
        </div>
    )
}