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.

44.4 KB
Raw Blame
import { useEffect, useMemo, useRef, useState } from 'react'

import { LemonSegmentedButton, LemonSlider, LemonSwitch, LemonTag, type LemonTagType } from '@posthog/lemon-ui'

import { LemonModal } from 'lib/lemon-ui/LemonModal'
import { cn } from 'lib/utils/css-classes'

import { OverlayRect } from './OverlayRect'

export type VisualDiffResult = 'changed' | 'new' | 'removed' | 'unchanged'

export type ComparisonMode = 'sideBySide' | 'blend' | 'split' | 'diff'

/** Bounding box of a connected diff region, in natural image coordinates. */
export interface DiffOverlayBox {
    x: number
    y: number
    width: number
    height: number
}

/**
 * A run of rows the current image gained or lost, in natural image coordinates.
 * A deleted band's rows are not in the current image, so its `y` is the seam the
 * removed rows left behind, not the top of a region that is still there.
 */
export interface DiffOverlayBand {
    y: number
    rows: number
    kind: 'inserted' | 'deleted'
}

export interface VisualImageDiffViewerProps {
    baselineUrl: string | null
    currentUrl: string | null
    diffUrl: string | null
    diffPercentage: number | null
    result: VisualDiffResult
    className?: string
    /** Natural image width — images under 600px on both axes render at 2x with pixelated scaling */
    imageWidth?: number
    imageHeight?: number
    /**
     * Natural per-side dimensions. When baseline and current differ in size,
     * split/blend composite both onto a shared canvas (the union of the two
     * sizes) at one scale, anchored top-left, instead of rescaling each to
     * fill the frame. Fall back to `imageWidth`/`imageHeight` when unset.
     */
    baselineWidth?: number
    baselineHeight?: number
    currentWidth?: number
    currentHeight?: number
    mode?: ComparisonMode
    onModeChange?: (mode: ComparisonMode) => void
    /**
     * Bounding boxes drawn over the diff image (and the blend overlay
     * when on `blend` mode). Coords are in the diff image's natural
     * pixel space (which is the *padded* size when baseline and current
     * differed); the overlay scales with the rendered image via SVG
     * viewBox + preserveAspectRatio="none". Empty array == no overlays.
     */
    diffOverlayBoxes?: DiffOverlayBox[]
    /**
     * Rows the current image gained or lost, in the same vertical coord space
     * as `diffOverlayBoxes`. Each band spans the full width, so it carries no
     * x. Drawn in their own color because a band is where content moved, not
     * where it changed, and unlike clusters they stay visible when the cluster
     * toggle is off.
     */
    diffOverlayBands?: DiffOverlayBand[]
    /**
     * Natural-pixel dimensions of the bbox coord space — the diff
     * image's dimensions, which equal current/baseline when sizes
     * match and the padded size when they don't. Defaults to
     * `imageWidth`/`imageHeight` for back-compat with callers that
     * never had a size mismatch.
     */
    diffOverlayWidth?: number
    diffOverlayHeight?: number
    /** Highlighted cluster index — emphasized in the overlay (filled, opaque). */
    highlightedOverlayIndex?: number | null
    /** Fires when a user hovers a bbox/number in the overlay. Lets the
     *  parent sync a sidebar panel's row highlight to the overlay. */
    onOverlayHover?: (index: number | null) => void
}

const RESULT_LABELS: Record<VisualDiffResult, string> = {
    changed: 'Changed',
    new: 'New',
    removed: 'Removed',
    unchanged: 'Unchanged',
}

const RESULT_TAG_TYPES: Record<VisualDiffResult, LemonTagType> = {
    changed: 'warning',
    new: 'primary',
    removed: 'danger',
    unchanged: 'success',
}

function isComparisonResult(result: VisualDiffResult): boolean {
    return result === 'changed' || result === 'unchanged'
}

function formatDiffPercentage(diffPercentage: number | null): string | null {
    if (diffPercentage === null || Number.isNaN(diffPercentage)) {
        return null
    }
    const decimals = Math.abs(diffPercentage) < 10 ? 2 : 1
    return `${Math.abs(diffPercentage).toFixed(decimals)}% different`
}

interface ImagePanelProps {
    url: string | null
    label: string
    emptyTitle: string
    imgClassName?: string
    imgStyle?: React.CSSProperties
    /** When set, draw bbox outlines over the image at these natural-coord positions. */
    overlayBoxes?: DiffOverlayBox[]
    /** Shift bands, drawn in the same coord space as `overlayBoxes`. */
    overlayBands?: DiffOverlayBand[]
    overlayWidth?: number
    overlayHeight?: number
    /** Highlighted cluster index — that one box renders emphasized. */
    highlightedOverlayIndex?: number | null
    /** Fires on hover so a parent panel can sync. */
    onOverlayHover?: (index: number | null) => void
    onClick?: () => void
}

function ImagePanel({
    url,
    label,
    emptyTitle,
    imgClassName,
    imgStyle,
    overlayBoxes,
    overlayBands,
    overlayWidth,
    overlayHeight,
    highlightedOverlayIndex,
    onOverlayHover,
    onClick,
}: ImagePanelProps): JSX.Element {
    const boxCount = (overlayBoxes?.length ?? 0) + (overlayBands?.length ?? 0)
    const hasOverlay = !!url && boxCount > 0 && !!overlayWidth && !!overlayHeight
    const image = url ? (
        // `block` on the inline-block wrapper kills the implicit
        // baseline-descender gap that nudges the SVG overlay a few
        // pixels below the image's actual bottom edge.
        <div className="relative inline-block max-w-full leading-none">
            <img
                src={url}
                alt={label}
                loading="lazy"
                decoding="async"
                className={cn('block h-auto bg-black/5', imgClassName || 'max-w-full')}
                // eslint-disable-next-line react/forbid-dom-props
                style={imgStyle}
            />
            {hasOverlay && (
                <BboxOverlay
                    boxes={overlayBoxes ?? []}
                    bands={overlayBands}
                    width={overlayWidth!}
                    height={overlayHeight!}
                    highlightedIndex={highlightedOverlayIndex ?? null}
                    onHover={onOverlayHover}
                />
            )}
        </div>
    ) : null

    return (
        <div className="overflow-hidden rounded-lg border bg-bg-light inline-block max-w-full">
            <div className="px-2 py-1 text-[11px] font-semibold uppercase tracking-wide border-b bg-bg-3000">
                {label}
            </div>
            {image ? (
                onClick ? (
                    <button
                        type="button"
                        onClick={onClick}
                        aria-label={`View ${label.toLowerCase()} snapshot full screen`}
                        data-attr="visual-review-zoom-image"
                        className="block max-w-full cursor-zoom-in border-0 bg-transparent p-0 text-left"
                    >
                        {image}
                    </button>
                ) : (
                    image
                )
            ) : (
                <EmptyImageState title={emptyTitle} />
            )}
        </div>
    )
}

interface BboxOverlayProps {
    boxes: DiffOverlayBox[]
    /** Rows the current image gained or lost. Not numbered and not hoverable. */
    bands?: DiffOverlayBand[]
    /** Natural pixel coord space the bboxes live in. */
    width: number
    height: number
    /** When non-null, that box is emphasized (filled) and the others fade. */
    highlightedIndex: number | null
    /** Fires on hover so the parent can sync sidebar highlight. */
    onHover?: (index: number | null) => void
}

// Warm orange palette to match the mockup — distinct from the
// blue-tinted "Before/After" labels and the green/red of result tags.
// These are literal colors rather than tokens because they are drawn over a
// screenshot, so they have to read the same whatever theme the page is in.
const OVERLAY_STROKE = 'rgb(245, 134, 52)'
const OVERLAY_FILL_DEFAULT = 'rgba(245, 134, 52, 0.10)'
const OVERLAY_FILL_HIGHLIGHT = 'rgba(245, 134, 52, 0.28)'

// Violet for shift bands, so a band is never read as a change region.
const BAND_STROKE = 'rgb(124, 92, 214)'
const BAND_FILL = 'rgba(124, 92, 214, 0.22)'

// A deleted band marks a seam, not a region, so it gets a fixed thin height
// instead of the rows it removed. A deletion at the bottom edge sits at
// y = height, so the seam is clamped to keep it inside the image.
const BAND_SEAM_HEIGHT = 3

function BboxOverlay({ boxes, bands, width, height, highlightedIndex, onHover }: BboxOverlayProps): JSX.Element {
    return (
        <>
            <svg
                // viewBox in the bbox coord space + preserveAspectRatio=none
                // stretches the SVG to the rendered image's box. With
                // `vector-effect: non-scaling-stroke` the stroke stays a
                // constant 2px regardless of how the image is scaled. The
                // SVG itself stays pointer-events-none so the rects don't
                // shadow underlying interactions; rects flip to auto so
                // they can fire hover callbacks for sidebar sync.
                className="absolute inset-0 w-full h-full pointer-events-none"
                viewBox={`0 0 ${width} ${height}`}
                preserveAspectRatio="none"
            >
                {/* A band spans the whole image, so it reads as a rule across the
                 * diff rather than as a region of it. Inserted rows are there to
                 * fill. Deleted rows are not, so their band marks the solid seam
                 * they left behind instead of covering the rows below it. */}
                {(bands ?? []).map((band, i) =>
                    band.kind === 'deleted' ? (
                        <OverlayRect
                            key={`band-${i}`}
                            x={0}
                            y={Math.max(0, Math.min(band.y, height - BAND_SEAM_HEIGHT))}
                            width={width}
                            height={BAND_SEAM_HEIGHT}
                            fill={BAND_STROKE}
                            stroke={BAND_STROKE}
                            strokeWidth={2}
                        />
                    ) : (
                        <OverlayRect
                            key={`band-${i}`}
                            x={0}
                            y={band.y}
                            width={width}
                            height={band.rows}
                            fill={BAND_FILL}
                            stroke={BAND_STROKE}
                            strokeWidth={2}
                        />
                    )
                )}
                {boxes.map((b, i) => {
                    const isHighlighted = highlightedIndex === i
                    const isDimmed = highlightedIndex !== null && !isHighlighted
                    return (
                        <OverlayRect
                            key={i}
                            x={b.x}
                            y={b.y}
                            width={b.width}
                            height={b.height}
                            fill={isHighlighted ? OVERLAY_FILL_HIGHLIGHT : OVERLAY_FILL_DEFAULT}
                            stroke={OVERLAY_STROKE}
                            strokeWidth={isHighlighted ? 3 : 2}
                            strokeDasharray={isHighlighted ? undefined : '4 3'}
                            opacity={isDimmed ? 0.4 : 1}
                            onHover={onHover}
                            index={i}
                        />
                    )
                })}
            </svg>
            {boxes.map((b, i) => {
                const leftPct = (b.x / width) * 100
                const topPct = (b.y / height) * 100
                const isHighlighted = highlightedIndex === i
                const isDimmed = highlightedIndex !== null && !isHighlighted
                return (
                    <span
                        key={`label-${i}`}
                        className="absolute flex items-center justify-center rounded-full text-white text-[11px] font-bold tabular-nums shadow-md ring-1 ring-white/70 transition-transform"
                        // eslint-disable-next-line react/forbid-dom-props
                        style={{
                            left: `calc(${leftPct}% - 11px)`,
                            top: `calc(${topPct}% - 11px)`,
                            width: 22,
                            height: 22,
                            background: OVERLAY_STROKE,
                            opacity: isDimmed ? 0.45 : 1,
                            transform: isHighlighted ? 'scale(1.15)' : undefined,
                            cursor: onHover ? 'pointer' : undefined,
                            pointerEvents: onHover ? 'auto' : 'none',
                        }}
                        onMouseEnter={onHover ? () => onHover(i) : undefined}
                        onMouseLeave={onHover ? () => onHover(null) : undefined}
                    >
                        {i + 1}
                    </span>
                )
            })}
        </>
    )
}

function EmptyImageState({ title }: { title: string }): JSX.Element {
    return (
        <div className="flex size-full items-center justify-center bg-bg-light px-4 text-center text-muted-foreground">
            <div>
                <div className="text-sm font-semibold">{title}</div>
                <div className="text-xs mt-1">No image available</div>
            </div>
        </div>
    )
}

/** Images smaller than this threshold render at 2x with pixelated scaling */
const SMALL_IMAGE_THRESHOLD = 600

function effectiveMode(requested: ComparisonMode, supportsComparison: boolean, hasDiffImage: boolean): ComparisonMode {
    if (!supportsComparison) {
        return 'blend'
    }
    if (!hasDiffImage && requested === 'diff') {
        return 'blend'
    }
    return requested
}

export function VisualImageDiffViewer({
    baselineUrl,
    currentUrl,
    diffUrl,
    diffPercentage,
    result,
    className,
    imageWidth,
    imageHeight,
    baselineWidth,
    baselineHeight,
    currentWidth,
    currentHeight,
    diffOverlayBoxes,
    diffOverlayBands,
    diffOverlayWidth,
    diffOverlayHeight,
    highlightedOverlayIndex,
    onOverlayHover,
    mode: controlledMode,
    onModeChange,
}: VisualImageDiffViewerProps): JSX.Element {
    // Bbox coords live in the diff image's natural pixel space (= padded
    // size when sizes mismatched). Fall back to image dims for the
    // common matched-size case.
    const overlayCoordWidth = diffOverlayWidth ?? imageWidth
    const overlayCoordHeight = diffOverlayHeight ?? imageHeight
    const hasOverlayBoxes = !!diffOverlayBoxes && diffOverlayBoxes.length > 0
    const [showClusters, setShowClusters] = useState(true)
    const overlayBoxesIfShown = showClusters ? diffOverlayBoxes : undefined
    // Bands stay on with the cluster toggle off. A shift band is what the
    // toggle is meant to leave behind: where the page moved, not what changed.
    const hasOverlayContent = (overlayBoxesIfShown?.length ?? 0) + (diffOverlayBands?.length ?? 0) > 0
    // Overlays live in the diff image's coord space. When that space is the
    // padded size they line up with neither the baseline nor the current
    // image, so every surface that draws over one of those two has to check.
    const overlayCoordsMatch = overlayCoordWidth === imageWidth && overlayCoordHeight === imageHeight
    const supportsComparison = isComparisonResult(result)
    const hasBothImages = Boolean(baselineUrl && currentUrl)
    const hasDiffImage = Boolean(diffUrl)

    // Natural per-side dimensions, falling back to the single size the caller
    // already passes for matched-size snapshots.
    const baselineNaturalWidth = baselineWidth ?? imageWidth
    const baselineNaturalHeight = baselineHeight ?? imageHeight
    const currentNaturalWidth = currentWidth ?? imageWidth
    const currentNaturalHeight = currentHeight ?? imageHeight
    // Shared compositing canvas = union of both sizes. split/blend overlay
    // both images onto it at one scale, anchored top-left: when widths match
    // the canvas only grows downward, when heights match it only grows to the
    // right, and the smaller image keeps its top-left origin with the extra
    // area left empty — so a size change reads as growth, not a rescale.
    const canvasWidth =
        baselineNaturalWidth !== undefined && currentNaturalWidth !== undefined
            ? Math.max(baselineNaturalWidth, currentNaturalWidth)
            : (baselineNaturalWidth ?? currentNaturalWidth)
    const canvasHeight =
        baselineNaturalHeight !== undefined && currentNaturalHeight !== undefined
            ? Math.max(baselineNaturalHeight, currentNaturalHeight)
            : (baselineNaturalHeight ?? currentNaturalHeight)
    // Only set an explicit aspect ratio (and thus absolute-positioned image
    // layers) when both canvas dims are known; otherwise fall back to letting
    // the base image size the stage in normal flow.
    const stageAspectRatio = canvasWidth && canvasHeight ? `${canvasWidth} / ${canvasHeight}` : undefined

    const isSmallImage =
        canvasWidth !== undefined &&
        canvasWidth < SMALL_IMAGE_THRESHOLD &&
        (canvasHeight === undefined || canvasHeight < SMALL_IMAGE_THRESHOLD)
    const pixelatedStyle = isSmallImage
        ? { imageRendering: 'pixelated' as const, width: (canvasWidth ?? 0) * 2, maxWidth: '100%' }
        : {}
    const pixelatedClass = isSmallImage ? '' : 'max-w-full'

    // One image layer on the shared canvas. When the canvas size is known the
    // layer is absolutely positioned at its true fraction of the canvas
    // (top-left anchored); otherwise it falls back to filling the stage width.
    const layerClass = stageAspectRatio ? 'block bg-black/5' : 'w-full h-auto block bg-black/5'
    const layerStyle = (naturalWidth?: number, naturalHeight?: number): React.CSSProperties => {
        const pixelated = isSmallImage ? { imageRendering: 'pixelated' as const } : {}
        if (stageAspectRatio && canvasWidth && canvasHeight && naturalWidth && naturalHeight) {
            return {
                position: 'absolute',
                top: 0,
                left: 0,
                width: `${(naturalWidth / canvasWidth) * 100}%`,
                height: `${(naturalHeight / canvasHeight) * 100}%`,
                ...pixelated,
            }
        }
        return pixelated
    }

    // The diff raster and the overlays live in the diff image's coord space, so
    // on the shared canvas they cover that space's fraction of it, the way an
    // image layer does. The diff is current-sized when the pair aligned, and it
    // then has to land on the current image instead of stretching over the empty
    // area a taller baseline leaves behind. Without alignment the diff is the
    // padded union size, and this leaves it filling the whole canvas.
    const diffLayerIsFractional = Boolean(
        stageAspectRatio && canvasWidth && canvasHeight && overlayCoordWidth && overlayCoordHeight
    )
    const diffLayerClass = diffLayerIsFractional ? 'absolute top-0 left-0' : 'absolute top-0 left-0 w-full h-full'

    const [internalMode, setInternalMode] = useState<ComparisonMode>('sideBySide')
    const requestedMode = controlledMode ?? internalMode
    const mode: ComparisonMode = effectiveMode(requestedMode, supportsComparison, hasDiffImage)
    const setMode = (newMode: ComparisonMode): void => {
        setInternalMode(newMode)
        onModeChange?.(newMode)
    }
    const [splitPosition, setSplitPosition] = useState(25)
    const [blendPercentage, setBlendPercentage] = useState(50)
    const [showDiffOverlay, setShowDiffOverlay] = useState(false)
    const [diffOverlayOpacity, setDiffOverlayOpacity] = useState(55)
    const [zoomedImage, setZoomedImage] = useState<{ url: string; label: string } | null>(null)
    const [flicker, setFlicker] = useState(false)
    const [flickerCurrentVisible, setFlickerCurrentVisible] = useState(false)
    const [draggingSplit, setDraggingSplit] = useState(false)
    const overlayRef = useRef<HTMLDivElement | null>(null)

    const diffLabel = formatDiffPercentage(diffPercentage)

    const comparisonModes = useMemo(() => {
        const modes: { value: ComparisonMode; label: string; 'data-attr': string }[] = [
            { value: 'sideBySide', label: 'Side by side', 'data-attr': 'image-diff-mode-side-by-side' },
            { value: 'blend', label: 'Blend', 'data-attr': 'image-diff-mode-blend' },
            { value: 'split', label: 'Split', 'data-attr': 'image-diff-mode-split' },
        ]
        if (hasDiffImage) {
            modes.push({ value: 'diff', label: 'Diff', 'data-attr': 'image-diff-mode-diff' })
        }
        return modes
    }, [hasDiffImage])

    useEffect(() => {
        if (!(flicker && mode === 'split' && result === 'changed' && hasBothImages)) {
            setFlickerCurrentVisible(false)
            return
        }
        const interval = window.setInterval(() => {
            setFlickerCurrentVisible((current) => !current)
        }, 650)
        return () => {
            window.clearInterval(interval)
        }
    }, [flicker, mode, result, hasBothImages])

    useEffect(() => {
        if (mode !== 'split' || !hasBothImages) {
            setFlicker(false)
        }
    }, [mode, hasBothImages])

    useEffect(() => {
        if (!draggingSplit) {
            return
        }

        const setSplitFromClientX = (clientX: number): void => {
            if (!overlayRef.current) {
                return
            }
            const rect = overlayRef.current.getBoundingClientRect()
            if (rect.width <= 0) {
                return
            }
            const next = ((clientX - rect.left) / rect.width) * 100
            setSplitPosition(Math.max(0, Math.min(100, next)))
        }

        const handleMouseMove = (event: MouseEvent): void => {
            setSplitFromClientX(event.clientX)
        }
        const handleTouchMove = (event: TouchEvent): void => {
            if (event.touches.length > 0) {
                setSplitFromClientX(event.touches[0].clientX)
            }
        }
        const stopDragging = (): void => {
            setDraggingSplit(false)
        }

        window.addEventListener('mousemove', handleMouseMove)
        window.addEventListener('mouseup', stopDragging)
        window.addEventListener('touchmove', handleTouchMove, { passive: true })
        window.addEventListener('touchend', stopDragging)

        return () => {
            window.removeEventListener('mousemove', handleMouseMove)
            window.removeEventListener('mouseup', stopDragging)
            window.removeEventListener('touchmove', handleTouchMove)
            window.removeEventListener('touchend', stopDragging)
        }
    }, [draggingSplit])

    const renderComparisonBody = (): JSX.Element => {
        if (mode === 'diff') {
            return (
                <div className="p-3 flex justify-center">
                    <ImagePanel
                        url={diffUrl}
                        label="Diff"
                        emptyTitle="No diff image available"
                        overlayBoxes={overlayBoxesIfShown}
                        overlayBands={diffOverlayBands}
                        overlayWidth={overlayCoordWidth}
                        overlayHeight={overlayCoordHeight}
                        highlightedOverlayIndex={highlightedOverlayIndex}
                        onOverlayHover={onOverlayHover}
                    />
                </div>
            )
        }

        if (mode === 'sideBySide') {
            // Overlay only on the "After" panel — that's the side users
            // judge against, and bboxes were computed against current.
            // Skip when the bbox coord space doesn't match the rendered
            // image (size-mismatch case).
            return (
                <div className="grid grid-cols-1 items-start justify-items-center gap-3 p-3 @md/image-diff:grid-cols-2">
                    <ImagePanel
                        url={baselineUrl}
                        label="Before"
                        emptyTitle="Before snapshot missing"
                        imgClassName={pixelatedClass}
                        imgStyle={pixelatedStyle}
                        onClick={
                            baselineUrl
                                ? () => setZoomedImage({ url: baselineUrl, label: 'Before snapshot' })
                                : undefined
                        }
                    />
                    <ImagePanel
                        url={currentUrl}
                        label="After"
                        emptyTitle="After snapshot missing"
                        imgClassName={pixelatedClass}
                        imgStyle={pixelatedStyle}
                        onClick={
                            currentUrl ? () => setZoomedImage({ url: currentUrl, label: 'After snapshot' }) : undefined
                        }
                        overlayBoxes={overlayCoordsMatch ? overlayBoxesIfShown : undefined}
                        overlayBands={overlayCoordsMatch ? diffOverlayBands : undefined}
                        overlayWidth={overlayCoordWidth}
                        overlayHeight={overlayCoordHeight}
                        highlightedOverlayIndex={highlightedOverlayIndex}
                        onOverlayHover={onOverlayHover}
                    />
                </div>
            )
        }

        const activeOverlayUrl =
            flicker && mode === 'split' && hasBothImages
                ? flickerCurrentVisible
                    ? currentUrl
                    : baselineUrl
                : currentUrl

        return (
            <div className="p-3 flex justify-center">
                <div
                    className="overflow-hidden rounded-lg border bg-bg-light w-full max-w-full relative"
                    // eslint-disable-next-line react/forbid-dom-props
                    style={isSmallImage ? { width: (canvasWidth ?? 0) * 2, maxWidth: '100%' } : undefined}
                >
                    {/* Base header — blend: both labels; split: "Before" left-aligned */}
                    <div className="flex items-center justify-between px-2 py-1 border-b bg-bg-3000 text-[11px] font-semibold uppercase tracking-wide">
                        {mode === 'blend' ? (
                            <>
                                <span>Before</span>
                                <span className="font-normal normal-case tracking-normal tabular-nums text-muted">
                                    {100 - blendPercentage}% / {blendPercentage}%
                                </span>
                                <span>After</span>
                            </>
                        ) : (
                            <span>Before</span>
                        )}
                    </div>

                    {/* Base image area — sized to the shared canvas so both the
                     * baseline and the clipped "After" overlay composite at one
                     * scale (see layerStyle). */}
                    <div
                        ref={overlayRef}
                        className="relative overflow-hidden"
                        // eslint-disable-next-line react/forbid-dom-props
                        style={stageAspectRatio ? { aspectRatio: stageAspectRatio } : undefined}
                    >
                        {baselineUrl ? (
                            <img
                                src={baselineUrl}
                                alt="Before snapshot"
                                className={layerClass}
                                // eslint-disable-next-line react/forbid-dom-props
                                style={layerStyle(baselineNaturalWidth, baselineNaturalHeight)}
                            />
                        ) : (
                            <EmptyImageState title="Before snapshot missing" />
                        )}

                        {/* Flicker overlay — full image swap inside image area */}
                        {flicker && mode === 'split' && activeOverlayUrl && (
                            <div className="absolute top-0 left-0 w-full h-full overflow-hidden">
                                <img
                                    src={activeOverlayUrl}
                                    alt="Flicker frame"
                                    className={layerClass}
                                    // eslint-disable-next-line react/forbid-dom-props
                                    style={layerStyle(
                                        flickerCurrentVisible ? currentNaturalWidth : baselineNaturalWidth,
                                        flickerCurrentVisible ? currentNaturalHeight : baselineNaturalHeight
                                    )}
                                />
                            </div>
                        )}

                        {/* Blend overlay — inside image area only */}
                        {mode === 'blend' && activeOverlayUrl && (
                            <div
                                className="absolute top-0 left-0 w-full h-full overflow-hidden"
                                // eslint-disable-next-line react/forbid-dom-props
                                style={{
                                    opacity: !flicker ? Math.max(0, Math.min(1, blendPercentage / 100)) : 1,
                                }}
                            >
                                <img
                                    src={activeOverlayUrl}
                                    alt="After snapshot"
                                    className={layerClass}
                                    // eslint-disable-next-line react/forbid-dom-props
                                    style={layerStyle(currentNaturalWidth, currentNaturalHeight)}
                                />
                            </div>
                        )}

                        {showDiffOverlay && hasDiffImage && (
                            <img
                                src={diffUrl as string}
                                alt="Diff overlay"
                                className={cn(diffLayerClass, 'mix-blend-screen pointer-events-none')}
                                // eslint-disable-next-line react/forbid-dom-props
                                style={{
                                    ...layerStyle(overlayCoordWidth, overlayCoordHeight),
                                    opacity: diffOverlayOpacity / 100,
                                }}
                            />
                        )}

                        {/* Cluster bbox overlays — drawn on top of the
                         * blend stack so users can read where the change
                         * regions are without leaving blend mode. Only
                         * meaningful when the bbox coord space matches
                         * the underlying image (mismatch case has bboxes
                         * in padded coords that don't align with either
                         * baseline or current). */}
                        {(mode === 'blend' || mode === 'split') &&
                            hasOverlayContent &&
                            !!overlayCoordWidth &&
                            !!overlayCoordHeight &&
                            overlayCoordsMatch && (
                                <div
                                    className={cn(diffLayerClass, 'pointer-events-none')}
                                    // eslint-disable-next-line react/forbid-dom-props
                                    style={layerStyle(overlayCoordWidth, overlayCoordHeight)}
                                >
                                    <BboxOverlay
                                        boxes={overlayBoxesIfShown ?? []}
                                        bands={diffOverlayBands}
                                        width={overlayCoordWidth}
                                        height={overlayCoordHeight}
                                        highlightedIndex={highlightedOverlayIndex ?? null}
                                        onHover={onOverlayHover}
                                    />
                                </div>
                            )}

                        {/* Split drag handle — inside image area */}
                        {!flicker && mode === 'split' && hasBothImages && (
                            <button
                                type="button"
                                className="absolute inset-y-0 z-30 w-8 -translate-x-1/2 cursor-col-resize focus:outline-none"
                                // eslint-disable-next-line react/forbid-dom-props
                                style={{ left: `${splitPosition}%` }}
                                onMouseDown={(event) => {
                                    event.preventDefault()
                                    setDraggingSplit(true)
                                }}
                                onTouchStart={() => setDraggingSplit(true)}
                                aria-label="Drag comparison split handle"
                            >
                                <div className="absolute left-1/2 top-1/2 flex h-9 w-9 -translate-x-1/2 -translate-y-1/2 items-center justify-center rounded-full border bg-surface-primary text-xs shadow-sm">
                                    ⇆
                                </div>
                            </button>
                        )}
                    </div>

                    {/* Split overlay — spans header + image, clipped from the left at split position */}
                    {mode === 'split' && !flicker && activeOverlayUrl && (
                        <div
                            className="absolute inset-0 z-10 overflow-hidden pointer-events-none"
                            // eslint-disable-next-line react/forbid-dom-props
                            style={{ clipPath: `inset(0 0 0 ${splitPosition}%)` }}
                        >
                            <div className="flex items-center justify-end px-2 py-1 border-b bg-bg-3000 text-[11px] font-semibold uppercase tracking-wide">
                                <span>After</span>
                            </div>
                            <div
                                className="relative overflow-hidden"
                                // eslint-disable-next-line react/forbid-dom-props
                                style={stageAspectRatio ? { aspectRatio: stageAspectRatio } : undefined}
                            >
                                <img
                                    src={activeOverlayUrl}
                                    alt="After snapshot"
                                    className={layerClass}
                                    // eslint-disable-next-line react/forbid-dom-props
                                    style={layerStyle(currentNaturalWidth, currentNaturalHeight)}
                                />
                                {/* Second copy of the bbox overlay inside the
                                 * clipped After half so the boxes stay visible
                                 * regardless of where the user drags the split.
                                 * Forwards onHover so hovering a chip on the
                                 * After side still syncs with the sidebar
                                 * panel (without it the After-side bboxes were
                                 * silently non-interactive). */}
                                {hasOverlayContent &&
                                    !!overlayCoordWidth &&
                                    !!overlayCoordHeight &&
                                    overlayCoordsMatch && (
                                        <div
                                            className={cn(diffLayerClass, 'pointer-events-none')}
                                            // eslint-disable-next-line react/forbid-dom-props
                                            style={layerStyle(overlayCoordWidth, overlayCoordHeight)}
                                        >
                                            <BboxOverlay
                                                boxes={overlayBoxesIfShown ?? []}
                                                bands={diffOverlayBands}
                                                width={overlayCoordWidth}
                                                height={overlayCoordHeight}
                                                highlightedIndex={highlightedOverlayIndex ?? null}
                                                onHover={onOverlayHover}
                                            />
                                        </div>
                                    )}
                            </div>
                        </div>
                    )}

                    {/* Split divider line + shadow — spans full height including header */}
                    {!flicker && mode === 'split' && hasBothImages && (
                        <>
                            <div
                                className="absolute inset-y-0 z-20 w-px bg-border-bold pointer-events-none"
                                // eslint-disable-next-line react/forbid-dom-props
                                style={{ left: `${splitPosition}%` }}
                            />
                            <div
                                className="absolute inset-y-0 z-20 w-3 -translate-x-full pointer-events-none"
                                // eslint-disable-next-line react/forbid-dom-props
                                style={{
                                    left: `${splitPosition}%`,
                                    background: 'linear-gradient(to left, rgba(0,0,0,0.15), transparent)',
                                }}
                            />
                        </>
                    )}
                </div>
            </div>
        )
    }

    const renderSingleImageBody = (): JSX.Element => {
        const singleImage =
            result === 'new' ? currentUrl : result === 'removed' ? baselineUrl : currentUrl || baselineUrl || diffUrl
        const singleLabel = result === 'new' ? 'New snapshot' : 'Before snapshot'
        const emptyTitle =
            result === 'new' ? 'New snapshot is missing an image' : 'Removed snapshot has no before image'

        return (
            <div className="p-3 flex justify-center">
                <ImagePanel
                    url={singleImage}
                    label={singleLabel}
                    emptyTitle={emptyTitle}
                    imgClassName={pixelatedClass}
                    imgStyle={pixelatedStyle}
                />
            </div>
        )
    }

    return (
        <>
            <section
                className={cn(
                    '@container/image-diff w-full min-w-0 overflow-hidden rounded-xl border bg-surface-primary shadow-sm',
                    className
                )}
            >
                <div className="border-b bg-gradient-to-r from-bg-light via-bg-light to-bg-light/70 p-3">
                    <div className="flex flex-wrap items-center justify-between gap-2">
                        <div className="flex flex-wrap items-center gap-2">
                            <LemonTag type={RESULT_TAG_TYPES[result]}>{RESULT_LABELS[result]}</LemonTag>
                            {diffLabel && <LemonTag type="muted">{diffLabel}</LemonTag>}
                            {hasOverlayBoxes && (
                                <LemonSwitch
                                    checked={showClusters}
                                    onChange={setShowClusters}
                                    size="xsmall"
                                    label="Clusters"
                                    bordered
                                />
                            )}
                            {isSmallImage && (
                                <LemonTag type="highlight" className="font-bold">
                                    Enlarged 2x for review
                                </LemonTag>
                            )}
                        </div>
                        {supportsComparison && (
                            <LemonSegmentedButton
                                size="small"
                                value={mode}
                                onChange={(newMode) => setMode(newMode)}
                                options={comparisonModes}
                            />
                        )}
                    </div>

                    {supportsComparison && mode !== 'sideBySide' && mode !== 'diff' && (
                        <div className="mt-3 flex flex-wrap items-center gap-4 rounded-lg border bg-surface-primary px-3 py-2">
                            {mode === 'split' && hasBothImages && (
                                <LemonSwitch checked={flicker} onChange={setFlicker} size="small" label="Flicker" />
                            )}
                            {mode === 'blend' && (
                                <div className="flex min-w-0 basis-60 flex-1 items-center gap-3">
                                    <span className="text-xs text-muted-foreground whitespace-nowrap">
                                        Before → After
                                    </span>
                                    <LemonSlider
                                        min={0}
                                        max={100}
                                        step={1}
                                        value={blendPercentage}
                                        onChange={setBlendPercentage}
                                        className="m-0 w-full"
                                    />
                                    <span className="text-xs tabular-nums text-muted-foreground w-10 text-right">
                                        {blendPercentage}%
                                    </span>
                                </div>
                            )}
                            {hasDiffImage && (
                                <>
                                    <LemonSwitch
                                        checked={showDiffOverlay}
                                        onChange={setShowDiffOverlay}
                                        size="small"
                                        label="Diff overlay"
                                    />
                                    <div
                                        className={cn(
                                            'flex min-w-0 basis-60 flex-1 items-center gap-3 transition-opacity',
                                            showDiffOverlay ? 'opacity-100' : 'opacity-40 pointer-events-none'
                                        )}
                                        aria-hidden={!showDiffOverlay}
                                    >
                                        <span className="text-xs text-muted-foreground whitespace-nowrap">
                                            Overlay opacity
                                        </span>
                                        <LemonSlider
                                            min={0}
                                            max={100}
                                            step={1}
                                            value={diffOverlayOpacity}
                                            onChange={setDiffOverlayOpacity}
                                            className="m-0 w-full"
                                        />
                                        <span className="text-xs tabular-nums text-muted-foreground w-10 text-right">
                                            {diffOverlayOpacity}%
                                        </span>
                                    </div>
                                </>
                            )}
                        </div>
                    )}
                </div>

                {supportsComparison ? renderComparisonBody() : renderSingleImageBody()}
            </section>
            <LemonModal
                isOpen={!!zoomedImage}
                onClose={() => setZoomedImage(null)}
                fullScreen
                simple
                data-attr="visual-review-zoomed-image"
            >
                <div className="flex h-full min-h-0 items-center justify-center bg-bg-3000 p-4">
                    {zoomedImage && (
                        <img src={zoomedImage.url} alt={zoomedImage.label} className="h-full w-full object-contain" />
                    )}
                </div>
            </LemonModal>
        </>
    )
}