import Link from 'next/link'; import db from '@/lib/db'; import CalendarView, { DayCounter } from '../components/CalendarView'; export const dynamic = 'force-dynamic'; interface TopCounter { id: number; name: string; image_path: string | null; increments: number; } export default function StatsPage() { const cutoff = Date.now() - 365 * 24 * 60 * 60 * 1000; const topCounters = db.prepare(` SELECT c.id, c.name, c.image_path, COALESCE(SUM(e.delta), 0) AS increments FROM counters c LEFT JOIN events e ON e.counter_id = c.id GROUP BY c.id HAVING increments > 0 ORDER BY increments DESC LIMIT 10 `).all() as TopCounter[]; const dailyCounters = db.prepare(` SELECT date(e.created_at / 1000, 'unixepoch', 'localtime') AS date, c.id AS counter_id, c.name AS counter_name, c.image_path AS image_path, SUM(e.delta) AS increments FROM events e JOIN counters c ON c.id = e.counter_id WHERE e.created_at >= ? GROUP BY date, c.id HAVING SUM(e.delta) > 0 ORDER BY date ASC, increments DESC `).all(cutoff) as DayCounter[]; const maxIncrements = Math.max(...topCounters.map(c => c.increments), 1); const totalIncrements = dailyCounters.reduce((s, d) => s + d.increments, 0); return (
{/* Header */}
Back

Statistics

{/* Empty state */} {totalIncrements === 0 && topCounters.length === 0 ? (

No activity recorded yet.

Start tapping + on your counters to see stats appear here.

) : (
{/* Top counters */} {topCounters.length > 0 && (

Most Active Counters

{topCounters.map(counter => (
{counter.image_path ? ( // eslint-disable-next-line @next/next/no-img-element ) : (
)} {counter.name}
+{counter.increments}
))}
)} {/* Activity calendar */} {totalIncrements > 0 && (

Activity — Last 12 Months

{totalIncrements} total increments
)}
)}
); }