|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { ArrowRight, Cpu, Globe2 } from 'lucide-react' |
| 4 | +import Link from 'next/link' |
| 5 | +import { useEffect, useRef, useState } from 'react' |
| 6 | + |
| 7 | +import { |
| 8 | + EMPTY_LIVE_STATS, |
| 9 | + countryName, |
| 10 | + useLiveStats, |
| 11 | +} from './live-stats-client' |
| 12 | + |
| 13 | +import type { FreebuffLiveStats } from '@/server/live-stats' |
| 14 | +import type { LucideIcon } from 'lucide-react' |
| 15 | + |
| 16 | +function useHomepageLiveStats(initialStats: FreebuffLiveStats) { |
| 17 | + const [isVisible, setIsVisible] = useState(false) |
| 18 | + const sectionRef = useRef<HTMLElement>(null) |
| 19 | + const stats = useLiveStats(initialStats, { |
| 20 | + enabled: isVisible, |
| 21 | + pauseWhenHidden: true, |
| 22 | + refreshOnMount: true, |
| 23 | + }) |
| 24 | + |
| 25 | + useEffect(() => { |
| 26 | + const section = sectionRef.current |
| 27 | + if (!section || !('IntersectionObserver' in window)) { |
| 28 | + setIsVisible(true) |
| 29 | + return |
| 30 | + } |
| 31 | + |
| 32 | + const observer = new IntersectionObserver( |
| 33 | + ([entry]) => setIsVisible(entry.isIntersecting), |
| 34 | + { rootMargin: '240px 0px', threshold: 0.01 }, |
| 35 | + ) |
| 36 | + |
| 37 | + observer.observe(section) |
| 38 | + return () => observer.disconnect() |
| 39 | + }, []) |
| 40 | + |
| 41 | + return { sectionRef, stats } |
| 42 | +} |
| 43 | + |
| 44 | +function LiveRows({ |
| 45 | + title, |
| 46 | + icon: Icon, |
| 47 | + rows, |
| 48 | + emptyLabel, |
| 49 | +}: { |
| 50 | + title: string |
| 51 | + icon: LucideIcon |
| 52 | + rows: { label: string; value: number; sublabel?: string }[] |
| 53 | + emptyLabel: string |
| 54 | +}) { |
| 55 | + return ( |
| 56 | + <div className="rounded-lg border border-white/10 bg-white/[0.04] p-4"> |
| 57 | + <div className="mb-4 flex items-center justify-between gap-3"> |
| 58 | + <h3 className="font-mono text-xs uppercase tracking-[0.18em] text-white/46"> |
| 59 | + {title} |
| 60 | + </h3> |
| 61 | + <Icon className="h-4 w-4 text-cyan-300" aria-hidden /> |
| 62 | + </div> |
| 63 | + {rows.length > 0 ? ( |
| 64 | + <div className="space-y-2"> |
| 65 | + {rows.map((row) => ( |
| 66 | + <div |
| 67 | + key={`${row.label}-${row.sublabel ?? ''}`} |
| 68 | + className="flex items-center justify-between gap-3 rounded-md bg-black/25 px-3 py-2" |
| 69 | + > |
| 70 | + <div className="min-w-0"> |
| 71 | + <div className="truncate text-sm font-medium text-white/86"> |
| 72 | + {row.label} |
| 73 | + </div> |
| 74 | + {row.sublabel && ( |
| 75 | + <div className="font-mono text-[11px] text-white/36"> |
| 76 | + {row.sublabel} |
| 77 | + </div> |
| 78 | + )} |
| 79 | + </div> |
| 80 | + <div className="font-mono text-base text-acid-matrix"> |
| 81 | + {row.value.toLocaleString()} |
| 82 | + </div> |
| 83 | + </div> |
| 84 | + ))} |
| 85 | + </div> |
| 86 | + ) : ( |
| 87 | + <div className="rounded-md border border-dashed border-white/12 bg-black/20 px-3 py-5 text-center text-sm text-white/45"> |
| 88 | + {emptyLabel} |
| 89 | + </div> |
| 90 | + )} |
| 91 | + </div> |
| 92 | + ) |
| 93 | +} |
| 94 | + |
| 95 | +export function HomepageLiveStats({ |
| 96 | + initialStats = EMPTY_LIVE_STATS, |
| 97 | +}: { |
| 98 | + initialStats?: FreebuffLiveStats |
| 99 | +}) { |
| 100 | + const { sectionRef, stats } = useHomepageLiveStats(initialStats) |
| 101 | + const isLoading = stats.generatedAt === EMPTY_LIVE_STATS.generatedAt |
| 102 | + const topCountries = stats.countries.slice(0, 4).map((country) => ({ |
| 103 | + label: countryName(country.countryCode), |
| 104 | + sublabel: country.countryCode, |
| 105 | + value: country.count, |
| 106 | + })) |
| 107 | + const topModels = stats.models.slice(0, 4).map((model) => ({ |
| 108 | + label: model.displayName, |
| 109 | + value: model.count, |
| 110 | + })) |
| 111 | + const countryEmptyLabel = isLoading |
| 112 | + ? 'Loading active countries...' |
| 113 | + : 'No active countries yet.' |
| 114 | + const modelEmptyLabel = isLoading |
| 115 | + ? 'Loading active models...' |
| 116 | + : 'No active models right now.' |
| 117 | + |
| 118 | + return ( |
| 119 | + <section |
| 120 | + ref={sectionRef} |
| 121 | + className="relative overflow-hidden bg-black py-14 md:py-20" |
| 122 | + > |
| 123 | + <div className="absolute inset-0 bg-[linear-gradient(rgba(124,255,63,0.04)_1px,transparent_1px),linear-gradient(90deg,rgba(34,211,238,0.035)_1px,transparent_1px)] bg-[size:56px_56px]" /> |
| 124 | + <div className="relative container mx-auto px-4"> |
| 125 | + <div className="grid gap-6 lg:grid-cols-[minmax(0,0.9fr)_minmax(0,1.1fr)] lg:items-end"> |
| 126 | + <div> |
| 127 | + <div className="flex items-center gap-3"> |
| 128 | + <span className="h-2.5 w-2.5 rounded-full bg-acid-matrix shadow-[0_0_20px_rgba(124,255,63,0.9)]" /> |
| 129 | + <span className="font-mono text-xs uppercase tracking-[0.22em] text-white/48"> |
| 130 | + Active users |
| 131 | + </span> |
| 132 | + </div> |
| 133 | + <div className="mt-3 font-mono text-6xl font-medium leading-none text-acid-matrix neon-text md:text-8xl"> |
| 134 | + {isLoading ? '...' : stats.totalLiveUsers.toLocaleString()} |
| 135 | + </div> |
| 136 | + <p className="mt-4 max-w-md text-sm leading-6 text-white/52 md:text-base"> |
| 137 | + Active Freebuff sessions right now, grouped by country and model. |
| 138 | + </p> |
| 139 | + <Link |
| 140 | + href="/live" |
| 141 | + className="mt-6 inline-flex items-center gap-2 rounded-md border border-acid-matrix/45 bg-acid-matrix/10 px-4 py-2 text-sm font-medium text-acid-matrix transition-colors hover:bg-acid-matrix/15" |
| 142 | + > |
| 143 | + <span>View live map</span> |
| 144 | + <ArrowRight className="h-4 w-4" aria-hidden /> |
| 145 | + </Link> |
| 146 | + </div> |
| 147 | + |
| 148 | + <div className="grid gap-4 md:grid-cols-2"> |
| 149 | + <LiveRows |
| 150 | + title="Top countries" |
| 151 | + icon={Globe2} |
| 152 | + rows={topCountries} |
| 153 | + emptyLabel={countryEmptyLabel} |
| 154 | + /> |
| 155 | + <LiveRows |
| 156 | + title="Models" |
| 157 | + icon={Cpu} |
| 158 | + rows={topModels} |
| 159 | + emptyLabel={modelEmptyLabel} |
| 160 | + /> |
| 161 | + </div> |
| 162 | + </div> |
| 163 | + </div> |
| 164 | + </section> |
| 165 | + ) |
| 166 | +} |
0 commit comments