"use client"; import { enUS, it } from "date-fns/locale"; import { CalendarIcon } from "lucide-react"; import * as React from "react"; import type { DateRange } from "react-day-picker"; import { Bar, BarChart, CartesianGrid, XAxis } from "recharts"; import { Button } from "~/components/ui/button"; import { Calendar } from "~/components/ui/calendar"; import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "~/components/ui/card"; import { type ChartConfig, ChartContainer, ChartTooltip, ChartTooltipContent, } from "~/components/ui/chart"; import { Popover, PopoverContent, PopoverTrigger, } from "~/components/ui/popover"; import { cn } from "~/lib/utils"; import { useTranslation } from "~/providers/I18nProvider"; const chartConfig = { value: { label: "Valore", color: "var(--chart-2)", }, } satisfies ChartConfig; type TimeserieBarChartProps = { title: string; description?: string; timeserie: { date: Date; value: number }[]; }; export default function TimeserieBarChart({ timeserie, title, description, }: TimeserieBarChartProps) { const { locale } = useTranslation(); const startDate = timeserie[0] ? new Date(timeserie[0].date) : new Date(2025, 1, 1); const endDate = timeserie[timeserie.length - 1] ? // biome-ignore lint/style/noNonNullAssertion: new Date(timeserie[timeserie.length - 1]!.date) : new Date(); const [range, setRange] = React.useState({ from: startDate, to: endDate, }); const filteredData = React.useMemo(() => { if (!range?.from && !range?.to) { return timeserie; } return timeserie.filter((item) => { // biome-ignore lint/style/noNonNullAssertion: return item.date >= range.from! && item.date <= range.to!; }); }, [range]); const DatePickerLocale = locale === "it" ? it : enUS; return ( {title} {description || "Analytics for the selected period."} {filteredData.length === 0 ? (
Nessun dato disponibile per il periodo selezionato.
) : ( { const date = new Date(String(value)); return date.toLocaleDateString("it-IT", { day: "numeric", month: "short", }); }} /> } /> )}
Totale:{" "} {filteredData.reduce((acc, item) => acc + item.value, 0)}
); }