import React, { useState, useEffect, useCallback } from 'react'; import { useAuth, api } from '@nexus/shared'; import { ShieldCheck, Activity, Clock, Filter, RefreshCcw, Fingerprint, ChevronLeft, ChevronRight, Search } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import styles from './ForensicsView.module.css'; interface AuditLog { id: string; workspace_id: string | null; entity_type: string; entity_id: string; action: string; user_id: string | null; actor_name: string; old_value: any; new_value: any; metadata_json: any; created_at: string; } interface AIInteraction { id: string; agent_name: string; model_info: string; input_data: any; output_data: any; status: string; duration_ms: any; usage_metadata: any; created_at: string; } type ForensicsTab = 'ai' | 'security'; interface ForensicsViewProps { workspaceId?: string; } export const ForensicsView: React.FC = ({ workspaceId }) => { useAuth(); const [activeTab, setActiveTab] = useState('security'); const [logs, setLogs] = useState([]); const [aiTraces, setAiTraces] = useState([]); const [loading, setLoading] = useState(true); const [filterType, setFilterType] = useState(''); const [page, setPage] = useState(1); const [hasMore, setHasMore] = useState(true); const [searchTerm, setSearchTerm] = useState(''); const [localSearch, setLocalSearch] = useState(''); const LIMIT = 20; // Smaller limit for better pagination feel const fetchAuditLogs = useCallback(async (targetPage = 1) => { setLoading(true); try { const currentOffset = (targetPage - 1) * LIMIT; const params: any = { limit: LIMIT, offset: currentOffset }; if (filterType) params.entity_type = filterType; if (workspaceId) params.workspace_id = workspaceId; if (searchTerm) params.entity_id = searchTerm; // Support searching by entity ID const res = await api.get('system/audit/', { params }); const newLogs = res.data || []; setLogs(newLogs); setPage(targetPage); setHasMore(newLogs.length === LIMIT); } catch (err) { console.error('Forensics fetch failed', err); } finally { setLoading(false); } }, [filterType, workspaceId, searchTerm]); const fetchAIInteractions = useCallback(async (targetPage = 1) => { setLoading(true); try { const currentOffset = (targetPage - 1) * LIMIT; const params: any = { limit: LIMIT, offset: currentOffset }; if (workspaceId) params.workspace_id = workspaceId; if (searchTerm) params.ticket_id = searchTerm; // Neural traces search by ticket ID const res = await api.get('system/interactions/', { params }); const newTraces = res.data || []; setAiTraces(newTraces); setPage(targetPage); setHasMore(newTraces.length === LIMIT); } catch (err) { console.error('AI Forensics fetch failed', err); } finally { setLoading(false); } }, [workspaceId, searchTerm]); useEffect(() => { if (activeTab === 'security') { fetchAuditLogs(page); } else { fetchAIInteractions(page); } }, [activeTab, filterType, searchTerm, workspaceId, page, fetchAuditLogs, fetchAIInteractions]); const handleSearchKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { setPage(1); setSearchTerm(localSearch); } }; const renderSecurityAudit = () => (
setLocalSearch(e.target.value)} onKeyDown={handleSearchKeyDown} style={{ background: 'transparent', border: 'none', color: 'inherit', fontSize: '11px', outline: 'none', width: '150px' }} />
RECURSIVE INTEGRITY CHECK: PASSED
TIMESTAMP
ACTOR
ACTION
ENTITY
INTELLIGENCE
{loading && ( FILTERING AUDIT TRAILS... )}
{logs.map((log, idx) => (
{new Date(log.created_at).toLocaleTimeString()}
{new Date(log.created_at).toLocaleDateString()}
{log.actor_name?.[0] || 'S'}
{log.actor_name} {log.user_id?.slice(0, 8) || 'SYSTEM'}
{(log.action || 'system_action').replace(/_/g, ' ').toUpperCase()}
{(log.entity_type || 'system').toUpperCase()} #{log.entity_id?.slice(0, 8) || '0000'}
{log.new_value ? (
{JSON.stringify(log.new_value, null, 2)}
) : ( Stateless interaction record )}
))}
{logs.length === 0 && !loading && (

No security events found matching current filter.

)}
PAGE {page}
); const renderNeuralTrace = () => (
setLocalSearch(e.target.value)} onKeyDown={handleSearchKeyDown} style={{ background: 'transparent', border: 'none', color: 'inherit', fontSize: '11px', outline: 'none', width: '180px' }} />
NEURAL STREAM: ACTIVE
TIMESTAMP
AGENT / MODEL
METRICS
STATUS
INTELLIGENCE
{loading && ( ISOLATING NEURAL TRACES... )}
{aiTraces.map((trace, idx) => (
{new Date(trace.created_at).toLocaleTimeString()}
{new Date(trace.created_at).toLocaleDateString()}
{trace.agent_name.replace(/_/g, ' ').toUpperCase()} {trace.model_info || 'NEURAL_CORE_V1'}
LATENCY: {trace.duration_ms?.total || trace.duration_ms || 0}ms
TOKENS: {trace.usage_metadata?.total_tokens || 0}
{(trace.status || 'SUCCESS').toUpperCase()}
PROMPT_GUIDE:
{JSON.stringify(trace.input_data, null, 2)}
RESULTANT_INFERENCE:
{JSON.stringify(trace.output_data, null, 2)}
))} {aiTraces.length === 0 && !loading && (

No neural traces found in current temporal window.

)}
PAGE {page}
); return (
{activeTab === 'security' ? renderSecurityAudit() : renderNeuralTrace()}
); };