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.

5.2 KB
Raw Blame
import { useValues } from 'kea'
import { router } from 'kea-router'

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

import type { ReplayObservationApi } from '../generated/api.schemas'
import { POSTHOG_AI_ORIGIN, currentReturnPath, observationFromOriginUrl } from '../utils/breadcrumbs'
import { flattenMarkdownToLine } from '../utils/markdown'
import { readErrorMessage, readReasoning, readSummary, readTitle } from '../utils/observation'
import { replayVisionScanWidgetLogic } from './replayVisionScanWidgetLogic'

export interface ReplayVisionScanWidgetProps {
    scanId: string
    sessionIds: string[]
    /** Sessions the scan did not start, with the reason, so the user is not left waiting on them. */
    skipped: { sessionId: string; reason: string }[]
}

const SKIP_MESSAGES: Record<string, string> = {
    skipped_quota: "this project's monthly Replay Vision credits are used up",
    skipped_scanner_limit: 'this scanner reached its own credit limit',
    skipped_limit: 'too many scans were already running',
    no_recording: 'the recording could not be found',
    failed: 'the scan could not be started',
}

export function ReplayVisionScanWidget({ scanId, sessionIds, skipped }: ReplayVisionScanWidgetProps): JSX.Element {
    const { gaveUp, latestPerSession, pendingCount } = useValues(replayVisionScanWidgetLogic({ scanId, sessionIds }))
    // Read once per widget, not per row; the thread can stay open in the side panel across scenes.
    const { location } = useValues(router)
    const returnPath = currentReturnPath(location)
    const skippedByReason = Object.entries(
        skipped.reduce<Record<string, number>>((counts, entry) => {
            counts[entry.reason] = (counts[entry.reason] ?? 0) + 1
            return counts
        }, {})
    )

    return (
        <div className="overflow-hidden rounded border bg-surface-primary">
            <div className="flex items-center justify-between gap-2 border-b px-3 py-2">
                <span className="text-sm font-semibold">
                    {pendingCount === 0
                        ? 'Scan complete'
                        : gaveUp
                          ? `${pendingCount} recording${pendingCount === 1 ? '' : 's'} still running`
                          : `Watching ${pendingCount} of ${sessionIds.length} recordings`}
                </span>
                {pendingCount > 0 && !gaveUp && <Spinner />}
            </div>

            {skippedByReason.length > 0 && (
                <LemonBanner type="warning" className="m-3">
                    {skippedByReason.map(([reason, count]) => (
                        <p key={reason} className="m-0">
                            {count} recording{count === 1 ? ' was' : 's were'} not scanned because{' '}
                            {SKIP_MESSAGES[reason] ?? 'the scan could not be started'}.
                        </p>
                    ))}
                </LemonBanner>
            )}

            <div className="divide-y">
                {latestPerSession.map((observation) => (
                    <ObservationRow key={observation.id} observation={observation} returnPath={returnPath} />
                ))}
                {pendingCount > 0 && gaveUp && (
                    <p className="m-0 px-3 py-3 text-sm text-secondary">
                        These are taking longer than usual. Their results will appear on the recordings when they
                        finish.
                    </p>
                )}
                {latestPerSession.length === 0 && pendingCount > 0 && !gaveUp && (
                    <p className="m-0 px-3 py-3 text-sm text-secondary">Starting the scans...</p>
                )}
            </div>
        </div>
    )
}

function ObservationRow({
    observation,
    returnPath,
}: {
    observation: ReplayObservationApi
    returnPath: string
}): JSX.Element {
    if (observation.status === 'pending' || observation.status === 'running') {
        return (
            <div className="flex items-center gap-2 px-3 py-2 text-sm text-secondary">
                <Spinner />
                <span>Still watching</span>
            </div>
        )
    }

    if (observation.status !== 'succeeded') {
        const reason = readErrorMessage(observation)
        return (
            <div className="px-3 py-2 text-sm">
                <p className="m-0 text-secondary">Could not watch this recording{reason ? `: ${reason}` : '.'}</p>
            </div>
        )
    }

    // Summarizers write a title and a body; a monitor scan has reasoning, which can carry markdown.
    const title = readTitle(observation)
    const reasoning = readReasoning(observation)
    const body = readSummary(observation) ?? (reasoning ? flattenMarkdownToLine(reasoning) : null)

    return (
        <div className="px-3 py-2 text-sm">
            {title && <p className="m-0 font-semibold">{title}</p>}
            {body && <p className="m-0 mt-0.5 text-secondary">{body}</p>}
            <Link
                data-attr="vision-scan-widget-open-observation"
                to={observationFromOriginUrl(observation.id, POSTHOG_AI_ORIGIN, returnPath)}
                className="text-xs"
            >
                View details
            </Link>
        </div>
    )
}