import { api } from "@/utils/api"; import { CartesianGrid, Line, LineChart, Tooltip, XAxis, YAxis, } from "recharts"; import { ReportLineChartTooltip } from "./ReportLineChartTooltop"; import { useFormatDateInterval } from "@/hooks/useFormatDateInterval"; import { type IChartInput } from "@/types"; import { getChartColor } from "@/utils/theme"; import { ReportTable } from "./ReportTable"; import { useEffect, useRef, useState } from "react"; import { AutoSizer } from "@/components/AutoSizer"; type ReportLineChartProps = IChartInput & { showTable?: boolean; }; export function ReportLineChart({ interval, startDate, endDate, events, breakdowns, showTable, chartType, name, }: ReportLineChartProps) { const [visibleSeries, setVisibleSeries] = useState([]); const chart = api.chartMeta.chart.useQuery( { interval, chartType, startDate, endDate, events, breakdowns, name, }, { enabled: events.length > 0, }, ); const formatDate = useFormatDateInterval(interval); const ref = useRef(false); useEffect(() => { if (!ref.current && chart.data) { const max = 20; setVisibleSeries( chart.data?.series?.slice(0, max).map((serie) => serie.name) ?? [], ); // ref.current = true; } }, [chart.data]); return ( <> {chart.isSuccess && chart.data?.series?.[0]?.data && ( <> {({ width }) => ( } /> { return formatDate(m); }} tickLine={false} allowDuplicatedCategory={false} /> {chart.data?.series .filter((serie) => { return visibleSeries.includes(serie.name); }) .map((serie) => { const realIndex = chart.data?.series.findIndex( (item) => item.name === serie.name, ); const key = serie.name; const strokeColor = getChartColor(realIndex); return ( ); })} )} {showTable && ( )} )} ); }