"use client"; import { useContext } from "react"; import { LineChart, Line, XAxis, YAxis, CartesianGrid, ResponsiveContainer, } from "recharts"; import { DataContext } from "@/lib/data-context"; import { greet } from "@/lib/utils"; const colors: Record = { lizzy: "#d985e6", alex: "#66d1c3", }; export default function UpdateChart() { const data = useContext(DataContext); if (!data.whois) return null; const groupedData: { [name: string]: { time: number; value: number }[] } = {}; data.whois.forEach((update, i, arr) => { if (!(update.name in groupedData)) groupedData[update.name] = []; const msPerHour = 60 * 60 * 1000; if (i === arr.length - 1) { const now = new Date(); groupedData[update.name].push({ time: update.time, value: 0 }); groupedData[update.name].push({ time: now.getTime(), value: (now.getTime() - update.time) / msPerHour, }); } if (i === 0) return; const prev = arr[i - 1]; groupedData[prev.name].push({ time: prev.time, value: 0 }); groupedData[prev.name].push({ time: update.time, value: (update.time - prev.time) / msPerHour, }); groupedData[prev.name].push({ time: update.time, value: 0 }); }); const uniqueNames = Object.keys(groupedData); const chartData = Object.entries(groupedData) .flatMap(([name, dataPoints]) => dataPoints.map((data) => ({ time: data.time, name, [name]: data.value })) ) .sort((a, b) => a.time - b.time); return (

{greet(data.whois?.at(-1)?.name ?? "Friend", new Date())}

((d) => `${d.toLocaleDateString()} ${d.getHours().toString().padStart(2, '0')}:${d.getMinutes().toString().padStart(2, '0')}`)(new Date(tick))} stroke="rgba(var(--foreground), 0.6)" /> (tick === 0 ? "" : `${tick} hrs`)} /> {uniqueNames.map((uniqueName, _index) => ( name === uniqueName)} name={uniqueName} stroke={colors[uniqueName] ?? "#ff0000"} strokeWidth={3} dot={false} isAnimationActive={false} /> ))}
{Object.keys(groupedData).map((name) => ( {name} ))}
); }