import { memo, useMemo } from 'react'; import { ResponsiveContainer, ComposedChart, CartesianGrid, Line, Area, XAxis, YAxis, Tooltip, } from 'recharts'; import { __, sprintf } from '../../lib/i18n'; interface SeriesPoint { date: string; ai: number; other: number; } interface TrafficChartProps { series: SeriesPoint[]; showOther?: boolean; } function formatDayLabel(dateStr: string): string { const d = new Date(`${dateStr}T12:00:00`); if (Number.isNaN(d.getTime())) return dateStr; return d.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }); } function formatK(n: number): string { if (n >= 1000) return `${Math.round(n / 1000)}K`; return String(n); } interface TooltipProps { active?: boolean; payload?: Array<{ dataKey: string; value: number; color: string }>; label?: string; } function ChartTooltip({ active, payload, label }: TooltipProps) { if (!active || !payload?.length) return null; const ai = payload.find((p) => p.dataKey === 'ai'); const other = payload.find((p) => p.dataKey === 'other'); return (
{label &&

{label}

} {ai != null && (

{sprintf(__('%s AI referrals', 'llmagnet-llm-txt-generator'), formatK(ai.value))}

)} {other != null && (

{sprintf(__('%s other referrals', 'llmagnet-llm-txt-generator'), formatK(other.value))}

)}
); } export const ConnectionsTrafficChart = memo(function ConnectionsTrafficChart({ series, showOther = false, }: TrafficChartProps) { const chartData = useMemo( () => series.map((point) => ({ ...point, day: formatDayLabel(point.date), })), [series], ); if (!chartData.length) return null; return (
} cursor={{ stroke: 'rgba(124, 58, 237, 0.2)', strokeWidth: 1 }} /> {showOther && ( )}
); });