From 74cf627fb851b47fe8a0c64cb355f96feb770251 Mon Sep 17 00:00:00 2001 From: Yoga Pratama Date: Wed, 11 Nov 2026 17:45:34 +0700 Subject: [PATCH] fix(modals): portal modals to document.body and add interactive time filter to device & remote IP popups --- backend/routes/deviceDetailsHandler.js | 21 ++++- src/components/ui/AgentDetailModal.tsx | 17 +++- src/components/ui/AppDetailModal.tsx | 17 +++- src/components/ui/DeviceDetailModal.tsx | 98 ++++++++++++++++++++--- src/components/ui/RemoteIpDetailModal.tsx | 96 +++++++++++++++++++--- 5 files changed, 221 insertions(+), 28 deletions(-) diff --git a/backend/routes/deviceDetailsHandler.js b/backend/routes/deviceDetailsHandler.js index 8361f70..0f22bd1 100644 --- a/backend/routes/deviceDetailsHandler.js +++ b/backend/routes/deviceDetailsHandler.js @@ -74,8 +74,9 @@ module.exports = async function deviceDetailsHandler(req, res, helpers) { const flowFilter = { ...siteFilter, ...strictAgentFilter }; const rawTimeRange = String(req.query.timeRange ?? 'all'); + let tf = null; if (rawTimeRange !== 'all') { - const tf = getTimeFilter(req); + tf = getTimeFilter(req); if (tf) flowFilter.timestamp = tf; } @@ -89,6 +90,10 @@ module.exports = async function deviceDetailsHandler(req, res, helpers) { else if (mac) threatQuery.$or = [{ mac_address: mac }, { src_mac: mac }]; const appFilter = { ...siteFilter, ...strictAgentFilter, ip_address: ip }; + if (tf) { + appFilter.timestamp = tf; + threatQuery.timestamp = tf; + } const [deviceAppStats, flowsQuery, rawThreats] = await Promise.all([ ip ? DeviceAppStat.find(appFilter).sort({ timestamp: -1 }).lean() : [], @@ -157,8 +162,18 @@ module.exports = async function deviceDetailsHandler(req, res, helpers) { agentUuid = deviceAppStats[0]?.agent_uuid || flowsQuery[0]?.agent_uuid || agentUuid; const appsDl = apps.reduce((s, a) => s + (a.download || 0), 0); const appsUl = apps.reduce((s, a) => s + (a.upload || 0), 0); - const totalDownload = Math.max(device?.download || 0, appsDl); - const totalUpload = Math.max(device?.upload || 0, appsUl); + let totalDownload = appsDl; + let totalUpload = appsUl; + + if (rawTimeRange === 'all') { + totalDownload = Math.max(device?.download || 0, appsDl); + totalUpload = Math.max(device?.upload || 0, appsUl); + } else { + const flowsDl = flowsQuery.filter(f => (ip ? f.src_ip === ip : f.src_mac === mac)).reduce((s, f) => s + (f.download || 0), 0); + const flowsUl = flowsQuery.filter(f => (ip ? f.src_ip === ip : f.src_mac === mac)).reduce((s, f) => s + (f.upload || 0), 0); + totalDownload = Math.max(appsDl, flowsDl); + totalUpload = Math.max(appsUl, flowsUl); + } // ── Device metadata ─────────────────────────────────────────────────────── const flowMac = flowsQuery.find(f => f.src_ip === ip && f.src_mac && !f.src_mac.startsWith('00:16:3e:'))?.src_mac; diff --git a/src/components/ui/AgentDetailModal.tsx b/src/components/ui/AgentDetailModal.tsx index 9e0bd21..7d62882 100644 --- a/src/components/ui/AgentDetailModal.tsx +++ b/src/components/ui/AgentDetailModal.tsx @@ -1,6 +1,7 @@ "use client"; import { useState, useEffect } from "react"; +import { createPortal } from "react-dom"; import { fetchAgentDetails, AgentDetails } from "@/lib/api"; import { fmtBytes, getAppFavicon } from "@/lib/utils"; import { DeviceDetailModal } from "@/components/ui/DeviceDetailModal"; @@ -37,6 +38,15 @@ export function AgentDetailModal({ agentUuid, agentLabel, onClose }: Props) { const [selectedDevice, setSelectedDevice] = useState<{ ip: string; label: string | null; mac: string | null; agent_uuid?: string } | null>(null); const [isClosing, setIsClosing] = useState(false); const [selectedAppLabel, setSelectedAppLabel] = useState(null); + const [mounted, setMounted] = useState(false); + + useEffect(() => { + setMounted(true); + document.body.style.overflow = "hidden"; + return () => { + document.body.style.overflow = "unset"; + }; + }, []); const handleClose = () => { setIsClosing(true); @@ -77,7 +87,9 @@ export function AgentDetailModal({ agentUuid, agentLabel, onClose }: Props) { { key: "telemetry", label: "Telemetry", icon: }, ]; - return ( + if (!mounted) return null; + + return createPortal( <>
@@ -229,6 +241,7 @@ export function AgentDetailModal({ agentUuid, agentLabel, onClose }: Props) { onClose={() => setSelectedAppLabel(null)} /> )} - + , + document.body ); } diff --git a/src/components/ui/AppDetailModal.tsx b/src/components/ui/AppDetailModal.tsx index 9b9f70a..42fcc2a 100644 --- a/src/components/ui/AppDetailModal.tsx +++ b/src/components/ui/AppDetailModal.tsx @@ -1,6 +1,7 @@ "use client"; import { useState, useEffect } from "react"; +import { createPortal } from "react-dom"; import { fetchAppDetails, AppDetails } from "@/lib/api"; import { useTimeFilter } from "@/contexts/TimeFilterContext"; import { fmtBytes } from "@/lib/utils"; @@ -63,11 +64,20 @@ export function AppDetailModal({ appLabel, favicon, category, agentUuid, onClose const [error, setError] = useState(null); const [isClosing, setIsClosing] = useState(false); const [selectedIp, setSelectedIp] = useState(null); + const [mounted, setMounted] = useState(false); const { timeRange } = useTimeFilter(); const [page, setPage] = useState(1); const pageSize = 50; + useEffect(() => { + setMounted(true); + document.body.style.overflow = "hidden"; + return () => { + document.body.style.overflow = "unset"; + }; + }, []); + const handleClose = () => { setIsClosing(true); setTimeout(() => { onClose(); }, 480); @@ -88,7 +98,9 @@ export function AppDetailModal({ appLabel, favicon, category, agentUuid, onClose const startIndex = (page - 1) * pageSize; const paginatedIps = topIps.slice(startIndex, startIndex + pageSize); - return ( + if (!mounted) return null; + + return createPortal( <>
@@ -210,6 +222,7 @@ export function AppDetailModal({ appLabel, favicon, category, agentUuid, onClose onClose={() => setSelectedIp(null)} /> )} - + , + document.body ); } diff --git a/src/components/ui/DeviceDetailModal.tsx b/src/components/ui/DeviceDetailModal.tsx index a6f097e..7412cb7 100644 --- a/src/components/ui/DeviceDetailModal.tsx +++ b/src/components/ui/DeviceDetailModal.tsx @@ -1,8 +1,10 @@ "use client"; import { useState, useEffect } from "react"; +import { createPortal } from "react-dom"; import { fetchDeviceDetails, DeviceDetails, DeviceStat } from "@/lib/api"; +import { useTimeFilter } from "@/contexts/TimeFilterContext"; import { fmtBytes } from "@/lib/utils"; -import { Loader2, X, Monitor, Activity, Shield, Box, Server, Cpu, Clock, HardDrive, ArrowDownToLine, ArrowUpFromLine, RefreshCcw, Globe, Link2 } from "lucide-react"; +import { Loader2, X, Monitor, Activity, Shield, Box, Server, Cpu, Clock, HardDrive, ArrowDownToLine, ArrowUpFromLine, RefreshCcw, Globe, Link2, ChevronDown } from "lucide-react"; import ActiveDurationDisplay from "./ActiveDurationDisplay"; import DeviceAppsTab from "./DeviceAppsTab"; import DeviceProtocolsTab from "./DeviceProtocolsTab"; @@ -19,6 +21,16 @@ interface Props { onClose: () => void; } +const MODAL_TIME_OPTIONS = [ + { value: "5m", label: "Last 5 Minutes" }, + { value: "30m", label: "Last 30 Minutes" }, + { value: "1h", label: "Last 1 Hour" }, + { value: "1d", label: "Last 24 Hours" }, + { value: "7d", label: "Last 7 Days" }, + { value: "30d", label: "Last 30 Days" }, + { value: "all", label: "All Time" }, +] as const; + type Tab = "info" | "apps" | "protocols" | "domains" | "flows" | "threats"; const tabs: { id: Tab; label: string; icon: any }[] = [ @@ -31,6 +43,11 @@ const tabs: { id: Tab; label: string; icon: any }[] = [ ]; export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props) { + const { timeRange: globalTimeRange } = useTimeFilter(); + const [currentTimeRange, setCurrentTimeRange] = useState(globalTimeRange || '1d'); + const [isTimeDropdownOpen, setIsTimeDropdownOpen] = useState(false); + const [mounted, setMounted] = useState(false); + const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isRefreshing, setIsRefreshing] = useState(false); @@ -38,18 +55,32 @@ export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props) const [isClosing, setIsClosing] = useState(false); const [activeTab, setActiveTab] = useState("info"); + useEffect(() => { + setMounted(true); + document.body.style.overflow = "hidden"; + return () => { + document.body.style.overflow = "unset"; + }; + }, []); + + useEffect(() => { + if (globalTimeRange) { + setCurrentTimeRange(globalTimeRange); + } + }, [globalTimeRange]); + const handleClose = () => { setIsClosing(true); setTimeout(() => onClose(), 480); }; - const loadData = async (silent = false) => { + const loadData = async (silent = false, range = currentTimeRange) => { if (!silent) setIsLoading(true); else setIsRefreshing(true); setError(null); try { const agentUuid = deviceData?.agent_uuid || ''; - const res = await fetchDeviceDetails(ip, 'all', mac, agentUuid); + const res = await fetchDeviceDetails(ip, range, mac, agentUuid); setData(res); } catch (e: any) { if (!silent) setError(e.message); @@ -60,8 +91,8 @@ export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props) }; useEffect(() => { - loadData(false); - }, [ip, mac]); + loadData(false, currentTimeRange); + }, [ip, mac, currentTimeRange]); const [isExporting, setIsExporting] = useState(false); @@ -93,8 +124,10 @@ export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props) } }; - return ( -
+ if (!mounted) return null; + + return createPortal( +
-
+
+ {/* Time Filter Selector in Modal */} +
+ + + {isTimeDropdownOpen && ( + <> +
setIsTimeDropdownOpen(false)} /> +
+ {MODAL_TIME_OPTIONS.map((opt) => ( + + ))} +
+ + )} +
+ {data ? ( ) :
} -
@@ -238,6 +313,7 @@ export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props) ) : null}
-
+
, + document.body ); } diff --git a/src/components/ui/RemoteIpDetailModal.tsx b/src/components/ui/RemoteIpDetailModal.tsx index 12c5509..8545b7a 100644 --- a/src/components/ui/RemoteIpDetailModal.tsx +++ b/src/components/ui/RemoteIpDetailModal.tsx @@ -1,10 +1,12 @@ "use client"; import { useState, useEffect } from "react"; +import { createPortal } from "react-dom"; import { fetchRemoteIpDetails } from "@/lib/api"; +import { useTimeFilter } from "@/contexts/TimeFilterContext"; import { RemoteIpDetails } from "@/lib/api-detail-types"; import { fmtBytes } from "@/lib/utils"; -import { Loader2, X, Globe, Activity, Shield, Box, Server, Clock, ArrowDownToLine, ArrowUpFromLine, RefreshCcw, Link2, Laptop } from "lucide-react"; +import { Loader2, X, Globe, Activity, Shield, Box, Server, Clock, ArrowDownToLine, ArrowUpFromLine, RefreshCcw, Link2, Laptop, ChevronDown } from "lucide-react"; import ActiveDurationDisplay from "./ActiveDurationDisplay"; import { IpDetails } from "./IpDetails"; @@ -19,9 +21,24 @@ interface Props { onClose: () => void; } +const MODAL_TIME_OPTIONS = [ + { value: "5m", label: "Last 5 Minutes" }, + { value: "30m", label: "Last 30 Minutes" }, + { value: "1h", label: "Last 1 Hour" }, + { value: "1d", label: "Last 24 Hours" }, + { value: "7d", label: "Last 7 Days" }, + { value: "30d", label: "Last 30 Days" }, + { value: "all", label: "All Time" }, +] as const; + type Tab = "info" | "local_devices" | "protocols" | "domains" | "flows" | "threats"; export function RemoteIpDetailModal({ ip, onClose }: Props) { + const { timeRange: globalTimeRange } = useTimeFilter(); + const [currentTimeRange, setCurrentTimeRange] = useState(globalTimeRange || '1d'); + const [isTimeDropdownOpen, setIsTimeDropdownOpen] = useState(false); + const [mounted, setMounted] = useState(false); + const [data, setData] = useState(null); const [isLoading, setIsLoading] = useState(true); const [isRefreshing, setIsRefreshing] = useState(false); @@ -29,17 +46,31 @@ export function RemoteIpDetailModal({ ip, onClose }: Props) { const [isClosing, setIsClosing] = useState(false); const [activeTab, setActiveTab] = useState("info"); + useEffect(() => { + setMounted(true); + document.body.style.overflow = "hidden"; + return () => { + document.body.style.overflow = "unset"; + }; + }, []); + + useEffect(() => { + if (globalTimeRange) { + setCurrentTimeRange(globalTimeRange); + } + }, [globalTimeRange]); + const handleClose = () => { setIsClosing(true); setTimeout(() => onClose(), 480); }; - const loadData = async (silent = false) => { + const loadData = async (silent = false, range = currentTimeRange) => { if (!silent) setIsLoading(true); else setIsRefreshing(true); setError(null); try { - const res = await fetchRemoteIpDetails(ip, '1d'); + const res = await fetchRemoteIpDetails(ip, range); setData(res); } catch (e: any) { if (!silent) setError(e.message); @@ -50,8 +81,8 @@ export function RemoteIpDetailModal({ ip, onClose }: Props) { }; useEffect(() => { - loadData(false); - }, [ip]); + loadData(false, currentTimeRange); + }, [ip, currentTimeRange]); const tabs: { id: Tab; label: string; icon: any }[] = [ { id: "info", label: "Overview", icon: Globe }, @@ -62,7 +93,9 @@ export function RemoteIpDetailModal({ ip, onClose }: Props) { { id: "threats", label: "Threats", icon: Shield } ]; - return ( + if (!mounted) return null; + + return createPortal(
-
+
+ {/* Time Filter Selector in Modal */} +
+ + + {isTimeDropdownOpen && ( + <> +
setIsTimeDropdownOpen(false)} /> +
+ {MODAL_TIME_OPTIONS.map((opt) => ( + + ))} +
+ + )} +
+ @@ -226,6 +301,7 @@ export function RemoteIpDetailModal({ ip, onClose }: Props) { ) : null}
-
+
, + document.body ); }