import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, } from "recharts"; import { useQuery } from "@tanstack/react-query"; import apiClient from "../../lib/api"; interface MonthlyTrendsProps { className?: string; days?: number; } interface TooltipProps { active?: boolean; payload?: Array<{ name: string; value: number; color: string; }>; label?: string; } export default function MonthlyTrends({ className, days = 365, }: MonthlyTrendsProps) { // Get pre-calculated monthly stats from the new endpoint const { data: monthlyData, isLoading } = useQuery({ queryKey: ["monthly-stats", days], queryFn: async () => { return await apiClient.getMonthlyTransactionStats(days); }, }); // Calculate number of months to display based on days filter const getMonthsToDisplay = (days: number): number => { if (days <= 30) return 1; if (days <= 180) return 6; if (days <= 365) return 12; return Math.ceil(days / 30); }; const monthsToDisplay = getMonthsToDisplay(days); const displayData = monthlyData ? monthlyData.slice(-monthsToDisplay) : []; if (isLoading) { return (

Monthly Spending Trends

); } if (displayData.length === 0) { return (

Monthly Spending Trends

No transaction data available
); } const CustomTooltip = ({ active, payload, label }: TooltipProps) => { if (active && payload && payload.length) { return (

{label}

{payload.map((entry, index) => (

{entry.name}: €{Math.abs(entry.value).toLocaleString()}

))}
); } return null; }; // Generate dynamic title based on time period const getTitle = (days: number): string => { if (days <= 30) return "Monthly Spending Trends (Last 30 Days)"; if (days <= 180) return "Monthly Spending Trends (Last 6 Months)"; if (days <= 365) return "Monthly Spending Trends (Last 12 Months)"; return `Monthly Spending Trends (Last ${Math.ceil(days / 30)} Months)`; }; return (

{getTitle(days)}

`€${value.toLocaleString()}`} /> } />
Income
Expenses
); }