"use client"; import * as React from "react"; import { Area, AreaChart, ResponsiveContainer } from "recharts"; import { cn } from "@/lib/utils"; import type { BandwidthDataPoint } from "@/types"; interface BandwidthMiniChartProps { data: BandwidthDataPoint[]; currentBandwidth?: number; onClick?: () => void; className?: string; } export function BandwidthMiniChart({ data, currentBandwidth: externalBandwidth, onClick, className, }: BandwidthMiniChartProps) { // Transform data for the chart - combine sent and received for total bandwidth const chartData = React.useMemo(() => { // Fill in missing seconds with zeros for smooth chart if (data.length === 0) { // Create 60 seconds of zero data for the past minute const now = Math.floor(Date.now() / 1000); return Array.from({ length: 60 }, (_, i) => ({ time: now - (59 - i), bandwidth: 0, })); } const now = Math.floor(Date.now() / 1000); const result: { time: number; bandwidth: number }[] = []; // Get the last 60 seconds for (let i = 59; i >= 0; i--) { const targetTime = now - i; const point = data.find((d) => d.timestamp === targetTime); result.push({ time: targetTime, bandwidth: point ? point.bytes_sent + point.bytes_received : 0, }); } return result; }, [data]); // Find max value for scaling const _maxBandwidth = React.useMemo(() => { const max = Math.max(...chartData.map((d) => d.bandwidth), 1); return max; }, [chartData]); // Use external bandwidth if provided, otherwise calculate from last data point const currentBandwidth = externalBandwidth ?? chartData[chartData.length - 1]?.bandwidth ?? 0; // Format bytes to human readable const formatBytes = (bytes: number): string => { if (bytes === 0) return "0 B/s"; if (bytes < 1024) return `${bytes} B/s`; if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB/s`; return `${(bytes / (1024 * 1024)).toFixed(1)} MB/s`; }; return ( ); }