Files
Showy/react_frontend/src/components/dashboard/CouchDayChart.tsx
T

156 lines
4.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<number, string> = {
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 (
<div
style={{
backgroundColor: "var(--color-card)",
border: "1px solid var(--color-border)",
borderRadius: "8px",
padding: "8px 12px",
fontSize: "13px",
color: "var(--color-card-foreground)",
}}
>
<p style={{ fontWeight: 600 }}>{day}</p>
<p style={{ color: "var(--color-muted-foreground)" }}>
{total} {total === 1 ? "watch" : "watches"}
</p>
</div>
);
}
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 (
<Card>
<CardHeader>
<div className="flex flex-col gap-1">
<CardTitle>Your Couch Day</CardTitle>
<CardDescription>
Which day of the week do you watch most?
{peakDay && peakDay.total > 0 && (
<span className="ml-1 text-foreground font-medium">
Looks like {peakDay.day}.
</span>
)}
</CardDescription>
</div>
</CardHeader>
<CardContent>
{isLoading ? (
<Skeleton className="h-52 w-full" />
) : (
<ResponsiveContainer width="100%" height={210}>
<BarChart
data={chartData}
margin={{ top: 4, right: 8, left: -20, bottom: 0 }}
>
<CartesianGrid
strokeDasharray="3 3"
stroke="var(--color-border)"
vertical={false}
/>
<XAxis
dataKey="day"
tick={{ fontSize: 12, fill: "var(--color-muted-foreground)" }}
axisLine={false}
tickLine={false}
/>
<YAxis
allowDecimals={false}
tick={{ fontSize: 12, fill: "var(--color-muted-foreground)" }}
axisLine={false}
tickLine={false}
/>
<Tooltip
content={<CustomTooltip />}
cursor={{ fill: "var(--color-accent)" }}
/>
<Bar dataKey="total" radius={[4, 4, 0, 0]}>
{chartData.map((entry) => (
<Cell
key={entry.day}
fill={
entry.total === maxTotal && entry.total > 0
? "var(--color-chart-4)"
: "var(--color-chart-1)"
}
opacity={entry.total === 0 ? 0.3 : 1}
/>
))}
</Bar>
</BarChart>
</ResponsiveContainer>
)}
</CardContent>
</Card>
);
}