"use client"; import { RiBankCard2Line, RiBarChartBoxLine } from "@remixicon/react"; import Image from "next/image"; import { Bar, BarChart, CartesianGrid, ReferenceLine, XAxis, YAxis, } from "recharts"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { type ChartConfig, ChartContainer, ChartTooltip, } from "@/components/ui/chart"; import type { CardDetailData } from "@/lib/relatorios/cartoes-report"; import { title_font } from "@/public/fonts/font_index"; type CardUsageChartProps = { data: CardDetailData["monthlyUsage"]; limit: number; card: { name: string; logo: string | null; }; }; const chartConfig = { amount: { label: "Uso", color: "#3b82f6", }, } satisfies ChartConfig; const resolveLogoPath = (logo: string | null) => { if (!logo) return null; if ( logo.startsWith("http://") || logo.startsWith("https://") || logo.startsWith("data:") ) { return logo; } return logo.startsWith("/") ? logo : `/logos/${logo}`; }; export function CardUsageChart({ data, limit, card }: CardUsageChartProps) { const formatCurrency = (value: number) => { return new Intl.NumberFormat("pt-BR", { style: "currency", currency: "BRL", minimumFractionDigits: 0, maximumFractionDigits: 0, }).format(value); }; const formatCurrencyCompact = (value: number) => { if (Math.abs(value) >= 1000) { return new Intl.NumberFormat("pt-BR", { style: "currency", currency: "BRL", minimumFractionDigits: 0, maximumFractionDigits: 0, notation: "compact", }).format(value); } return formatCurrency(value); }; // Always show last 12 months const chartData = data.slice(-12).map((item) => ({ month: item.periodLabel, amount: item.amount, })); const logoPath = resolveLogoPath(card.logo); return (
Histórico de Uso {/* Card logo and name */}
{logoPath ? ( {card.name} ) : ( )} {card.name}
{limit > 0 && ( )} { if (!active || !payload || payload.length === 0) { return null; } const data = payload[0].payload; const value = data.amount as number; const usagePercent = limit > 0 ? (value / limit) * 100 : 0; return (
{data.month}
Uso {formatCurrency(value)}
{limit > 0 && (
% do Limite {usagePercent.toFixed(0)}%
)}
); }} cursor={{ fill: "hsl(var(--muted))", opacity: 0.3 }} />
); }