diff --git a/react_frontend/src/components/dashboard/LibraryGrowthChart.tsx b/react_frontend/src/components/dashboard/LibraryGrowthChart.tsx index b985356..2ff9b62 100644 --- a/react_frontend/src/components/dashboard/LibraryGrowthChart.tsx +++ b/react_frontend/src/components/dashboard/LibraryGrowthChart.tsx @@ -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(); 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 ( @@ -86,7 +96,7 @@ export function LibraryGrowthChart() { 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) => ( - - ))} - +
+ {year === currentYear && monthTotal > 0 && ( + + This month: {monthTotal}h + + )} + {yearTotal > 0 && ( + + This year: {yearTotal}h + + )} + +