import { useEffect, useRef, useState } from 'react' const ANIMATION_DURATION = 600 // These values are calculated via static SQL queries under 'Key Stats' and rounded for display in data/surveys/state-of-startups-2025.tsx file export function SurveyStatCard({ label, percent }: { label: string; percent: number }) { const [displayValue, setDisplayValue] = useState(0) const [hasAnimated, setHasAnimated] = useState(false) const [shouldAnimateBar, setShouldAnimateBar] = useState(false) const cardRef = useRef(null) useEffect(() => { if (!percent || hasAnimated) return const observer = new IntersectionObserver( (entries) => { entries.forEach((entry) => { if (entry.isIntersecting && !hasAnimated) { setHasAnimated(true) setShouldAnimateBar(true) // Start counting animation const startTime = Date.now() const duration = ANIMATION_DURATION const startValue = 0 const endValue = percent const animate = () => { const elapsed = Date.now() - startTime const progress = Math.min(elapsed / duration, 1) // Easing function for smooth animation const easeOutQuart = 1 - Math.pow(1 - progress, 4) const currentValue = Math.round(startValue + (endValue - startValue) * easeOutQuart) setDisplayValue(currentValue) if (progress < 1) { requestAnimationFrame(animate) } } requestAnimationFrame(animate) } }) }, { threshold: 0.1 } ) const currentCardRef = cardRef.current if (currentCardRef) { observer.observe(currentCardRef) } return () => { if (currentCardRef) { observer.unobserve(currentCardRef) } } }, [percent, hasAnimated]) return (
{/* Progress bar */}
{/* Background pattern for the entire bar */}
{/* Filled portion of the bar */}
{/* Foreground pattern for the filled portion */}
{/* Text */}

{displayValue} %

{label}

) }