import EasyModal, { useModal } from "ez-modal-react"; import { useQuery } from "@tanstack/react-query"; import { Modal, Button, Table, Spinner, Badge } from "react-bootstrap"; import { getWgClientLogs } from "src/api/backend"; import { IconNotes } from "@tabler/icons-react"; interface Props { clientId: number; clientName: string; } const WireGuardClientLogsModal = EasyModal.create(({ clientId, clientName }: Props) => { const modal = useModal(); const { data: logs, isLoading } = useQuery({ queryKey: ["wg-client-logs", clientId], queryFn: () => getWgClientLogs(clientId), refetchInterval: 5000 }); const handleClose = () => { modal.resolve(null); modal.hide(); }; const formatDate = (dateValue: string | number | undefined) => { if (!dateValue) return "Unknown Date"; try { const dateString = String(dateValue); let d: Date; // If it's pure numbers, it might be an epoch timestamp (ms) if (/^\d+$/.test(dateString) && Number(dateString) > 1000000000) { d = new Date(Number(dateString)); } else if (dateString.includes("-") && !dateString.endsWith("Z")) { // Ensure UTC parsing from raw SQLite timestamp d = new Date(dateString + "Z"); } else { d = new Date(dateString); } return isNaN(d.getTime()) ? dateString : d.toLocaleString(); } catch { return String(dateValue); } }; const parseLogMeta = (metaString: string | any) => { try { const meta = typeof metaString === 'string' ? JSON.parse(metaString) : metaString; if (meta && typeof meta === 'object') { // If it's an object with a message, return the message string, else stringify the whole object return meta.message ? String(meta.message) : JSON.stringify(meta); } return typeof meta === 'string' ? meta : JSON.stringify(meta); } catch { return String(metaString); } }; const getActionBadge = (action: string) => { switch (action) { case "connected": return Connected; case "disconnected": return Disconnected; case "uploaded-file": return File Upload; case "deleted-file": return File Deleted; default: return {action}; } }; return ( Event Logs: {clientName} {isLoading ? (
) : (
{logs && logs.length > 0 ? ( logs.map((log: any) => ( )) ) : ( )}
Date / Time Event Details
{formatDate(log.created_on || log.createdOn)} {getActionBadge(log.action)} {parseLogMeta(log.meta)}
No events recorded for this client yet.
)}
); }); export default WireGuardClientLogsModal;