"use client"; import { useEffect, useMemo, useRef, useState } from "react"; import { useLocale, useTranslations } from "next-intl"; import { SocialProviderIcon } from "@/components/social-provider-icon"; import { intlLocale } from "@/i18n/config"; import { PROVIDER_LABELS, formatMetric, type SocialProviderId } from "@/lib/social"; export interface DailyViews { date: string; views: number; by_provider: Partial>; } const HEIGHT = 200; const PAD = { top: 12, right: 8, bottom: 24, left: 40 }; function parseDay(value: string) { const [year, month, day] = value.split("-").map(Number); return new Date(year, month - 1, day); } /** Rounds up to 1, 2 or 5 × 10^n so gridlines land on readable numbers. */ function niceCeil(value: number) { if (value <= 1) return 1; const magnitude = 10 ** Math.floor(Math.log10(value)); const step = [1, 2, 5, 10].find((candidate) => candidate * magnitude >= value) ?? 10; return step * magnitude; } /** Daily new views as a single monochrome area; the platform split lives in the tooltip. */ export function ViewsChart({ days }: { days: DailyViews[] }) { const t = useTranslations("analytics"); const locale = useLocale(); const dayLabel = useMemo( () => new Intl.DateTimeFormat(intlLocale(locale), { month: "short", day: "numeric" }), [locale], ); const containerRef = useRef(null); const [width, setWidth] = useState(0); const [hovered, setHovered] = useState(null); useEffect(() => { const element = containerRef.current; if (!element) return; const observer = new ResizeObserver(([entry]) => setWidth(entry.contentRect.width)); observer.observe(element); return () => observer.disconnect(); }, []); const plotWidth = Math.max(0, width - PAD.left - PAD.right); const plotHeight = HEIGHT - PAD.top - PAD.bottom; const yMax = niceCeil(Math.max(0, ...days.map((day) => day.views))); const points = useMemo( () => days.map((day, index) => ({ x: PAD.left + (days.length > 1 ? (index / (days.length - 1)) * plotWidth : plotWidth / 2), y: PAD.top + plotHeight - (day.views / yMax) * plotHeight, })), [days, plotWidth, plotHeight, yMax], ); const line = points.map((point, index) => `${index ? "L" : "M"}${point.x},${point.y}`).join(" "); const baseline = PAD.top + plotHeight; const area = points.length ? `${line} L${points[points.length - 1].x},${baseline} L${points[0].x},${baseline} Z` : ""; const xTicks = days.length > 2 ? [0, Math.floor((days.length - 1) / 2), days.length - 1] : days.map((_, index) => index); const onPointerMove = (event: React.PointerEvent) => { const bounds = event.currentTarget.getBoundingClientRect(); const ratio = (event.clientX - bounds.left) / bounds.width; setHovered(Math.min(days.length - 1, Math.max(0, Math.round(ratio * (days.length - 1))))); }; const active = hovered === null ? null : days[hovered]; const activePoint = hovered === null ? null : points[hovered]; const total = days.reduce((sum, day) => sum + day.views, 0); return (
{width > 0 && ( {[0, yMax / 2, yMax].map((tick) => { const y = PAD.top + plotHeight - (tick / yMax) * plotHeight; return ( {formatMetric(tick)} ); })} {xTicks.map((index) => ( {dayLabel.format(parseDay(days[index].date))} ))} {activePoint && ( <> )} setHovered(null)} /> )} {active && activePoint && (

{dayLabel.format(parseDay(active.date))}

{t("chart.viewCount", { count: active.views, formatted: formatMetric(active.views) })}

{Object.entries(active.by_provider).length > 1 && Object.entries(active.by_provider).map(([provider, views]) => (

{PROVIDER_LABELS[provider as SocialProviderId]} {formatMetric(views)}

))}
)} {days.map((day) => ( ))}
{t("chart.caption")}
{t("chart.date")} {t("chart.views")}
{day.date} {day.views}
); }