"use client"; import { validNumber, type EvidenceSeries } from "@/components/dashboard/scan-terminal/temperature-chart-logic"; type TooltipSeries = Pick; type TooltipRow = TooltipSeries & { value: number }; function isRunwayTooltipSeries(seriesKey: string) { return seriesKey.startsWith("runway_"); } function nearestSeriesValue( data: Array>, seriesKey: string, activeIndex: number, ) { if (!data.length || activeIndex < 0) return null; for (let offset = 0; offset < data.length; offset += 1) { const left = activeIndex - offset; if (left >= 0) { const value = validNumber(data[left]?.[seriesKey]); if (value !== null) return value; } const right = activeIndex + offset; if (right < data.length) { const value = validNumber(data[right]?.[seriesKey]); if (value !== null) return value; } } return null; } export function TemperatureTooltipContent({ active, label, payload, data, series, tempSymbol = "°C", }: { active?: boolean; label?: string | number; payload?: ReadonlyArray<{ payload?: Record }>; data: Array>; series: TooltipSeries[]; tempSymbol?: string; }) { if (!active || !payload?.length || !series.length) return null; const activePoint = payload[0]?.payload || {}; const rows = buildTooltipRows(activePoint, data, series); if (!rows.length) return null; return (
{label}
{rows.slice(0, 8).map((item) => (
{item.label} {item.value.toFixed(2)}{tempSymbol}
))}
); } function buildTooltipRows( activePoint: Record, data: Array>, series: TooltipSeries[], ): TooltipRow[] { const activeIndex = data.findIndex((point) => point.ts === activePoint.ts); return series .map((item) => { const directValue = validNumber(activePoint[item.key]); const value = directValue ?? ( isRunwayTooltipSeries(item.key) ? null : nearestSeriesValue(data, item.key, activeIndex) ); return value === null ? null : { ...item, value }; }) .filter((item): item is TooltipRow => item !== null); } export const __buildTemperatureTooltipRowsForTest = buildTooltipRows;