import { useState } from 'react'; import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer } from 'recharts'; import { epochDayToDate } from '../../lib/formatters'; import { CHART_THEME } from '../../lib/chart-theme'; import type { DailyRollup } from '../../types/stats'; interface MediaWatchChartProps { rollups: DailyRollup[]; } type Range = 14 | 30 | 90; function formatActiveMinutes(value: number | string) { const minutes = Number(value); return [`${Number.isFinite(minutes) ? minutes : 0} min`, 'Active Time']; } export function MediaWatchChart({ rollups }: MediaWatchChartProps) { const [range, setRange] = useState(30); const byDay = new Map(); for (const r of rollups) { byDay.set(r.rollupDayOrMonth, (byDay.get(r.rollupDayOrMonth) ?? 0) + r.totalActiveMin); } const chartData = Array.from(byDay.entries()) .sort(([a], [b]) => a - b) .map(([day, mins]) => ({ date: epochDayToDate(day).toLocaleDateString(undefined, { month: 'short', day: 'numeric' }), minutes: Math.round(mins), })) .slice(-range); const ranges: Range[] = [14, 30, 90]; if (chartData.length === 0) { return null; } return (

Watch Time

{ranges.map((r) => ( ))}
); }