feat(dashboard): add recharts data visualizations
- Install recharts - Add WatchActivityChart (AreaChart, movies + episodes/month, year filter) - Add WatchTimeChart (stacked BarChart, hours/month, synced year filter) - Add RatingsChart (BarChart, user ratings distribution 1–10, color ramp) - Add TopGenresChart (horizontal BarChart, top 10 genres, type filter) - Add CouchDayChart (BarChart, watch count by weekday, peak day highlight) - Add GET /statistics/watch-weekday backend endpoint (DAYOFWEEK aggregation) - Extend statisticsApi with getYearlyWatchCount, getYearlyWatchTime, getWatchdayDistribution - Add useYearlyWatchCount, useYearlyWatchTime, useWatchdayDistribution hooks - Wire all charts into DashboardPage with responsive two-column grid layout
This commit is contained in:
1 parent
3648c41cfd
commit
ae67663623
12 files changed
+1216
-2
No files matched your search
@@ -0,0 +1,133 @@
|
||||
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>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
import {
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Cell,
|
||||
ResponsiveContainer,
|
||||
} from 'recharts';
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
||||
import type { Entry } from '@/types';
|
||||
|
||||
type Filter = 'all' | 'movies' | 'series';
|
||||
|
||||
function buildRatingsData(entries: Entry[]) {
|
||||
const counts: Record<number, number> = {};
|
||||
for (let i = 1; i <= 10; i++) counts[i] = 0;
|
||||
|
||||
for (const entry of entries) {
|
||||
const rating = entry.entryRatings?.user_rating;
|
||||
if (rating != null) {
|
||||
const key = Math.round(rating);
|
||||
if (key >= 1 && key <= 10) counts[key]++;
|
||||
}
|
||||
}
|
||||
|
||||
return Object.entries(counts).map(([score, count]) => ({
|
||||
score: `${score}★`,
|
||||
count,
|
||||
raw: Number(score),
|
||||
}));
|
||||
}
|
||||
|
||||
// Color ramp: low scores red-ish → mid yellow-ish → high scores green/blue
|
||||
const SCORE_COLORS: Record<number, string> = {
|
||||
1: 'var(--color-chart-5)',
|
||||
2: 'var(--color-chart-5)',
|
||||
3: 'var(--color-chart-5)',
|
||||
4: 'var(--color-chart-3)',
|
||||
5: 'var(--color-chart-3)',
|
||||
6: 'var(--color-chart-3)',
|
||||
7: 'var(--color-chart-2)',
|
||||
8: 'var(--color-chart-2)',
|
||||
9: 'var(--color-chart-1)',
|
||||
10: 'var(--color-chart-1)',
|
||||
};
|
||||
|
||||
interface CustomTooltipProps {
|
||||
active?: boolean;
|
||||
payload?: { value: number; payload: { score: string; count: number } }[];
|
||||
}
|
||||
|
||||
function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const { score, count } = 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 }}>{score}</p>
|
||||
<p style={{ color: 'var(--color-muted-foreground)' }}>{count} {count === 1 ? 'title' : 'titles'}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function RatingsChart() {
|
||||
const allEntries = useLibraryEntries();
|
||||
const [filter, setFilter] = useState<Filter>('all');
|
||||
|
||||
const filteredEntries = useMemo(() => {
|
||||
const library = allEntries.filter((e) => !e.watchlist);
|
||||
if (filter === 'movies') return library.filter((e) => !e.is_tvseries);
|
||||
if (filter === 'series') return library.filter((e) => e.is_tvseries);
|
||||
return library;
|
||||
}, [allEntries, filter]);
|
||||
|
||||
const chartData = useMemo(() => buildRatingsData(filteredEntries), [filteredEntries]);
|
||||
|
||||
const ratedCount = filteredEntries.filter(
|
||||
(e) => e.entryRatings?.user_rating != null,
|
||||
).length;
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<CardTitle>My Ratings</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
{ratedCount} rated {filter === 'all' ? 'titles' : filter === 'movies' ? 'movies' : 'series'}
|
||||
</CardDescription>
|
||||
</div>
|
||||
<div className="flex rounded-md border border-border overflow-hidden text-xs self-start">
|
||||
{(['all', 'movies', 'series'] as Filter[]).map((f) => (
|
||||
<button
|
||||
key={f}
|
||||
onClick={() => setFilter(f)}
|
||||
className={`px-3 py-1.5 capitalize transition-colors ${
|
||||
filter === f
|
||||
? 'bg-primary text-primary-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent'
|
||||
}`}
|
||||
>
|
||||
{f}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ResponsiveContainer width="100%" height={220}>
|
||||
<BarChart data={chartData} margin={{ top: 4, right: 8, left: -20, bottom: 0 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--color-border)" vertical={false} />
|
||||
<XAxis
|
||||
dataKey="score"
|
||||
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="count" radius={[4, 4, 0, 0]}>
|
||||
{chartData.map((entry) => (
|
||||
<Cell
|
||||
key={entry.raw}
|
||||
fill={SCORE_COLORS[entry.raw] ?? 'var(--color-chart-1)'}
|
||||
/>
|
||||
))}
|
||||
</Bar>
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,142 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
import {
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Cell,
|
||||
ResponsiveContainer,
|
||||
} from 'recharts';
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
||||
import type { Entry } from '@/types';
|
||||
|
||||
type Filter = 'all' | 'movies' | 'series';
|
||||
|
||||
const TOP_N = 10;
|
||||
|
||||
// Cycle through chart color vars for variety
|
||||
const COLORS = [
|
||||
'var(--color-chart-1)',
|
||||
'var(--color-chart-2)',
|
||||
'var(--color-chart-3)',
|
||||
'var(--color-chart-4)',
|
||||
'var(--color-chart-5)',
|
||||
];
|
||||
|
||||
function buildGenreData(entries: Entry[]) {
|
||||
const counts: Record<string, number> = {};
|
||||
for (const entry of entries) {
|
||||
if (!entry.genres) continue;
|
||||
for (const genre of entry.genres as unknown as string[]) {
|
||||
counts[genre] = (counts[genre] ?? 0) + 1;
|
||||
}
|
||||
}
|
||||
return Object.entries(counts)
|
||||
.sort((a, b) => b[1] - a[1])
|
||||
.slice(0, TOP_N)
|
||||
.map(([genre, count]) => ({ genre, count }));
|
||||
}
|
||||
|
||||
interface CustomTooltipProps {
|
||||
active?: boolean;
|
||||
payload?: { value: number; payload: { genre: string; count: number } }[];
|
||||
}
|
||||
|
||||
function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const { genre, count } = 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 }}>{genre}</p>
|
||||
<p style={{ color: 'var(--color-muted-foreground)' }}>{count} {count === 1 ? 'title' : 'titles'}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function TopGenresChart() {
|
||||
const allEntries = useLibraryEntries();
|
||||
const [filter, setFilter] = useState<Filter>('all');
|
||||
|
||||
const filteredEntries = useMemo(() => {
|
||||
const library = allEntries.filter((e) => !e.watchlist);
|
||||
if (filter === 'movies') return library.filter((e) => !e.is_tvseries);
|
||||
if (filter === 'series') return library.filter((e) => e.is_tvseries);
|
||||
return library;
|
||||
}, [allEntries, filter]);
|
||||
|
||||
const chartData = useMemo(() => buildGenreData(filteredEntries), [filteredEntries]);
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<CardTitle>Top Genres</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
Most watched genres in your library
|
||||
</CardDescription>
|
||||
</div>
|
||||
<div className="flex rounded-md border border-border overflow-hidden text-xs self-start">
|
||||
{(['all', 'movies', 'series'] as Filter[]).map((f) => (
|
||||
<button
|
||||
key={f}
|
||||
onClick={() => setFilter(f)}
|
||||
className={`px-3 py-1.5 capitalize transition-colors ${
|
||||
filter === f
|
||||
? 'bg-primary text-primary-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent'
|
||||
}`}
|
||||
>
|
||||
{f}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ResponsiveContainer width="100%" height={Math.max(chartData.length * 36, 180)}>
|
||||
<BarChart
|
||||
layout="vertical"
|
||||
data={chartData}
|
||||
margin={{ top: 4, right: 24, left: 0, bottom: 0 }}
|
||||
>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--color-border)" horizontal={false} />
|
||||
<XAxis
|
||||
type="number"
|
||||
allowDecimals={false}
|
||||
tick={{ fontSize: 12, fill: 'var(--color-muted-foreground)' }}
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
/>
|
||||
<YAxis
|
||||
type="category"
|
||||
dataKey="genre"
|
||||
width={90}
|
||||
tick={{ fontSize: 12, fill: 'var(--color-foreground)' }}
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
/>
|
||||
<Tooltip content={<CustomTooltip />} cursor={{ fill: 'var(--color-accent)' }} />
|
||||
<Bar dataKey="count" radius={[0, 4, 4, 0]}>
|
||||
{chartData.map((_, i) => (
|
||||
<Cell key={i} fill={COLORS[i % COLORS.length]} />
|
||||
))}
|
||||
</Bar>
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
import { useState, useMemo } from 'react';
|
||||
import {
|
||||
AreaChart,
|
||||
Area,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
} from 'recharts';
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { useYearlyWatchCount } from '@/hooks/useStatistics';
|
||||
|
||||
const MONTH_LABELS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
|
||||
type Filter = 'all' | 'movies' | 'episodes';
|
||||
|
||||
function buildChartData(
|
||||
series: { month: number; count: string }[],
|
||||
movies: { month: number; count: string }[],
|
||||
) {
|
||||
return MONTH_LABELS.map((label, i) => {
|
||||
const monthNum = i + 1;
|
||||
const s = series.find((x) => Number(x.month) === monthNum);
|
||||
const m = movies.find((x) => Number(x.month) === monthNum);
|
||||
return {
|
||||
month: label,
|
||||
Episodes: s ? parseInt(s.count, 10) : 0,
|
||||
Movies: m ? parseInt(m.count, 10) : 0,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export function WatchActivityChart() {
|
||||
const currentYear = new Date().getFullYear();
|
||||
const [year, setYear] = useState(currentYear);
|
||||
const [filter, setFilter] = useState<Filter>('all');
|
||||
|
||||
const { data, isLoading } = useYearlyWatchCount(year);
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
if (!data) return [];
|
||||
return buildChartData(data.series, data.movies);
|
||||
}, [data]);
|
||||
|
||||
const years = Array.from({ length: 5 }, (_, i) => currentYear - i);
|
||||
|
||||
const showMovies = filter === 'all' || filter === 'movies';
|
||||
const showEpisodes = filter === 'all' || filter === 'episodes';
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<CardTitle>Watch Activity</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
Movies and episodes watched per month
|
||||
</CardDescription>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
{/* Filter tabs */}
|
||||
<div className="flex rounded-md border border-border overflow-hidden text-xs">
|
||||
{(['all', 'movies', 'episodes'] as Filter[]).map((f) => (
|
||||
<button
|
||||
key={f}
|
||||
onClick={() => setFilter(f)}
|
||||
className={`px-3 py-1.5 capitalize transition-colors ${
|
||||
filter === f
|
||||
? 'bg-primary text-primary-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent'
|
||||
}`}
|
||||
>
|
||||
{f}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{/* Year selector */}
|
||||
<select
|
||||
value={year}
|
||||
onChange={(e) => setYear(Number(e.target.value))}
|
||||
className="rounded-md border border-border bg-card text-foreground text-xs px-2 py-1.5 focus:outline-none focus:ring-1 focus:ring-ring"
|
||||
>
|
||||
{years.map((y) => (
|
||||
<option key={y} value={y}>{y}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<Skeleton className="h-64 w-full" />
|
||||
) : (
|
||||
<ResponsiveContainer width="100%" height={260}>
|
||||
<AreaChart data={chartData} margin={{ top: 4, right: 8, left: -16, bottom: 0 }}>
|
||||
<defs>
|
||||
<linearGradient id="colorMovies" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="var(--color-chart-1)" stopOpacity={0.3} />
|
||||
<stop offset="95%" stopColor="var(--color-chart-1)" stopOpacity={0} />
|
||||
</linearGradient>
|
||||
<linearGradient id="colorEpisodes" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="5%" stopColor="var(--color-chart-2)" stopOpacity={0.3} />
|
||||
<stop offset="95%" stopColor="var(--color-chart-2)" stopOpacity={0} />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--color-border)" />
|
||||
<XAxis
|
||||
dataKey="month"
|
||||
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
|
||||
contentStyle={{
|
||||
backgroundColor: 'var(--color-card)',
|
||||
border: '1px solid var(--color-border)',
|
||||
borderRadius: '8px',
|
||||
fontSize: '13px',
|
||||
color: 'var(--color-card-foreground)',
|
||||
}}
|
||||
cursor={{ stroke: 'var(--color-border)', strokeWidth: 1 }}
|
||||
/>
|
||||
<Legend
|
||||
formatter={(value) => (
|
||||
<span style={{ fontSize: 12, color: 'var(--color-muted-foreground)' }}>{value}</span>
|
||||
)}
|
||||
/>
|
||||
{showMovies && (
|
||||
<Area
|
||||
type="monotone"
|
||||
dataKey="Movies"
|
||||
stroke="var(--color-chart-1)"
|
||||
strokeWidth={2}
|
||||
fill="url(#colorMovies)"
|
||||
dot={false}
|
||||
activeDot={{ r: 4, strokeWidth: 0, fill: 'var(--color-chart-1)' }}
|
||||
/>
|
||||
)}
|
||||
{showEpisodes && (
|
||||
<Area
|
||||
type="monotone"
|
||||
dataKey="Episodes"
|
||||
stroke="var(--color-chart-2)"
|
||||
strokeWidth={2}
|
||||
fill="url(#colorEpisodes)"
|
||||
dot={false}
|
||||
activeDot={{ r: 4, strokeWidth: 0, fill: 'var(--color-chart-2)' }}
|
||||
/>
|
||||
)}
|
||||
</AreaChart>
|
||||
</ResponsiveContainer>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import { useMemo } from 'react';
|
||||
import {
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Legend,
|
||||
ResponsiveContainer,
|
||||
} from 'recharts';
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { useYearlyWatchTime } from '@/hooks/useStatistics';
|
||||
|
||||
const MONTH_LABELS = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
|
||||
function minutesToHours(minutes: number) {
|
||||
return Math.round((minutes / 60) * 10) / 10;
|
||||
}
|
||||
|
||||
function buildChartData(
|
||||
series: { month: number; watchTime: string }[],
|
||||
movies: { month: number; watchTime: string }[],
|
||||
) {
|
||||
return MONTH_LABELS.map((label, i) => {
|
||||
const monthNum = i + 1;
|
||||
const s = series.find((x) => Number(x.month) === monthNum);
|
||||
const m = movies.find((x) => Number(x.month) === monthNum);
|
||||
return {
|
||||
month: label,
|
||||
'TV Series': s ? minutesToHours(parseInt(s.watchTime, 10)) : 0,
|
||||
Movies: m ? minutesToHours(parseInt(m.watchTime, 10)) : 0,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
interface CustomTooltipProps {
|
||||
active?: boolean;
|
||||
payload?: { name: string; value: number; color: string }[];
|
||||
label?: string;
|
||||
}
|
||||
|
||||
function CustomTooltip({ active, payload, label }: CustomTooltipProps) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const total = payload.reduce((sum, p) => sum + p.value, 0);
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
backgroundColor: 'var(--color-card)',
|
||||
border: '1px solid var(--color-border)',
|
||||
borderRadius: '8px',
|
||||
padding: '10px 14px',
|
||||
fontSize: '13px',
|
||||
color: 'var(--color-card-foreground)',
|
||||
}}
|
||||
>
|
||||
<p style={{ fontWeight: 600, marginBottom: 6 }}>{label}</p>
|
||||
{payload.map((p) => (
|
||||
<p key={p.name} style={{ color: p.color }}>
|
||||
{p.name}: {p.value}h
|
||||
</p>
|
||||
))}
|
||||
{payload.length > 1 && (
|
||||
<p style={{ color: 'var(--color-muted-foreground)', marginTop: 4, borderTop: '1px solid var(--color-border)', paddingTop: 4 }}>
|
||||
Total: {total}h
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function WatchTimeChart({ year, onYearChange }: { year: number; onYearChange: (y: number) => void }) {
|
||||
const currentYear = new Date().getFullYear();
|
||||
const { data, isLoading } = useYearlyWatchTime(year);
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
if (!data) return [];
|
||||
return buildChartData(data.series, data.movies);
|
||||
}, [data]);
|
||||
|
||||
const years = Array.from({ length: 5 }, (_, i) => currentYear - i);
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<CardTitle>Hours Watched</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
Total watch time per month, split by movies and TV series
|
||||
</CardDescription>
|
||||
</div>
|
||||
<select
|
||||
value={year}
|
||||
onChange={(e) => onYearChange(Number(e.target.value))}
|
||||
className="rounded-md border border-border bg-card text-foreground text-xs px-2 py-1.5 self-start focus:outline-none focus:ring-1 focus:ring-ring"
|
||||
>
|
||||
{years.map((y) => (
|
||||
<option key={y} value={y}>{y}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
{isLoading ? (
|
||||
<Skeleton className="h-56 w-full" />
|
||||
) : (
|
||||
<ResponsiveContainer width="100%" height={220}>
|
||||
<BarChart data={chartData} margin={{ top: 4, right: 8, left: -16, bottom: 0 }}>
|
||||
<CartesianGrid strokeDasharray="3 3" stroke="var(--color-border)" vertical={false} />
|
||||
<XAxis
|
||||
dataKey="month"
|
||||
tick={{ fontSize: 12, fill: 'var(--color-muted-foreground)' }}
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
/>
|
||||
<YAxis
|
||||
unit="h"
|
||||
tick={{ fontSize: 12, fill: 'var(--color-muted-foreground)' }}
|
||||
axisLine={false}
|
||||
tickLine={false}
|
||||
/>
|
||||
<Tooltip content={<CustomTooltip />} cursor={{ fill: 'var(--color-accent)' }} />
|
||||
<Legend
|
||||
formatter={(value) => (
|
||||
<span style={{ fontSize: 12, color: 'var(--color-muted-foreground)' }}>{value}</span>
|
||||
)}
|
||||
/>
|
||||
<Bar
|
||||
dataKey="Movies"
|
||||
stackId="a"
|
||||
fill="var(--color-chart-1)"
|
||||
radius={[0, 0, 0, 0]}
|
||||
/>
|
||||
<Bar
|
||||
dataKey="TV Series"
|
||||
stackId="a"
|
||||
fill="var(--color-chart-2)"
|
||||
radius={[4, 4, 0, 0]}
|
||||
/>
|
||||
</BarChart>
|
||||
</ResponsiveContainer>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user