"use client"; import { useMemo } from "react"; import { AreaChart, ChartTooltip } from "@mantine/charts"; import { Stack, useMantineTheme } from "@mantine/core"; import { useElementSize } from "@mantine/hooks"; import { ReferenceLine, XAxis } from "recharts"; import { useRequiredBoard } from "@homarr/boards/context"; import type { SpeedtestTrackerResult } from "@homarr/integrations/types"; import { useScopedI18n } from "@homarr/translation/client"; import { SectionLabel } from "./section-label"; interface XAxisTicks { midnightTs: number | null; xTicks: number[]; topDateTicks: number[]; } function buildXAxisTicks(data: { ts: number }[]): XAxisTicks { if (data.length === 0) return { midnightTs: null, xTicks: [], topDateTicks: [] }; const first = data[0]?.ts ?? 0; const last = data.at(-1)?.ts ?? 0; const twoHourMs = 2 * 60 * 60 * 1000; const startTick = Math.ceil(first / twoHourMs) * twoHourMs; const tickSet = new Set(); for (let t = startTick; t <= last; t += twoHourMs) tickSet.add(t); const midnightDate = new Date(first); midnightDate.setHours(24, 0, 0, 0); const mTs = midnightDate.getTime(); const foundMidnight = mTs > first && mTs < last ? mTs : null; if (foundMidnight) tickSet.add(foundMidnight); const topTicks: number[] = [first]; if (foundMidnight) topTicks.push(foundMidnight); return { midnightTs: foundMidnight, xTicks: Array.from(tickSet).sort((tsA, tsB) => tsA - tsB), topDateTicks: topTicks, }; } function makeXTickRenderer(midnightTs: number | null) { return ({ x, y, payload }: { x: number | string; y: number | string; payload: { value: number } }) => { const date = new Date(payload.value); const timeStr = date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); const isMidnight = midnightTs !== null && payload.value === midnightTs; const dateStr = isMidnight ? date.toLocaleDateString([], { month: "short", day: "numeric" }) : null; return ( {timeStr} {dateStr && ( {dateStr} )} ); }; } function renderTopDateTick(props: { x: number | string; y: number | string; payload: { value: number }; index: number; }) { const { x, y, payload, index } = props; const date = new Date(payload.value); const dateStr = date.toLocaleDateString([], { weekday: "short", month: "short", day: "numeric" }); const anchor = index === 0 ? "start" : "middle"; return ( {dateStr} ); } function buildYAxisConfig(maxVal: number, stepOptions: { threshold: number; step: number }[]) { const step = (stepOptions.find((opt) => maxVal <= opt.threshold) ?? stepOptions.at(-1))?.step ?? 1; const roundedMax = Math.ceil(maxVal / step) * step || step; const ticks: number[] = []; for (let val = 0; val <= roundedMax; val += step) ticks.push(val); return { ticks, domain: [0, roundedMax] as [number, number] }; } function formatTooltipDate(label: number | string | undefined): string { const timestamp = typeof label === "number" ? label : Number(label); if (Number.isNaN(timestamp)) return ""; return new Date(timestamp).toLocaleString([], { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); } export function RecentResultsSection({ results, showPingGraph, }: { results: SpeedtestTrackerResult[]; showPingGraph: boolean; }) { const t = useScopedI18n("widget.speedtestTracker"); return ( {t("recentResults")}
{showPingGraph && }
); } function SpeedSubChart({ results, showLabel }: { results: SpeedtestTrackerResult[]; showLabel: boolean }) { const t = useScopedI18n("widget.speedtestTracker"); const { ref, height } = useElementSize(); return (
{showLabel && {t("recentResultsSpeed")}}
{height > 0 && }
); } function PingSubChart({ results }: { results: SpeedtestTrackerResult[] }) { const t = useScopedI18n("widget.speedtestTracker"); const { ref, height } = useElementSize(); return (
{t("recentResultsPing")}
{height > 0 && }
); } function SpeedHistoryChart({ results, height }: { results: SpeedtestTrackerResult[]; height: number }) { const board = useRequiredBoard(); const theme = useMantineTheme(); const t = useScopedI18n("widget.speedtestTracker"); const data = useMemo( () => [...results] .sort((resultA, resultB) => resultA.created_at.getTime() - resultB.created_at.getTime()) .filter((result) => (result.download_bits ?? 0) > 0) .map((result) => ({ ts: result.created_at.getTime(), Download: parseFloat(((result.download_bits ?? 0) / 1_000_000).toFixed(2)), Upload: result.upload_bits != null && result.upload_bits > 0 ? parseFloat((result.upload_bits / 1_000_000).toFixed(2)) : 0, })), [results], ); const yConfig = useMemo( () => buildYAxisConfig(Math.max(...data.map((item) => Math.max(item.Download, item.Upload)), 0), [ { threshold: 400, step: 100 }, { threshold: Infinity, step: 200 }, ]), [data], ); const { midnightTs, xTicks, topDateTicks } = useMemo(() => buildXAxisTicks(data), [data]); const renderXTick = useMemo(() => makeXTickRenderer(midnightTs), [midnightTs]); if (data.length === 0) return null; return ( `${val} Mbps`} xAxisProps={{ type: "number", domain: ["dataMin", "dataMax"], ticks: xTicks, tick: renderXTick, interval: 0, height: midnightTs !== null ? 38 : 28, }} yAxisProps={{ ticks: yConfig.ticks, domain: yConfig.domain, tickFormatter: (val: number) => `${val}`, width: 50, tick: { fontSize: 10 }, }} tooltipProps={{ // eslint-disable-next-line @typescript-eslint/no-explicit-any content: (props: any) => { const { label, payload, active } = props as { label?: number | string; payload?: Record[]; active?: boolean; }; if (!active || !payload?.length) return null; return ( `${val} Mbps`} /> ); }, }} > {xTicks.map((tickTs) => ( ))} ); } function PingHistoryChart({ results, height }: { results: SpeedtestTrackerResult[]; height: number }) { const board = useRequiredBoard(); const theme = useMantineTheme(); const t = useScopedI18n("widget.speedtestTracker"); const data = useMemo( () => [...results] .sort((resultA, resultB) => resultA.created_at.getTime() - resultB.created_at.getTime()) .map((result) => result.ping === null ? null : { ts: result.created_at.getTime(), Ping: result.ping, }, ) .filter((result) => result !== null), [results], ); const yConfig = useMemo( () => buildYAxisConfig(Math.max(...data.map((item) => item.Ping), 0), [ { threshold: 10, step: 2 }, { threshold: 50, step: 10 }, { threshold: 200, step: 50 }, { threshold: Infinity, step: 100 }, ]), [data], ); const { midnightTs, xTicks } = useMemo(() => buildXAxisTicks(data), [data]); const renderXTick = useMemo(() => makeXTickRenderer(midnightTs), [midnightTs]); if (data.length === 0) return null; return ( `${val} ms`} xAxisProps={{ type: "number", domain: ["dataMin", "dataMax"], ticks: xTicks, tick: renderXTick, interval: 0, height: midnightTs !== null ? 38 : 28, }} yAxisProps={{ ticks: yConfig.ticks, domain: yConfig.domain, tickFormatter: (val: number) => `${val}`, width: 50, tick: { fontSize: 10 }, }} tooltipProps={{ // eslint-disable-next-line @typescript-eslint/no-explicit-any content: (props: any) => { const { label, payload, active } = props as { label?: number | string; payload?: Record[]; active?: boolean; }; if (!active || !payload?.length) return null; return ( `${val} ms`} /> ); }, }} > {xTicks.map((tickTs) => ( ))} ); }