import { useState, useEffect } from "react"; import { useQuery } from "@tanstack/react-query"; import { useToast } from "@/hooks/use-toast"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Loader2, RefreshCw } from "lucide-react"; import DashboardLayout from "@/layouts/dashboard-layout"; import { Badge } from "@/components/ui/badge"; import { Label } from "@/components/ui/label"; // Define the audit log type based on schema interface AuditLog { id: number; timestamp: string; userId: number | null; action: string; targetId: string; details: Record; connectionId: number; } // Define the connection type for filtering interface Connection { id: number; name: string; } export default function AuditLogsPage() { const { toast } = useToast(); const [selectedConnection, setSelectedConnection] = useState("all"); // Fetch LDAP connections for the filter dropdown const { data: connections = [], isLoading: isLoadingConnections } = useQuery({ queryKey: ["/api/connections"], staleTime: 60000, }); // Fetch audit logs with connection filter if needed const { data: auditLogs = [], isLoading: isLoadingLogs, refetch, isRefetching } = useQuery({ queryKey: [ selectedConnection === "all" ? "/api/audit-logs" : `/api/connections/${selectedConnection}/audit-logs` ], staleTime: 30000, }); // Format date for display const formatDate = (dateString: string) => { const date = new Date(dateString); return new Intl.DateTimeFormat("en-US", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" }).format(date); }; // Format the action for display const formatAction = (action: string) => { return action.replace(/_/g, " ").toLowerCase().replace(/\b\w/g, (c) => c.toUpperCase()); }; // Get the connection name from ID const getConnectionName = (connectionId: number) => { const connection = connections.find(c => c.id === connectionId); return connection ? connection.name : `Connection ${connectionId}`; }; // Get the appropriate badge color based on action const getActionColor = (action: string) => { if (action.includes("ADD") || action.includes("CREATE")) return "bg-green-600"; if (action.includes("REMOVE") || action.includes("DELETE")) return "bg-red-600"; if (action.includes("MOVE") || action.includes("UPDATE")) return "bg-blue-600"; return "bg-gray-600"; }; return (

Audit Logs

Activity Log A detailed record of all actions performed in the system {(isLoadingLogs || isRefetching) ? (
) : auditLogs.length === 0 ? (
No audit logs found
) : (
Timestamp Action Target Connection Details {auditLogs.map((log) => ( {formatDate(log.timestamp)} {formatAction(log.action)} {log.targetId} {getConnectionName(log.connectionId)}
                            {JSON.stringify(log.details, null, 2)}
                          
))}
)}
); }