"use client"; import { useEffect } from "react"; import { createPortal } from "react-dom"; /** * Analytics metrics payload returned by the custom backend API */ export interface AnalyticsData { totalViews: number; todayViews: number; uniqueVisitors: number; lastPing?: number; latencyMs?: number; devices: { Desktop: number; Mobile: number; }; } interface AnalyticsModalProps { isOpen: boolean; onClose: () => void; stats: AnalyticsData; } /** * Formats unix timestamps into relative time strings (e.g., "5s ago", "2m ago") */ function getTimeAgo(timestamp?: number): string { if (!timestamp) return "just now"; const seconds = Math.floor((Date.now() - timestamp * 1000) / 1000); if (seconds < 10) return "just now"; if (seconds < 60) return `${seconds}s ago`; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `${minutes}m ago`; const hours = Math.floor(minutes / 60); return `${hours}h ago`; } /** * Terminal-style modal inspector for viewing live traffic and system health metric */ export default function AnalyticsModal({ isOpen, onClose, stats, }: AnalyticsModalProps) { // Global hotkeys to dismiss the modal (ESC) useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; if (isOpen) window.addEventListener("keydown", handleKeyDown); return () => window.removeEventListener("keydown", handleKeyDown); }, [isOpen, onClose]); if (!isOpen) return null; // Calculate device traffic ratio percentages const desktopCount = stats.devices?.Desktop || 0; const mobileCount = stats.devices?.Mobile || 0; const totalDeviceCount = desktopCount + mobileCount || 1; const desktopPercent = Math.round((desktopCount / totalDeviceCount) * 100); // Render modal into document.body via portal to escape parent z-index constraints return createPortal(
{/* Terminal Card Container */}
e.stopPropagation()} > {/* Glow Effects */}
{/* Top Window Bar */}
{/* Window Controls */}
{/* Simulated process header */} ~/ analytics.sh PID: {Math.floor(Math.random() * 8000 + 1000)}
{/* System Command Header */}
$ sys.analytics --inspect
STATUS: ONLINE LATENCY:{" "} {stats.latencyMs ?? "<1"}ms
{/* Primary Traffic Metrics Grid */}
views_today #1
{stats.todayViews}
unique_visitors #2
{stats.uniqueVisitors}
{/* Total Hits & Device Breakdown */}
total_hits: {stats.totalViews}
{/* Device ratio progress bar */}
device_ratio: 💻 {desktopCount} / 📱{" "} {mobileCount}
{/* GDPR & Activity Footer */}
GDPR_ANONYMIZED: TRUE LAST_PING:{" "} {getTimeAgo(stats.lastPing)}
, document.body, ); }