import { useMemo, useState } from 'react'; import { CartesianGrid, Legend, Line, LineChart, ResponsiveContainer, Tooltip, XAxis, YAxis, } from 'recharts'; type Dimension = 'total' | 'device' | 'browser' | 'os'; const DIMENSION_LABELS: Record = { total: 'Spolu', device: 'Zariadenie', browser: 'Prehliadač', os: 'OS', }; // Cycled per category line -- theme golds/creams, enough spread to stay // distinguishable across the handful of browsers/OSes/device types we expect. const PALETTE = ['#c9a84c', '#d8cba6', '#9c906c', '#f0d060', '#c2b58c', '#8a8064', '#7a6e4a']; interface Props { total: Record; byDevice: Record>; byBrowser: Record>; byOs: Record>; } export default function PageviewsChart({ total, byDevice, byBrowser, byOs }: Props) { const [dimension, setDimension] = useState('total'); const { rows, categories } = useMemo(() => { if (dimension === 'total') { const days = Object.keys(total).sort(); return { rows: days.map((day) => ({ day, n: total[day] })), categories: ['n'] }; } const byDay = dimension === 'device' ? byDevice : dimension === 'browser' ? byBrowser : byOs; const days = Object.keys(byDay).sort(); const categories = [...new Set(days.flatMap((d) => Object.keys(byDay[d])))].sort(); const rows = days.map((day) => { const row: Record = { day }; for (const c of categories) row[c] = byDay[day][c] ?? 0; return row; }); return { rows, categories }; }, [dimension, total, byDevice, byBrowser, byOs]); return (

Návštevy za deň

{(Object.keys(DIMENSION_LABELS) as Dimension[]).map((d) => ( ))}
{dimension !== 'total' && } {categories.map((c, i) => ( ))}
); }