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.

3.1 KB
Raw Blame
import clsx from 'clsx'
import { useEffect, useRef, useState } from 'react'

import { IconInfo } from '@posthog/icons'
import { LemonSkeleton, Tooltip } from '@posthog/lemon-ui'

const AnimatedNumber = ({ value, duration = 300 }: { value: number; duration?: number }): JSX.Element => {
    const [displayValue, setDisplayValue] = useState(value)
    const [isAnimating, setIsAnimating] = useState(false)
    const displayValueRef = useRef(value)
    const animationRef = useRef<number>()

    useEffect(() => {
        if (value === displayValueRef.current) {
            return
        }

        if (animationRef.current) {
            cancelAnimationFrame(animationRef.current)
        }

        const startValue = displayValueRef.current
        const endValue = value
        const startTime = performance.now()

        setIsAnimating(true)

        const animate = (currentTime: number): void => {
            const elapsed = currentTime - startTime
            const progress = Math.min(elapsed / duration, 1)

            const easeOut = 1 - (1 - progress) * (1 - progress)
            const currentValue = Math.round(startValue + (endValue - startValue) * easeOut)

            displayValueRef.current = currentValue
            setDisplayValue(currentValue)

            if (progress < 1) {
                animationRef.current = requestAnimationFrame(animate)
            } else {
                displayValueRef.current = endValue
                setDisplayValue(endValue)
                setIsAnimating(false)
            }
        }

        animationRef.current = requestAnimationFrame(animate)

        return () => {
            if (animationRef.current) {
                cancelAnimationFrame(animationRef.current)
            }
        }
    }, [value, duration])

    return (
        <span
            className={clsx(
                'text-2xl font-bold tabular-nums transition-colors duration-300',
                isAnimating && 'text-primary'
            )}
        >
            {displayValue.toLocaleString()}
        </span>
    )
}

export interface LiveStatCardProps {
    label: string
    value: number | null
    isLoading?: boolean
    tooltip?: string
}

export const LiveStatCard = ({ label, value, isLoading, tooltip }: LiveStatCardProps): JSX.Element => {
    return (
        <div className="flex flex-col">
            <div className="flex items-center gap-1 text-muted">
                <span className="text-xs uppercase font-medium">{label}</span>
                {tooltip && (
                    <Tooltip title={tooltip}>
                        <IconInfo className="text-sm" />
                    </Tooltip>
                )}
            </div>
            {isLoading ? (
                <LemonSkeleton className="w-16 h-8 mt-1" />
            ) : (
                <div className="flex items-baseline">
                    {value !== null ? <AnimatedNumber value={value} /> : <span className="text-2xl font-bold">-</span>}
                </div>
            )}
        </div>
    )
}

export const LiveStatDivider = (): JSX.Element => <div className="w-px h-10 bg-border hidden md:block" />