- Remove min-h-screen or make responsive (md:min-h-screen for desktop only) - Login modal: p-8 → p-4 md:p-6, space-y-8 → space-y-3 md:space-y-4 - NewItemDialog: responsive spacing p-4 md:p-6, gaps 3 md:gap-4 - StockAdjustmentPanel: p-6 → p-4 md:p-6, gaps/spacing reduced for mobile - All container padding and gaps now follow mobile-first pattern - Fixes viewport overflow on 375px portrait mode - Tests: 291/291 passing
188 lines
7.4 KiB
TypeScript
188 lines
7.4 KiB
TypeScript
'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, Search, Activity, ArrowDownCircle, ArrowUpCircle, RefreshCw } from 'lucide-react';
|
|
import { cn } from '@/lib/utils';
|
|
import { fetchAndCacheItems } from '@/lib/sync';
|
|
import StatCard from '@/components/StatCard';
|
|
import LogsTable from '@/components/LogsTable';
|
|
|
|
export default function LogsPage() {
|
|
const [auditLogs, setAuditLogs] = useState<any[]>([]);
|
|
const [inventory, setInventory] = useState<Item[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [searchQuery, setSearchQuery] = useState('');
|
|
const [filterAction, setFilterAction] = useState('ALL');
|
|
|
|
useEffect(() => {
|
|
loadData();
|
|
}, []);
|
|
|
|
const loadData = async () => {
|
|
setLoading(true);
|
|
try {
|
|
// 1. First, try to get fresh items to resolve names
|
|
let freshItems: Item[] = [];
|
|
try {
|
|
freshItems = await fetchAndCacheItems();
|
|
} catch (itemErr) {
|
|
console.warn("Item sync failed, using local cache for names", itemErr);
|
|
freshItems = await db.items.toArray();
|
|
}
|
|
setInventory(freshItems);
|
|
|
|
// 2. Then, fetch fresh logs
|
|
const logs = await inventoryApi.getAuditLogs(100);
|
|
|
|
// 3. Pre-resolve names to avoid UI flickering/mismatches
|
|
const enrichedLogs = (logs || []).map((log: any) => {
|
|
// [AUDIT HARDENING] Prioritize the historical snapshot from the backend
|
|
if (log.target_item_name) {
|
|
return { ...log, resolved_name: log.target_item_name };
|
|
}
|
|
|
|
// Fallback for legacy logs or system operations
|
|
const hasTarget = log.target_item_id && String(log.target_item_id) !== 'null';
|
|
const item = hasTarget ? freshItems.find(i => String(i.id) === String(log.target_item_id)) : null;
|
|
|
|
return {
|
|
...log,
|
|
resolved_name: item ? item.name : (hasTarget ? `Item #${log.target_item_id}` : "System Operation")
|
|
};
|
|
});
|
|
|
|
setAuditLogs(enrichedLogs);
|
|
} catch (err: any) {
|
|
console.error("Critical log load failure:", err);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
const filteredLogs = auditLogs.filter(log => {
|
|
const matchesSearch = (log.resolved_name || '').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') || l.action.includes('ADD')).length;
|
|
const outCount = auditLogs.filter(l => l.action.includes('OUT') || l.action.includes('TRASH') || l.action.includes('DELETE')).length;
|
|
const criticalLogsCount = auditLogs.filter(l => l.action.includes('DELETE') || l.action.includes('TRASH')).length;
|
|
|
|
const mostActiveUser = auditLogs.length > 0 ?
|
|
Object.entries(auditLogs.reduce((acc: any, curr) => {
|
|
const user = curr.username || 'System';
|
|
acc[user] = (acc[user] || 0) + 1;
|
|
return acc;
|
|
}, {})).sort((a: any, b: any) => b[1] - a[1])[0]?.[0] : 'N/A';
|
|
|
|
return (
|
|
<PageShell>
|
|
<main className="p-3 md:p-8 max-w-7xl mx-auto space-y-3 md:space-y-6 mb-20">
|
|
<header className="flex flex-col sm:flex-row sm:items-end justify-between gap-3 md:gap-4">
|
|
<div className="flex items-center gap-4">
|
|
<div className="p-3 md:p-4 bg-primary/10 rounded-2xl text-primary border border-primary/20 shadow-xl shadow-primary/5">
|
|
<History size={28} className="md:w-8 md:h-8" />
|
|
</div>
|
|
<div>
|
|
<h1 className="text-2xl md:text-3xl font-normal tracking-tight text-white leading-tight">Operations Audit</h1>
|
|
<p className="text-xs md:text-sm text-secondary font-normal tracking-widest mt-0.5">Real-time Intervention Tracking</p>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex gap-2 ml-auto sm:ml-0">
|
|
<button
|
|
onClick={loadData}
|
|
disabled={loading}
|
|
className="flex items-center gap-2 px-5 py-2.5 bg-surface border border-slate-800 text-secondary hover:text-white rounded-2xl text-xs font-normal transition-all active:scale-95 disabled:opacity-50 shadow-xl"
|
|
>
|
|
<RefreshCw size={14} className={cn(loading && "animate-spin")} />
|
|
Sync Logs
|
|
</button>
|
|
</div>
|
|
</header>
|
|
|
|
{/* Stats Grid */}
|
|
<section className="grid grid-cols-2 lg:grid-cols-4 gap-2.5 md:gap-4">
|
|
<StatCard
|
|
label="Total Events"
|
|
value={totalCount}
|
|
icon={Activity}
|
|
/>
|
|
<StatCard
|
|
label="Inbound"
|
|
value={inCount}
|
|
icon={ArrowDownCircle}
|
|
/>
|
|
<StatCard
|
|
label="Outbound"
|
|
value={outCount}
|
|
icon={ArrowUpCircle}
|
|
/>
|
|
<StatCard
|
|
label="Integrity"
|
|
value={totalCount - criticalLogsCount}
|
|
icon={History}
|
|
/>
|
|
</section>
|
|
|
|
<section className="space-y-3 md:space-y-4">
|
|
<div className="relative group">
|
|
<input
|
|
type="text"
|
|
placeholder="Filter by user, action or asset..."
|
|
value={searchQuery}
|
|
onChange={(e) => setSearchQuery(e.target.value)}
|
|
className="w-full bg-surface/50 border border-slate-800 rounded-2xl py-3.5 pr-4 pl-12 text-sm focus:border-primary outline-none transition-all placeholder:text-secondary shadow-2xl"
|
|
/>
|
|
<div className="absolute left-4 top-1/2 -translate-y-1/2 text-secondary transition-colors group-focus-within:text-primary">
|
|
<Search size={18} />
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex items-center gap-2 overflow-x-auto no-scrollbar pb-1 px-1 -mx-1">
|
|
<button
|
|
onClick={() => setFilterAction('ALL')}
|
|
className={cn(
|
|
"px-5 py-2 rounded-full text-xs font-normal transition-all whitespace-nowrap border tracking-widest",
|
|
filterAction === 'ALL'
|
|
? "bg-white text-slate-950 border-white shadow-xl shadow-white/10"
|
|
: "bg-surface/70 text-secondary border-slate-800 hover:border-slate-700"
|
|
)}
|
|
>
|
|
All Streams
|
|
</button>
|
|
{['ADD', 'REMOVE', 'ADJUST', 'DELETE', 'LOGIN'].map((f) => (
|
|
<button
|
|
key={f}
|
|
onClick={() => setFilterAction(f)}
|
|
className={cn(
|
|
"px-5 py-2 rounded-full text-xs font-normal transition-all whitespace-nowrap border tracking-widest",
|
|
filterAction === f
|
|
? "bg-primary text-white border-primary shadow-xl shadow-primary/10"
|
|
: "bg-surface/70 text-secondary border-slate-800 hover:border-slate-700"
|
|
)}
|
|
>
|
|
{f}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</section>
|
|
|
|
<section className="space-y-3">
|
|
<LogsTable logs={filteredLogs} loading={loading} />
|
|
</section>
|
|
</main>
|
|
</PageShell>
|
|
);
|
|
}
|