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 (
{label}
{payload.map((entry, index) => ({entry.name}: €{Math.abs(entry.value).toLocaleString()}
))}