import { useMemo } from "react"; import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, Cell, ResponsiveContainer, } from "recharts"; import { Card, CardContent, CardHeader, CardTitle, CardDescription, } from "@/components/ui/card"; import { Skeleton } from "@/components/ui/skeleton"; import { useWatchdayDistribution } from "@/hooks/useStatistics"; import type { WatchdayItem } from "@/api/statistics"; // DAYOFWEEK: MySQL returns 1=Sun, 2=Mon, 3=Tue, 4=Wed, 5=Thu, 6=Fri, 7=Sat // We display Mon–Sun const DAY_ORDER = [2, 3, 4, 5, 6, 7, 1]; const DAY_LABELS: Record = { 1: "Sun", 2: "Mon", 3: "Tue", 4: "Wed", 5: "Thu", 6: "Fri", 7: "Sat", }; function buildChartData(episodes: WatchdayItem[], movies: WatchdayItem[]) { return DAY_ORDER.map((dayNum) => { const ep = episodes.find((x) => Number(x.day) === dayNum); const mv = movies.find((x) => Number(x.day) === dayNum); const total = (ep ? parseInt(ep.count, 10) : 0) + (mv ? parseInt(mv.count, 10) : 0); return { day: DAY_LABELS[dayNum], total, dayNum }; }); } interface CustomTooltipProps { active?: boolean; payload?: { value: number; payload: { day: string; total: number } }[]; } function CustomTooltip({ active, payload }: CustomTooltipProps) { if (!active || !payload?.length) return null; const { day, total } = payload[0].payload; return (

{day}

{total} {total === 1 ? "watch" : "watches"}

); } export function CouchDayChart() { const { data, isLoading } = useWatchdayDistribution(); const chartData = useMemo(() => { if (!data) return []; return buildChartData(data.episodes, data.movies); }, [data]); const peakDay = useMemo(() => { if (!chartData.length) return null; return chartData.reduce( (max, d) => (d.total > max.total ? d : max), chartData[0], ); }, [chartData]); const maxTotal = Math.max(...chartData.map((d) => d.total), 1); return (
Your Couch Day Which day of the week do you watch most? {peakDay && peakDay.total > 0 && ( Looks like {peakDay.day}. )}
{isLoading ? ( ) : ( } cursor={{ fill: "var(--color-accent)" }} /> {chartData.map((entry) => ( 0 ? "var(--color-chart-4)" : "var(--color-chart-1)" } opacity={entry.total === 0 ? 0.3 : 1} /> ))} )}
); }