fix: update ChartTooltip to correctly display current metric values in tooltip based on payload order

This commit is contained in:
Usman Baig
2026-01-19 18:34:53 +01:00
parent ecac8002ae
commit 43dd977c4c

View File

@@ -84,9 +84,13 @@ function ChartTooltip({
colors: typeof CHART_COLORS_LIGHT
}) {
if (!active || !payload?.length || !label) return null
const d = payload[0]
const value = d.value as number
const prev = metric === 'visitors' ? d.payload.prevVisitors : d.payload.prevPageviews
// * Recharts sends one payload entry per Area; order can be [prevSeries, currentSeries].
// * Use the entry for the current metric so the tooltip shows today's value, not yesterday's.
type PayloadItem = { dataKey?: string; value?: number; payload: { prevPageviews?: number; prevVisitors?: number; visitors?: number; pageviews?: number } }
const items = payload as PayloadItem[]
const current = items.find((p) => p.dataKey === metric) ?? items[items.length - 1]
const value = Number(current?.value ?? (current?.payload as Record<string, number>)?.[metric] ?? 0)
const prev = metric === 'visitors' ? current?.payload?.prevVisitors : current?.payload?.prevPageviews
const hasPrev = showComparison && prev != null
const delta =
hasPrev && (prev as number) > 0