import { useState, useEffect, useMemo } from 'react'; import { RefreshCw, Search, FileText, Trash2 } from 'lucide-react'; import { LogsDataGrid, type LogsColumnDef } from '@/features/logs/components/LogsDataGrid'; import { formatTime } from '@/lib/utils/utils'; import { LOGS_PAGE_SIZE } from '@/features/logs/helpers'; import { useConfirm } from '@/lib/hooks/useConfirm'; import { Alert, AlertDescription, Button, ConfirmDialog, Input, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger, } from '@/components'; import { useAuditLogs, useClearAuditLogs } from '@/features/logs/hooks/useAuditLogs'; import type { GetAuditLogsRequest } from '@insforge/shared-schemas'; export default function AuditsPage() { const [searchQuery, setSearchQuery] = useState(''); const [currentPage, setCurrentPage] = useState(1); const [filters, setFilters] = useState>({}); const { confirm, confirmDialogProps } = useConfirm(); // Calculate offset based on current page const offset = (currentPage - 1) * LOGS_PAGE_SIZE; // Fetch logs with pagination and filters const { data: logsResponse, isLoading, error, refetch, } = useAuditLogs({ limit: LOGS_PAGE_SIZE, offset, ...filters, }); const clearMutation = useClearAuditLogs(); const handleRefresh = () => { void refetch(); }; const handleClearLogs = () => { void confirm({ title: 'Clear Audit Logs', description: 'Are you sure you want to clear old audit logs? This will keep the last 90 days of logs.', confirmText: 'Clear Logs', cancelText: 'Cancel', }).then((confirmed) => { if (confirmed) { clearMutation.mutate(90); } }); }; // Apply search filter useEffect(() => { setCurrentPage(1); if (searchQuery) { // Search can filter by actor, action, or module setFilters({ actor: searchQuery, }); } else { setFilters({}); } }, [searchQuery]); // Extract data from response const logsData = logsResponse?.data || []; const totalRecords = logsResponse?.pagination?.total || 0; // Define columns for audit logs const columns: LogsColumnDef[] = useMemo( () => [ { key: 'actor', name: 'Actor', width: '200px', }, { key: 'action', name: 'Action', width: '200px', }, { key: 'module', name: 'Module', width: '150px', }, { key: 'details', name: 'Details', renderCell: ({ row }) => (

{row.details ? JSON.stringify(row.details) : '-'}

), }, { key: 'createdAt', name: 'Time', width: '250px', renderCell: ({ row }) => (

{formatTime(String(row.createdAt ?? ''))}

), }, ], [] ); return (
{/* Sticky Header Section */}
{/* Page Header */}
{/* Separator */}
{/* Action buttons group */}

Refresh

Clear old logs

{/* Search Bar */}
setSearchQuery(e.target.value)} className="pl-9 h-11 bg-white dark:bg-neutral-900 border-gray-200 dark:border-neutral-700 rounded-full text-sm dark:text-white" />
{/* Content */}
{error && ( {String(error)} )}
{!logsData.length && !isLoading ? (

No Audit Logs Available

{searchQuery ? 'No logs found matching your search criteria' : 'Audit logs will appear here once operations are performed'}

) : (
{searchQuery ? 'No audit logs match your search criteria' : 'No audit logs found'}
} />
)}
{/* Confirm Dialog */}
); }