import { useState, useEffect } from 'react'; import { ChevronDown, Filter, Loader2, User } from 'lucide-react'; import { auditLogService } from '@/services/audit-log-service'; import type { AuditLog } from '@/types/audit-log'; import { useAppSelector } from '@/hooks/redux-hooks'; import { cn } from '@/lib/utils'; import { Card, CardHeader, CardContent } from '@/components/ui/card'; import { StatusBadge } from '@/components/shared'; import { Button } from '@/components/ui/button'; // Helper functions (kept from original) const getActionVariant = (action: string): 'success' | 'failure' | 'info' | 'process' => { const lowerAction = action.toLowerCase(); if (lowerAction.includes('create') || lowerAction.includes('register')) return 'success'; if (lowerAction.includes('update') || lowerAction.includes('version_update') || lowerAction.includes('login')) return 'info'; if (lowerAction.includes('delete') || lowerAction.includes('deregister')) return 'failure'; if (lowerAction.includes('read') || lowerAction.includes('get') || lowerAction.includes('status_change')) return 'process'; return 'info'; }; const formatRelativeTime = (dateString: string): string => { const date = new Date(dateString); const now = new Date(); const diffInSeconds = Math.floor((now.getTime() - date.getTime()) / 1000); if (diffInSeconds < 60) return 'Just now'; const diffInMinutes = Math.floor(diffInSeconds / 60); if (diffInMinutes < 60) return `${diffInMinutes} min ago`; const diffInHours = Math.floor(diffInMinutes / 60); if (diffInHours < 24) return `${diffInHours} hours ago`; const diffInDays = Math.floor(diffInHours / 24); if (diffInDays === 1) return 'Yesterday'; return date.toLocaleDateString(); }; export interface RecentActivityProps { variant?: 'list' | 'table'; } export const RecentActivity = ({ variant }: RecentActivityProps) => { const [auditLogs, setAuditLogs] = useState([]); const [isLoading, setIsLoading] = useState(true); const { tenantId } = useAppSelector((state) => state.auth); // Default to table variant for a more professional look const activeVariant = variant || 'table'; useEffect(() => { const fetchRecentActivity = async (): Promise => { try { setIsLoading(true); // Pass tenantId if we're on a tenant route to get tenant-specific logs const filterTenantId = window.location.pathname.startsWith('/tenant') ? tenantId : null; const response = await auditLogService.getAll(1, 5, { tenant_id: filterTenantId }, ['created_at', 'desc']); if (response.success) { setAuditLogs(response.data); } } catch (err: any) { // Fallback or log if needed } finally { setIsLoading(false); } }; fetchRecentActivity(); }, [tenantId, activeVariant]); if (activeVariant === 'list') { return (

Recent Activity

Last 24 hours
{isLoading ? (
) : auditLogs.length === 0 ? (
No recent activity
) : (
{auditLogs.map((log, index) => (
{formatRelativeTime(log.created_at)}
{log.user?.email?.split('@')[0] || 'System User'} {log.action.toLowerCase().includes('create') ? 'created' : 'performed'} {log.resource_type}
))}
)}
); } // TABLE VARIANT (Original design for superadmin dashboard) return (

Recent Activity

Action All
{isLoading ? (
) : (
{auditLogs.map((log) => ( ))}
User Profile Timestamp Resource Type Action
{log.user ? `${log.user.first_name || ''} ${log.user.last_name || ''}`.trim() || log.user.email.split('@')[0] : 'System User'} {log.user && {log.user.email}}
{formatRelativeTime(log.created_at)} {log.resource_type} {log.action}
)}
); };