fix(modals): portal modals to document.body and add interactive time filter to device & remote IP popups

This commit is contained in:
ypratama committed 2026-11-11 17:45:34 +07:00
1 parent 7abb56e328
commit 74cf627fb8
5 files changed
+221 -28

No files matched your search

+18 -3
View File
@@ -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;
+15 -2
View File
@@ -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<string | null>(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: <LineChart className="w-3.5 h-3.5" /> },
];
return (
if (!mounted) return null;
return createPortal(
<>
<div className="fixed inset-0 z-[9999] flex items-center justify-center p-4" onClick={handleClose}>
<div className={`absolute inset-0 bg-black/40 dark:bg-black/75 backdrop-blur-sm ${isClosing ? 'animate-overlay-out' : 'animate-overlay-in'}`} />
@@ -229,6 +241,7 @@ export function AgentDetailModal({ agentUuid, agentLabel, onClose }: Props) {
onClose={() => setSelectedAppLabel(null)}
/>
)}
</>
</>,
document.body
);
}
+15 -2
View File
@@ -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<string | null>(null);
const [isClosing, setIsClosing] = useState(false);
const [selectedIp, setSelectedIp] = useState<string | null>(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(
<>
<div className="fixed inset-0 z-[9999] flex items-center justify-center p-4" onClick={handleClose}>
<div className={`absolute inset-0 bg-black/40 dark:bg-black/75 backdrop-blur-sm ${isClosing ? 'animate-overlay-out' : 'animate-overlay-in'}`} />
@@ -210,6 +222,7 @@ export function AppDetailModal({ appLabel, favicon, category, agentUuid, onClose
onClose={() => setSelectedIp(null)}
/>
)}
</>
</>,
document.body
);
}
+87 -11
View File
@@ -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<string>(globalTimeRange || '1d');
const [isTimeDropdownOpen, setIsTimeDropdownOpen] = useState(false);
const [mounted, setMounted] = useState(false);
const [data, setData] = useState<DeviceDetails | null>(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<Tab>("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 (
<div className="fixed inset-0 z-50 flex items-center justify-center p-4" onClick={handleClose}>
if (!mounted) return null;
return createPortal(
<div className="fixed inset-0 z-[9999] flex items-center justify-center p-4" onClick={handleClose}>
<div className={`absolute inset-0 bg-black/60 backdrop-blur-md ${isClosing ? 'animate-overlay-out' : 'animate-overlay-in'}`} />
<div
className={`relative bg-card border border-border rounded-3xl w-full max-w-5xl h-[85vh] flex flex-col shadow-2xl shadow-black/10 dark:shadow-blue-900/20 overflow-hidden backdrop-blur-xl ${isClosing ? 'animate-modal-out' : 'animate-modal-in'}`}
@@ -133,12 +166,54 @@ export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props)
)}
</div>
</div>
<div className="flex items-center justify-between sm:justify-end gap-3 flex-shrink-0 w-full sm:w-auto border-t border-border/40 pt-3 sm:border-t-0 sm:pt-0">
<div className="flex items-center justify-between sm:justify-end gap-2.5 flex-shrink-0 w-full sm:w-auto border-t border-border/40 pt-3 sm:border-t-0 sm:pt-0">
{/* Time Filter Selector in Modal */}
<div className="relative">
<button
type="button"
onClick={() => setIsTimeDropdownOpen(!isTimeDropdownOpen)}
className="flex items-center gap-1.5 px-2.5 py-1.5 sm:px-3 sm:py-2 rounded-lg sm:rounded-xl text-xs font-semibold bg-secondary/80 hover:bg-secondary text-card-foreground border border-border transition-all shadow-sm cursor-pointer"
title="Filter time range"
>
<Clock className="w-3.5 h-3.5 text-blue-500" />
<span className="truncate max-w-[110px] sm:max-w-[140px]">
{MODAL_TIME_OPTIONS.find(o => o.value === currentTimeRange)?.label || currentTimeRange}
</span>
<ChevronDown className={`w-3 h-3 text-muted-foreground transition-transform duration-200 ${isTimeDropdownOpen ? 'rotate-180' : ''}`} />
</button>
{isTimeDropdownOpen && (
<>
<div className="fixed inset-0 z-[10000]" onClick={() => setIsTimeDropdownOpen(false)} />
<div className="absolute right-0 top-full mt-1.5 z-[10001] w-44 rounded-xl border border-border bg-card/95 backdrop-blur-xl p-1 shadow-2xl origin-top-right">
{MODAL_TIME_OPTIONS.map((opt) => (
<button
key={opt.value}
type="button"
onClick={() => {
setCurrentTimeRange(opt.value);
setIsTimeDropdownOpen(false);
}}
className={`flex items-center justify-between w-full rounded-lg px-2.5 py-1.5 text-left text-xs font-medium transition-colors ${
currentTimeRange === opt.value
? 'bg-blue-600 text-white'
: 'text-muted-foreground hover:bg-secondary hover:text-card-foreground'
}`}
>
<span>{opt.label}</span>
{currentTimeRange === opt.value && <span className="text-[10px]">✓</span>}
</button>
))}
</div>
</>
)}
</div>
{data ? (
<button
disabled={isExporting}
onClick={exportToPdf}
className="flex items-center gap-1.5 px-3 py-1.5 sm:px-3.5 sm:py-2 rounded-lg sm:rounded-xl text-xs font-semibold bg-blue-600 hover:bg-blue-500 text-white transition-all shadow-md shadow-blue-600/15 disabled:opacity-50 disabled:cursor-not-allowed"
className="flex items-center gap-1.5 px-3 py-1.5 sm:px-3.5 sm:py-2 rounded-lg sm:rounded-xl text-xs font-semibold bg-blue-600 hover:bg-blue-500 text-white transition-all shadow-md shadow-blue-600/15 disabled:opacity-50 disabled:cursor-not-allowed cursor-pointer"
>
{isExporting ? (
<Loader2 className="w-3 h-3 sm:w-3.5 sm:h-3.5 animate-spin" />
@@ -148,7 +223,7 @@ export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props)
Export PDF
</button>
) : <div />}
<button onClick={handleClose} className="p-1.5 sm:p-2 text-muted-foreground hover:text-card-foreground hover:bg-secondary rounded-lg sm:rounded-xl transition-all">
<button onClick={handleClose} className="p-1.5 sm:p-2 text-muted-foreground hover:text-card-foreground hover:bg-secondary rounded-lg sm:rounded-xl transition-all cursor-pointer">
<X className="w-4 h-4 sm:w-5 sm:h-5" />
</button>
</div>
@@ -238,6 +313,7 @@ export function DeviceDetailModal({ ip, mac = '', deviceData, onClose }: Props)
) : null}
</div>
</div>
</div>
</div>,
document.body
);
}
+86 -10
View File
@@ -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<string>(globalTimeRange || '1d');
const [isTimeDropdownOpen, setIsTimeDropdownOpen] = useState(false);
const [mounted, setMounted] = useState(false);
const [data, setData] = useState<RemoteIpDetails | null>(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<Tab>("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(
<div className="fixed inset-0 z-[9999] flex items-center justify-center p-4" onClick={handleClose}>
<div className={`absolute inset-0 bg-black/60 backdrop-blur-md ${isClosing ? 'animate-overlay-out' : 'animate-overlay-in'}`} />
<div
@@ -91,18 +124,60 @@ export function RemoteIpDetailModal({ ip, onClose }: Props) {
</div>
</div>
<div className="flex items-center justify-between sm:justify-end gap-2 flex-shrink-0 w-full sm:w-auto border-t border-border/40 pt-3 sm:border-t-0 sm:pt-0">
<div className="flex items-center justify-between sm:justify-end gap-2.5 flex-shrink-0 w-full sm:w-auto border-t border-border/40 pt-3 sm:border-t-0 sm:pt-0">
{/* Time Filter Selector in Modal */}
<div className="relative">
<button
type="button"
onClick={() => setIsTimeDropdownOpen(!isTimeDropdownOpen)}
className="flex items-center gap-1.5 px-2.5 py-1.5 sm:px-3 sm:py-2 rounded-lg sm:rounded-xl text-xs font-semibold bg-secondary/80 hover:bg-secondary text-card-foreground border border-border transition-all shadow-sm cursor-pointer"
title="Filter time range"
>
<Clock className="w-3.5 h-3.5 text-indigo-500" />
<span className="truncate max-w-[110px] sm:max-w-[140px]">
{MODAL_TIME_OPTIONS.find(o => o.value === currentTimeRange)?.label || currentTimeRange}
</span>
<ChevronDown className={`w-3 h-3 text-muted-foreground transition-transform duration-200 ${isTimeDropdownOpen ? 'rotate-180' : ''}`} />
</button>
{isTimeDropdownOpen && (
<>
<div className="fixed inset-0 z-[10000]" onClick={() => setIsTimeDropdownOpen(false)} />
<div className="absolute right-0 top-full mt-1.5 z-[10001] w-44 rounded-xl border border-border bg-card/95 backdrop-blur-xl p-1 shadow-2xl origin-top-right">
{MODAL_TIME_OPTIONS.map((opt) => (
<button
key={opt.value}
type="button"
onClick={() => {
setCurrentTimeRange(opt.value);
setIsTimeDropdownOpen(false);
}}
className={`flex items-center justify-between w-full rounded-lg px-2.5 py-1.5 text-left text-xs font-medium transition-colors ${
currentTimeRange === opt.value
? 'bg-indigo-600 text-white'
: 'text-muted-foreground hover:bg-secondary hover:text-card-foreground'
}`}
>
<span>{opt.label}</span>
{currentTimeRange === opt.value && <span className="text-[10px]">✓</span>}
</button>
))}
</div>
</>
)}
</div>
<button
onClick={() => loadData(true)}
disabled={isLoading || isRefreshing}
className="p-2 text-muted-foreground hover:text-indigo-500 hover:bg-indigo-500/10 rounded-full transition-all disabled:opacity-50"
className="p-2 text-muted-foreground hover:text-indigo-500 hover:bg-indigo-500/10 rounded-full transition-all disabled:opacity-50 cursor-pointer"
title="Refresh Data"
>
<RefreshCcw className="w-4 h-4" />
</button>
<button
onClick={handleClose}
className="p-2 text-muted-foreground hover:text-destructive hover:bg-destructive/10 rounded-full transition-all bg-black/5 dark:bg-white/5"
className="p-2 text-muted-foreground hover:text-destructive hover:bg-destructive/10 rounded-full transition-all bg-black/5 dark:bg-white/5 cursor-pointer"
>
<X className="w-4 h-4" />
</button>
@@ -226,6 +301,7 @@ export function RemoteIpDetailModal({ ip, onClose }: Props) {
) : null}
</div>
</div>
</div>
</div>,
document.body
);
}