'use client'; import { useState, useEffect } from 'react'; import { db, Item } from '@/lib/db'; import { inventoryApi } from '@/lib/api'; import PageShell from '@/components/PageShell'; import { History, X, Search, Filter, Activity, ArrowDownCircle, ArrowUpCircle, User } from 'lucide-react'; import { cn } from '@/lib/utils'; import { fetchAndCacheItems } from '@/lib/sync'; export default function LogsPage() { const [auditLogs, setAuditLogs] = useState([]); const [inventory, setInventory] = useState([]); const [loading, setLoading] = useState(true); const [searchQuery, setSearchQuery] = useState(''); const [filterAction, setFilterAction] = useState('ALL'); const [selectedLog, setSelectedLog] = useState(null); useEffect(() => { loadData(); }, []); const loadData = async () => { setLoading(true); try { // Load inventory for name lookups const cached = await db.items.toArray(); setInventory(cached); // Fetch fresh logs const logs = await inventoryApi.getAuditLogs(100); setAuditLogs(logs); } catch (err) { console.error(err); } finally { setLoading(false); } }; const filteredLogs = auditLogs.filter(log => { const itemName = inventory.find(i => i.id === log.target_item_id)?.name || ''; const matchesSearch = itemName.toLowerCase().includes(searchQuery.toLowerCase()) || log.action.toLowerCase().includes(searchQuery.toLowerCase()) || (log.username || '').toLowerCase().includes(searchQuery.toLowerCase()); const matchesAction = filterAction === 'ALL' || log.action.includes(filterAction); return matchesSearch && matchesAction; }); // Calculate stats const totalCount = auditLogs.length; const inCount = auditLogs.filter(l => l.action.includes('IN')).length; const outCount = auditLogs.filter(l => l.action.includes('OUT') || l.action.includes('TRASH')).length; const mostActiveUser = auditLogs.length > 0 ? Object.entries(auditLogs.reduce((acc: any, curr) => { acc[curr.username] = (acc[curr.username] || 0) + 1; return acc; }, {})).sort((a: any, b: any) => b[1] - a[1])[0]?.[0] : 'N/A'; return (

Audit Dashboard

Real-time Intervention Tracking

{/* Stats Grid */}

Total Events

{totalCount}

Check in

{inCount}

Check out

{outCount}

Top Operator

{mostActiveUser}

setSearchQuery(e.target.value)} className="w-full bg-slate-950/50 border border-slate-900 focus:border-primary/50 rounded-3xl py-5 pl-14 pr-6 text-sm text-white placeholder:text-slate-700 outline-none transition-all shadow-2xl" />
{[ { label: 'All', value: 'ALL' }, { label: 'Check in', value: 'CHECK_IN' }, { label: 'Check out', value: 'CHECK_OUT' }, { label: 'Trash', value: 'TRASH' }, { label: 'Create', value: 'CREATE' } ].map(action => ( ))}
{loading ? (

Securing Audit Stream...

) : filteredLogs.length === 0 ? (

No interventions found

Try adjusting your filters or search query

) : (
{filteredLogs.map((log) => ( ))}
)}
{/* Selected Log Modal */} {selectedLog && (
{selectedLog.action}

{inventory.find(i => i.id === selectedLog.target_item_id)?.name || `Item #${selectedLog.target_item_id}`}

Operator

{selectedLog.username || 'System Profile'}

Delta

0 ? "text-green-500" : "text-rose-500" )}> {selectedLog.quantity_change > 0 ? '+' : ''}{selectedLog.quantity_change} Units

Timestamp

{new Date(selectedLog.timestamp).toLocaleString(undefined, { dateStyle: 'full', timeStyle: 'medium' })}

{selectedLog.details && (

Intervention Details

"{selectedLog.details}"
)}
)}
); }