feat(dashboard): enhance charts with improved data handling and user interaction
This commit is contained in:
1 parent
fac8ba1d19
commit
984e2c8b50
5 files changed
+159
-65
No files matched your search
@@ -42,29 +42,39 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
|||||||
export function LibraryGrowthChart() {
|
export function LibraryGrowthChart() {
|
||||||
const allEntries = useLibraryEntries();
|
const allEntries = useLibraryEntries();
|
||||||
const currentYear = new Date().getFullYear();
|
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 years = useMemo(() => {
|
||||||
const set = new Set<number>();
|
const set = new Set<number>();
|
||||||
allEntries.forEach((e) => {
|
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);
|
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 chartData = useMemo(() => {
|
||||||
const library = allEntries.filter((e) => !e.watchlist);
|
const library = allEntries.filter((e) => !e.watchlist);
|
||||||
// Count entries added before this year for initial cumulative
|
const beforeYear = library.filter((e) => {
|
||||||
const beforeYear = library.filter(
|
const d = getEntryDate(e);
|
||||||
(e) => e.createdAt && new Date(e.createdAt).getFullYear() < year
|
return d && new Date(d).getFullYear() < effectiveYear;
|
||||||
).length;
|
}).length;
|
||||||
|
|
||||||
const monthlyCounts = new Array(12).fill(0);
|
const monthlyCounts = new Array(12).fill(0);
|
||||||
library.forEach((e) => {
|
library.forEach((e) => {
|
||||||
if (!e.createdAt) return;
|
const d = getEntryDate(e);
|
||||||
const d = new Date(e.createdAt);
|
if (!d) return;
|
||||||
if (d.getFullYear() === year) {
|
const date = new Date(d);
|
||||||
monthlyCounts[d.getMonth()]++;
|
if (date.getFullYear() === effectiveYear) {
|
||||||
|
monthlyCounts[date.getMonth()]++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -73,7 +83,7 @@ export function LibraryGrowthChart() {
|
|||||||
cumulative += monthlyCounts[i];
|
cumulative += monthlyCounts[i];
|
||||||
return { month: label, count: monthlyCounts[i], cumulative };
|
return { month: label, count: monthlyCounts[i], cumulative };
|
||||||
});
|
});
|
||||||
}, [allEntries, year]);
|
}, [allEntries, effectiveYear]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
@@ -86,7 +96,7 @@ export function LibraryGrowthChart() {
|
|||||||
</CardDescription>
|
</CardDescription>
|
||||||
</div>
|
</div>
|
||||||
<select
|
<select
|
||||||
value={year}
|
value={effectiveYear}
|
||||||
onChange={(e) => setYear(Number(e.target.value))}
|
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"
|
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 {
|
import {
|
||||||
PieChart,
|
PieChart,
|
||||||
Pie,
|
Pie,
|
||||||
@@ -10,6 +10,8 @@ import {
|
|||||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||||
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
||||||
|
|
||||||
|
type Scope = 'library' | 'watchlist';
|
||||||
|
|
||||||
const COLORS = ['var(--color-chart-1)', 'var(--color-chart-2)'];
|
const COLORS = ['var(--color-chart-1)', 'var(--color-chart-2)'];
|
||||||
|
|
||||||
interface CustomTooltipProps {
|
interface CustomTooltipProps {
|
||||||
@@ -39,26 +41,48 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
|||||||
|
|
||||||
export function MediaTypeChart() {
|
export function MediaTypeChart() {
|
||||||
const allEntries = useLibraryEntries();
|
const allEntries = useLibraryEntries();
|
||||||
|
const [scope, setScope] = useState<Scope>('library');
|
||||||
|
|
||||||
const chartData = useMemo(() => {
|
const chartData = useMemo(() => {
|
||||||
const library = allEntries.filter((e) => !e.watchlist);
|
const filtered = scope === 'library'
|
||||||
const movies = library.filter((e) => !e.is_tvseries).length;
|
? allEntries.filter((e) => !e.watchlist)
|
||||||
const tv = library.filter((e) => e.is_tvseries).length;
|
: allEntries.filter((e) => e.watchlist);
|
||||||
|
const movies = filtered.filter((e) => !e.is_tvseries).length;
|
||||||
|
const tv = filtered.filter((e) => e.is_tvseries).length;
|
||||||
return [
|
return [
|
||||||
{ name: 'Movies', value: movies },
|
{ name: 'Movies', value: movies },
|
||||||
{ name: 'TV Series', value: tv },
|
{ name: 'TV Series', value: tv },
|
||||||
];
|
];
|
||||||
}, [allEntries]);
|
}, [allEntries, scope]);
|
||||||
|
|
||||||
const total = chartData.reduce((s, d) => s + d.value, 0);
|
const total = chartData.reduce((s, d) => s + d.value, 0);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
|
<div className="flex flex-col gap-2 sm:flex-row sm:items-start sm:justify-between">
|
||||||
|
<div>
|
||||||
<CardTitle>Movies vs TV Series</CardTitle>
|
<CardTitle>Movies vs TV Series</CardTitle>
|
||||||
<CardDescription className="mt-1">
|
<CardDescription className="mt-1">
|
||||||
{total} titles in your library
|
{total} titles in your {scope}
|
||||||
</CardDescription>
|
</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>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<ResponsiveContainer width="100%" height={260}>
|
<ResponsiveContainer width="100%" height={260}>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { useMemo, useState } from 'react';
|
import { useMemo, useState } from 'react';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
BarChart,
|
BarChart,
|
||||||
Bar,
|
Bar,
|
||||||
@@ -75,6 +76,7 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
|||||||
|
|
||||||
export function RatingsChart() {
|
export function RatingsChart() {
|
||||||
const allEntries = useLibraryEntries();
|
const allEntries = useLibraryEntries();
|
||||||
|
const navigate = useNavigate();
|
||||||
const [filter, setFilter] = useState<Filter>('all');
|
const [filter, setFilter] = useState<Filter>('all');
|
||||||
|
|
||||||
const filteredEntries = useMemo(() => {
|
const filteredEntries = useMemo(() => {
|
||||||
@@ -134,7 +136,17 @@ export function RatingsChart() {
|
|||||||
tickLine={false}
|
tickLine={false}
|
||||||
/>
|
/>
|
||||||
<Tooltip content={<CustomTooltip />} cursor={{ fill: 'var(--color-accent)' }} />
|
<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) => (
|
{chartData.map((entry) => (
|
||||||
<Cell
|
<Cell
|
||||||
key={entry.raw}
|
key={entry.raw}
|
||||||
|
|||||||
@@ -1,13 +1,9 @@
|
|||||||
import { useMemo, useState } from 'react';
|
import { useMemo, useState } from 'react';
|
||||||
|
import { useNavigate } from 'react-router-dom';
|
||||||
import {
|
import {
|
||||||
BarChart,
|
Treemap,
|
||||||
Bar,
|
|
||||||
XAxis,
|
|
||||||
YAxis,
|
|
||||||
CartesianGrid,
|
|
||||||
Tooltip,
|
|
||||||
Cell,
|
|
||||||
ResponsiveContainer,
|
ResponsiveContainer,
|
||||||
|
Tooltip,
|
||||||
} from 'recharts';
|
} from 'recharts';
|
||||||
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card';
|
||||||
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
import { useLibraryEntries } from '@/stores/useLibraryStore';
|
||||||
@@ -17,7 +13,6 @@ type Filter = 'all' | 'movies' | 'series';
|
|||||||
|
|
||||||
const TOP_N = 10;
|
const TOP_N = 10;
|
||||||
|
|
||||||
// Cycle through chart color vars for variety
|
|
||||||
const COLORS = [
|
const COLORS = [
|
||||||
'var(--color-chart-1)',
|
'var(--color-chart-1)',
|
||||||
'var(--color-chart-2)',
|
'var(--color-chart-2)',
|
||||||
@@ -39,17 +34,17 @@ function buildGenreData(entries: Entry[]) {
|
|||||||
return Object.entries(counts)
|
return Object.entries(counts)
|
||||||
.sort((a, b) => b[1] - a[1])
|
.sort((a, b) => b[1] - a[1])
|
||||||
.slice(0, TOP_N)
|
.slice(0, TOP_N)
|
||||||
.map(([genre, count]) => ({ genre, count }));
|
.map(([genre, count], i) => ({ name: genre, size: count, fill: COLORS[i % COLORS.length] }));
|
||||||
}
|
}
|
||||||
|
|
||||||
interface CustomTooltipProps {
|
interface CustomTooltipProps {
|
||||||
active?: boolean;
|
active?: boolean;
|
||||||
payload?: { value: number; payload: { genre: string; count: number } }[];
|
payload?: { payload: { name: string; size: number } }[];
|
||||||
}
|
}
|
||||||
|
|
||||||
function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
||||||
if (!active || !payload?.length) return null;
|
if (!active || !payload?.length) return null;
|
||||||
const { genre, count } = payload[0].payload;
|
const { name, size } = payload[0].payload;
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
@@ -61,25 +56,74 @@ function CustomTooltip({ active, payload }: CustomTooltipProps) {
|
|||||||
color: 'var(--color-card-foreground)',
|
color: 'var(--color-card-foreground)',
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<p style={{ fontWeight: 600 }}>{genre}</p>
|
<p style={{ fontWeight: 600 }}>{name}</p>
|
||||||
<p style={{ color: 'var(--color-muted-foreground)' }}>{count} {count === 1 ? 'title' : 'titles'}</p>
|
<p style={{ color: 'var(--color-muted-foreground)' }}>{size} {size === 1 ? 'title' : 'titles'}</p>
|
||||||
</div>
|
</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() {
|
export function TopGenresChart() {
|
||||||
const allEntries = useLibraryEntries();
|
const allEntries = useLibraryEntries();
|
||||||
|
const navigate = useNavigate();
|
||||||
const [filter, setFilter] = useState<Filter>('all');
|
const [filter, setFilter] = useState<Filter>('all');
|
||||||
|
|
||||||
const filteredEntries = useMemo(() => {
|
const filteredEntries = useMemo(() => {
|
||||||
const library = allEntries.filter((e) => !e.watchlist);
|
// Only count actually watched entries (not watchlist, must be seen)
|
||||||
if (filter === 'movies') return library.filter((e) => !e.is_tvseries);
|
const watched = allEntries.filter((e) => !e.watchlist && e.date_last_seen);
|
||||||
if (filter === 'series') return library.filter((e) => e.is_tvseries);
|
if (filter === 'movies') return watched.filter((e) => !e.is_tvseries);
|
||||||
return library;
|
if (filter === 'series') return watched.filter((e) => e.is_tvseries);
|
||||||
|
return watched;
|
||||||
}, [allEntries, filter]);
|
}, [allEntries, filter]);
|
||||||
|
|
||||||
const chartData = useMemo(() => buildGenreData(filteredEntries), [filteredEntries]);
|
const chartData = useMemo(() => buildGenreData(filteredEntries), [filteredEntries]);
|
||||||
|
|
||||||
|
const handleClick = (node: { name?: string }) => {
|
||||||
|
if (node?.name) {
|
||||||
|
navigate(`/library?genre=${encodeURIComponent(node.name)}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card>
|
<Card>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
@@ -108,35 +152,16 @@ export function TopGenresChart() {
|
|||||||
</div>
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
<ResponsiveContainer width="100%" height={Math.max(chartData.length * 36, 180)}>
|
<ResponsiveContainer width="100%" height={280}>
|
||||||
<BarChart
|
<Treemap
|
||||||
layout="vertical"
|
|
||||||
data={chartData}
|
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} />
|
<Tooltip content={<CustomTooltip />} />
|
||||||
<XAxis
|
</Treemap>
|
||||||
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>
|
</ResponsiveContainer>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
</Card>
|
</Card>
|
||||||
|
|||||||
@@ -79,6 +79,17 @@ export function WatchTimeChart({ year, onYearChange }: { year: number; onYearCha
|
|||||||
return buildChartData(data.series, data.movies);
|
return buildChartData(data.series, data.movies);
|
||||||
}, [data]);
|
}, [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);
|
const years = Array.from({ length: 5 }, (_, i) => currentYear - i);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -91,16 +102,28 @@ export function WatchTimeChart({ year, onYearChange }: { year: number; onYearCha
|
|||||||
Total watch time per month, split by movies and TV series
|
Total watch time per month, split by movies and TV series
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</div>
|
</div>
|
||||||
|
<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
|
<select
|
||||||
value={year}
|
value={year}
|
||||||
onChange={(e) => onYearChange(Number(e.target.value))}
|
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"
|
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) => (
|
{years.map((y) => (
|
||||||
<option key={y} value={y}>{y}</option>
|
<option key={y} value={y}>{y}</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent>
|
<CardContent>
|
||||||
{isLoading ? (
|
{isLoading ? (
|
||||||
|
|||||||
Reference in new issue
Block a user