feat(dashboard): enhance charts with improved data handling and user interaction
This commit is contained in:
1 parent
fac8ba1d19
commit
984e2c8b50
5 files changed
+170
-76
No files matched your search
@@ -42,29 +42,39 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||
export function LibraryGrowthChart() {
|
||||
const allEntries = useLibraryEntries();
|
||||
const currentYear = new Date().getFullYear();
|
||||
const [year, setYear] = useState(currentYear);
|
||||
|
||||
const getEntryDate = (e: { createdAt?: string; date_first_seen?: string | null }) =>
|
||||
e.date_first_seen || e.createdAt || null;
|
||||
|
||||
const years = useMemo(() => {
|
||||
const set = new Set<number>();
|
||||
allEntries.forEach((e) => {
|
||||
if (e.createdAt) set.add(new Date(e.createdAt).getFullYear());
|
||||
const d = getEntryDate(e);
|
||||
if (d) set.add(new Date(d).getFullYear());
|
||||
});
|
||||
if (set.size === 0) set.add(currentYear);
|
||||
return Array.from(set).sort((a, b) => b - a);
|
||||
}, [allEntries]);
|
||||
}, [allEntries, currentYear]);
|
||||
|
||||
const [year, setYear] = useState(currentYear);
|
||||
|
||||
// Ensure selected year is always within the available years
|
||||
const effectiveYear = years.includes(year) ? year : years[0];
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
const library = allEntries.filter((e) => !e.watchlist);
|
||||
// Count entries added before this year for initial cumulative
|
||||
const beforeYear = library.filter(
|
||||
(e) => e.createdAt && new Date(e.createdAt).getFullYear() < year
|
||||
).length;
|
||||
const beforeYear = library.filter((e) => {
|
||||
const d = getEntryDate(e);
|
||||
return d && new Date(d).getFullYear() < effectiveYear;
|
||||
}).length;
|
||||
|
||||
const monthlyCounts = new Array(12).fill(0);
|
||||
library.forEach((e) => {
|
||||
if (!e.createdAt) return;
|
||||
const d = new Date(e.createdAt);
|
||||
if (d.getFullYear() === year) {
|
||||
monthlyCounts[d.getMonth()]++;
|
||||
const d = getEntryDate(e);
|
||||
if (!d) return;
|
||||
const date = new Date(d);
|
||||
if (date.getFullYear() === effectiveYear) {
|
||||
monthlyCounts[date.getMonth()]++;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -73,7 +83,7 @@ export function LibraryGrowthChart() {
|
||||
cumulative += monthlyCounts[i];
|
||||
return { month: label, count: monthlyCounts[i], cumulative };
|
||||
});
|
||||
}, [allEntries, year]);
|
||||
}, [allEntries, effectiveYear]);
|
||||
|
||||
return (
|
||||
<Card>
|
||||
@@ -86,7 +96,7 @@ export function LibraryGrowthChart() {
|
||||
</CardDescription>
|
||||
</div>
|
||||
<select
|
||||
value={year}
|
||||
value={effectiveYear}
|
||||
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"
|
||||
>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useMemo } from 'react';
|
||||
import { useMemo, useState } from 'react';
|
||||
import {
|
||||
PieChart,
|
||||
Pie,
|
||||
@@ -10,6 +10,8 @@ import {
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
||||
|
||||
type Scope = 'library' | 'watchlist';
|
||||
|
||||
const COLORS = ['var(--color-chart-1)', 'var(--color-chart-2)'];
|
||||
|
||||
interface CustomTooltipProps {
|
||||
@@ -39,26 +41,48 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||
|
||||
export function MediaTypeChart() {
|
||||
const allEntries = useLibraryEntries();
|
||||
const [scope, setScope] = useState<Scope>('library');
|
||||
|
||||
const chartData = useMemo(() => {
|
||||
const library = allEntries.filter((e) => !e.watchlist);
|
||||
const movies = library.filter((e) => !e.is_tvseries).length;
|
||||
const tv = library.filter((e) => e.is_tvseries).length;
|
||||
const filtered = scope === 'library'
|
||||
? allEntries.filter((e) => !e.watchlist)
|
||||
: allEntries.filter((e) => e.watchlist);
|
||||
const movies = filtered.filter((e) => !e.is_tvseries).length;
|
||||
const tv = filtered.filter((e) => e.is_tvseries).length;
|
||||
return [
|
||||
{ name: 'Movies', value: movies },
|
||||
{ name: 'TV Series', value: tv },
|
||||
];
|
||||
}, [allEntries]);
|
||||
}, [allEntries, scope]);
|
||||
|
||||
const total = chartData.reduce((s, d) => s + d.value, 0);
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
<CardTitle>Movies vs TV Series</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
{total} titles in your library
|
||||
</CardDescription>
|
||||
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
|
||||
<div>
|
||||
<CardTitle>Movies vs TV Series</CardTitle>
|
||||
<CardDescription className="mt-1">
|
||||
{total} titles in your {scope}
|
||||
</CardDescription>
|
||||
</div>
|
||||
<div className="flex rounded-md border border-border overflow-hidden text-xs self-start">
|
||||
{(['library', 'watchlist'] as Scope[]).map((s) => (
|
||||
<button
|
||||
key={s}
|
||||
onClick={() => setScope(s)}
|
||||
className={`px-3 py-1.5 capitalize transition-colors ${
|
||||
scope === s
|
||||
? 'bg-primary text-primary-foreground'
|
||||
: 'text-muted-foreground hover:bg-accent'
|
||||
}`}
|
||||
>
|
||||
{s}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ResponsiveContainer width="100%" height={260}>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import {
|
||||
BarChart,
|
||||
Bar,
|
||||
@@ -75,6 +76,7 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||
|
||||
export function RatingsChart() {
|
||||
const allEntries = useLibraryEntries();
|
||||
const navigate = useNavigate();
|
||||
const [filter, setFilter] = useState<Filter>('all');
|
||||
|
||||
const filteredEntries = useMemo(() => {
|
||||
@@ -134,7 +136,17 @@ export function RatingsChart() {
|
||||
tickLine={false}
|
||||
/>
|
||||
<Tooltip content={<CustomTooltip />} cursor={{ fill: 'var(--color-accent)' }} />
|
||||
<Bar dataKey="count" radius={[4, 4, 0, 0]}>
|
||||
<Bar
|
||||
dataKey="count"
|
||||
radius={[4, 4, 0, 0]}
|
||||
onClick={(data) => {
|
||||
const raw = (data as { raw?: number })?.raw;
|
||||
if (raw) {
|
||||
navigate(`/library?ratingMin=${raw}&ratingMax=${raw}`);
|
||||
}
|
||||
}}
|
||||
style={{ cursor: 'pointer' }}
|
||||
>
|
||||
{chartData.map((entry) => (
|
||||
<Cell
|
||||
key={entry.raw}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
import { useMemo, useState } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import {
|
||||
BarChart,
|
||||
Bar,
|
||||
XAxis,
|
||||
YAxis,
|
||||
CartesianGrid,
|
||||
Tooltip,
|
||||
Cell,
|
||||
Treemap,
|
||||
ResponsiveContainer,
|
||||
Tooltip,
|
||||
} from 'recharts';
|
||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
||||
@@ -17,7 +13,6 @@ 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)',
|
||||
@@ -39,17 +34,17 @@ function buildGenreData(entries: Entry[]) {
|
||||
return Object.entries(counts)
|
||||
.sort((a, b) => b[1] - a[1])
|
||||
.slice(0, TOP_N)
|
||||
.map(([genre, count]) => ({ genre, count }));
|
||||
.map(([genre, count], i) => ({ name: genre, size: count, fill: COLORS[i % COLORS.length] }));
|
||||
}
|
||||
|
||||
interface CustomTooltipProps {
|
||||
active?: boolean;
|
||||
payload?: { value: number; payload: { genre: string; count: number } }[];
|
||||
payload?: { payload: { name: string; size: number } }[];
|
||||
}
|
||||
|
||||
function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||
if (!active || !payload?.length) return null;
|
||||
const { genre, count } = payload[0].payload;
|
||||
const { name, size } = payload[0].payload;
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
@@ -61,25 +56,74 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||
color: 'var(--color-card-foreground)',
|
||||
}}
|
||||
>
|
||||
<p style={{ fontWeight: 600 }}>{genre}</p>
|
||||
<p style={{ color: 'var(--color-muted-foreground)' }}>{count} {count === 1 ? 'title' : 'titles'}</p>
|
||||
<p style={{ fontWeight: 600 }}>{name}</p>
|
||||
<p style={{ color: 'var(--color-muted-foreground)' }}>{size} {size === 1 ? 'title' : 'titles'}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function TreemapContent(props: {
|
||||
x: number;
|
||||
y: number;
|
||||
width: number;
|
||||
height: number;
|
||||
name: string;
|
||||
fill: string;
|
||||
}) {
|
||||
const { x, y, width, height, name, fill } = props;
|
||||
if (width < 30 || height < 20) return null;
|
||||
return (
|
||||
<g>
|
||||
<rect
|
||||
x={x}
|
||||
y={y}
|
||||
width={width}
|
||||
height={height}
|
||||
rx={4}
|
||||
fill={fill}
|
||||
stroke="var(--color-card)"
|
||||
strokeWidth={2}
|
||||
style={{ cursor: 'pointer' }}
|
||||
/>
|
||||
{width > 50 && height > 28 && (
|
||||
<text
|
||||
x={x + width / 2}
|
||||
y={y + height / 2}
|
||||
textAnchor="middle"
|
||||
dominantBaseline="central"
|
||||
fill="white"
|
||||
fontSize={Math.min(12, width / 6)}
|
||||
fontWeight={500}
|
||||
style={{ pointerEvents: 'none' }}
|
||||
>
|
||||
{name}
|
||||
</text>
|
||||
)}
|
||||
</g>
|
||||
);
|
||||
}
|
||||
|
||||
export function TopGenresChart() {
|
||||
const allEntries = useLibraryEntries();
|
||||
const navigate = useNavigate();
|
||||
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;
|
||||
// Only count actually watched entries (not watchlist, must be seen)
|
||||
const watched = allEntries.filter((e) => !e.watchlist && e.date_last_seen);
|
||||
if (filter === 'movies') return watched.filter((e) => !e.is_tvseries);
|
||||
if (filter === 'series') return watched.filter((e) => e.is_tvseries);
|
||||
return watched;
|
||||
}, [allEntries, filter]);
|
||||
|
||||
const chartData = useMemo(() => buildGenreData(filteredEntries), [filteredEntries]);
|
||||
|
||||
const handleClick = (node: { name?: string }) => {
|
||||
if (node?.name) {
|
||||
navigate(`/library?genre=${encodeURIComponent(node.name)}`);
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Card>
|
||||
<CardHeader>
|
||||
@@ -108,35 +152,16 @@ export function TopGenresChart() {
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
<ResponsiveContainer width="100%" height={Math.max(chartData.length * 36, 180)}>
|
||||
<BarChart
|
||||
layout="vertical"
|
||||
<ResponsiveContainer width="100%" height={280}>
|
||||
<Treemap
|
||||
data={chartData}
|
||||
margin={{ top: 4, right: 24, left: 0, bottom: 0 }}
|
||||
dataKey="size"
|
||||
aspectRatio={4 / 3}
|
||||
content={<TreemapContent x={0} y={0} width={0} height={0} name="" fill="" />}
|
||||
onClick={handleClick}
|
||||
>
|
||||
<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>
|
||||
<Tooltip content={<CustomTooltip />} />
|
||||
</Treemap>
|
||||
</ResponsiveContainer>
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
@@ -79,6 +79,17 @@ export function WatchTimeChart({ year, onYearChange }: { year: number; onYearCha
|
||||
return buildChartData(data.series, data.movies);
|
||||
}, [data]);
|
||||
|
||||
const currentMonth = new Date().getMonth(); // 0-based
|
||||
const monthTotal = useMemo(() => {
|
||||
if (!chartData.length || year !== currentYear) return 0;
|
||||
const m = chartData[currentMonth];
|
||||
return m ? Math.round(((m['Movies'] || 0) + (m['TV Series'] || 0)) * 10) / 10 : 0;
|
||||
}, [chartData, currentMonth, year, currentYear]);
|
||||
|
||||
const yearTotal = useMemo(() => {
|
||||
return Math.round(chartData.reduce((s, m) => s + (m['Movies'] || 0) + (m['TV Series'] || 0), 0) * 10) / 10;
|
||||
}, [chartData]);
|
||||
|
||||
const years = Array.from({ length: 5 }, (_, i) => currentYear - i);
|
||||
|
||||
return (
|
||||
@@ -91,15 +102,27 @@ export function WatchTimeChart({ year, onYearChange }: { year: number; onYearCha
|
||||
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 className="flex items-center gap-2 self-start flex-wrap">
|
||||
{year === currentYear && monthTotal > 0 && (
|
||||
<span className="rounded-md bg-accent px-2 py-1 text-xs font-medium text-accent-foreground">
|
||||
This month: {monthTotal}h
|
||||
</span>
|
||||
)}
|
||||
{yearTotal > 0 && (
|
||||
<span className="rounded-md bg-accent px-2 py-1 text-xs font-medium text-accent-foreground">
|
||||
This year: {yearTotal}h
|
||||
</span>
|
||||
)}
|
||||
<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 focus:outline-none focus:ring-1 focus:ring-ring"
|
||||
>
|
||||
{years.map((y) => (
|
||||
<option key={y} value={y}>{y}</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<CardContent>
|
||||
|
||||
Reference in new issue
Block a user