import type { MCPCostHistogramResponse } from "@/lib/types/logs"; import { useMemo } from "react"; import { Area, AreaChart, Bar, BarChart, CartesianGrid, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"; import { CHART_COLORS, formatCost, formatFullTimestamp, formatTimestamp } from "../../utils/chartUtils"; import { ChartErrorBoundary } from "./chartErrorBoundary"; import type { ChartType } from "./chartTypeToggle"; interface MCPCostChartProps { data: MCPCostHistogramResponse | null; chartType: ChartType; startTime: number; endTime: number; } function CustomTooltip({ active, payload }: any) { if (!active || !payload || !payload.length) return null; const data = payload[0]?.payload; if (!data) return null; return (
{formatFullTimestamp(data.timestamp)}
Cost {formatCost(data.total_cost)}
); } export function MCPCostChart({ data, chartType, startTime, endTime }: MCPCostChartProps) { const chartData = useMemo(() => { if (!data?.buckets || !data.bucket_size_seconds) { return []; } return data.buckets.map((bucket, index) => ({ ...bucket, index, formattedTime: formatTimestamp(bucket.timestamp, data.bucket_size_seconds), })); }, [data]); if (!data?.buckets || chartData.length === 0) { return
No data available
; } const commonProps = { data: chartData, margin: { top: 6, right: 4, left: 4, bottom: 0 }, }; return ( {chartType === "bar" ? ( chartData[Math.round(idx)]?.formattedTime || ""} interval="preserveStartEnd" /> formatCost(v)} domain={[0, (dataMax: number) => Math.max(dataMax, 0.01)]} allowDataOverflow={false} /> } cursor={{ fill: "#8c8c8f", fillOpacity: 0.15 }} /> ) : ( chartData[Math.round(idx)]?.formattedTime || ""} interval="preserveStartEnd" /> formatCost(v)} domain={[0, (dataMax: number) => Math.max(dataMax, 0.01)]} allowDataOverflow={false} /> } /> )} ); }