"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 } 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); 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 formatFriendlyDate = (dateStr?: string) => { if (!dateStr) return "-"; try { const date = new Date(dateStr); return date.toLocaleString("id-ID", { day: "2-digit", month: "2-digit", year: "numeric", hour: "2-digit", minute: "2-digit", }) + " WIB"; } 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} {formatFriendlyDate(ip.first_seen)} {formatFriendlyDate(ip.last_seen)} {fmtBytes(ip.download)} / {fmtBytes(ip.upload)} {ip.flows}
)}
); }