import { useState, useEffect } from 'react'; import { AlertTriangle, ShieldCheck, Zap } from 'lucide-react'; import { BarChart, Bar, XAxis, YAxis, Tooltip, ResponsiveContainer, Cell } from 'recharts'; import api from '../api/axios'; import { useSettings } from '../context/SettingsContext'; const AnomalyFeed = () => { const { currencySymbol } = useSettings(); const [anomalies, setAnomalies] = useState([]); const [loading, setLoading] = useState(true); const [showAll, setShowAll] = useState(false); useEffect(() => { const fetchAnomalies = async () => { try { const response = await api.get('analytics/anomalies/'); setAnomalies(response.data); } catch (err) { console.error("Anomaly Error:", err); } finally { setLoading(false); } }; fetchAnomalies(); }, []); // Prepare chart data - Recharts Scatter needs numeric X axis often for best results with type="number" // converting date string to timestamp const chartData = anomalies.map(a => ({ ...a, x: new Date(a.date).getTime(), y: a.amount })); const displayedAnomalies = showAll ? anomalies : anomalies.slice(0, 6); if (loading) return (
); return (

Security Radar

{anomalies.length}
{/* Anomaly Visualization Chart */} {anomalies.length > 0 && (
[`${currencySymbol}${value}`, 'Amount']} /> {anomalies.map((entry, index) => ( ))}
)}
{anomalies.length === 0 ? (

No anomalies detected.

) : (
{displayedAnomalies.map((item, idx) => (
{item.title}
{currencySymbol}{item.amount.toLocaleString()}
{item.date}
{item.score ? `Risk: ${(item.score * 100).toFixed(0)}%` : 'Detected'}
))} {/* Show More Button - Spans all columns */} {anomalies.length > 6 && ( )}
)}
AI Anomaly Detector
); }; export default AnomalyFeed;