"use client"; import { useState, useEffect } from "react"; import { Modal } from "@/components/ui/Modal"; import { fmtBytes } from "@/lib/utils"; import { getViewAsHeaders } from "@/lib/admin-api"; import { Loader2, Monitor, Shield, Tag, Cpu, Clock, Network, ArrowDownToLine } from "lucide-react"; interface IpHistoryItem { ip_address: string; first_seen: string; last_seen: string; download: number; upload: number; flows: number; } interface DeviceMacDetailsModalProps { isOpen: boolean; onClose: () => void; macAddress: string; customLabel: string | null; defaultLabel: string; agentName: string; manufacturer: string; deviceType: string; } export default function DeviceMacDetailsModal({ isOpen, onClose, macAddress, customLabel, defaultLabel, agentName, manufacturer, deviceType, }: DeviceMacDetailsModalProps) { const [ips, setIps] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [isExporting, setIsExporting] = useState(false); const exportToPdf = async () => { setIsExporting(true); try { const { pdf } = await import("@react-pdf/renderer"); const { default: DeviceMacPdfDocument } = await import("./DeviceMacPdfDocument"); const reportDateStr = new Date().toLocaleString("id-ID", { timeZone: "Asia/Jakarta", day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" }) + " WIB"; const doc = ; const blob = await pdf(doc).toBlob(); const url = URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = `Device_MAC_Details_${macAddress.replace(/[:]/g, "")}.pdf`; link.click(); URL.revokeObjectURL(url); } catch (err) { console.error("Failed to export PDF:", err); alert("Failed to generate PDF report. Please try again."); } finally { setIsExporting(false); } }; useEffect(() => { if (!isOpen || !macAddress) return; setIsLoading(true); setError(null); fetch(`/api/dashboard/devices/mac-details?mac=${encodeURIComponent(macAddress)}`, { headers: getViewAsHeaders() }) .then((res) => res.json()) .then((data) => { if (data.ok) setIps(data.ips || []); else setError(data.error || "Failed to load device details."); }) .catch((err) => setError(err.message || "An error occurred while loading details.")) .finally(() => setIsLoading(false)); }, [isOpen, macAddress]); const DateCell = ({ dateStr }: { dateStr?: string }) => { if (!dateStr) return -; try { const d = new Date(dateStr); const datePart = d.toLocaleDateString("id-ID", { day: "2-digit", month: "2-digit", year: "numeric" }); const timePart = d.toLocaleTimeString("id-ID", { hour: "2-digit", minute: "2-digit" }) + " WIB"; return (
{datePart} {timePart}
); } catch { return {dateStr}; } }; return (
{/* Profile Card */}
MAC Address {macAddress}
Manufacturer {manufacturer || "Unknown"}
Device Labels
Custom: {customLabel ? ( {customLabel} ) : ( No label configured )}
System: {defaultLabel}
Device Type {deviceType || "Unknown"}
Network Agent {agentName}
{/* IPs Section */}

🔗 Associated IP Addresses

{isLoading ? (
Resolving active IP address histories...
) : error ? (
⚠️ {error}
) : ips.length === 0 ? (
No IP addresses recorded for this MAC address.
) : (
{ips.map((ip) => ( ))}
IP Address First Seen Last Seen Traffic (Down / Up) Flows
{ip.ip_address}
{fmtBytes(ip.download)} / {fmtBytes(ip.upload)} {ip.flows?.toLocaleString() ?? 0}
)}
); }