"use client"; import { useState, useEffect, useCallback } from "react"; import { motion, AnimatePresence } from "framer-motion"; import { Shield, AlertTriangle, CheckCircle, RefreshCw, TrendingUp, Clock, Zap, Eye, XCircle, Activity, Sparkles, ChevronDown, ChevronUp, Loader2, } from "lucide-react"; import { aiApi } from "@/lib/api"; import { useAuthStore } from "@/lib/stores/authStore"; interface FraudAlert { id: string; fraud_log_id?: string; risk_score: number; details: string; status?: string; transaction_id?: string; merchant?: string; amount?: number; timestamp?: string; } interface FraudData { total_alerts: number; pending_reviews: number; alerts: FraudAlert[]; } interface FraudExplanation { ai_explanation: string; raw_reasons: string[]; amount_vs_avg: number; user_avg_amount: number; } function Skeleton({ className }: { className?: string }) { return
; } function RiskBadge({ score }: { score: number }) { const level = score >= 70 ? { label: "High Risk", color: "text-red-400", bg: "bg-red-500/10 border-red-500/20" } : score >= 40 ? { label: "Suspicious", color: "text-amber-400", bg: "bg-amber-500/10 border-amber-500/20" } : { label: "Low Risk", color: "text-emerald-400", bg: "bg-emerald-500/10 border-emerald-500/20" }; return ( {level.label} ); } function RiskBar({ score }: { score: number }) { const color = score >= 70 ? "#ef4444" : score >= 40 ? "#f59e0b" : "#10b981"; return ({alert.merchant || `Transaction ${(alert.transaction_id || alert.id).slice(0, 8)}...`}
{typeof alert.details === "string" ? alert.details : Array.isArray(alert.details) ? (alert.details as string[]).join(" · ") : "Suspicious activity detected"}
${Math.abs(alert.amount).toFixed(2)}
AI Fraud Explanation
{loadingExpl ? ({explanation.ai_explanation}
{explanation.raw_reasons.length > 0 && (Detection signals:
{explanation.raw_reasons.map((r, i) => ({r}
This transaction
${alert.amount?.toFixed(2)}
Your average
${explanation.user_avg_amount.toFixed(2)}
AI-powered fraud detection · Click any alert for a full AI explanation
Could not load fraud data — {error}
AI Fraud Shield Active
Click any flagged transaction to get a full AI explanation of exactly why it was flagged.
{stat.label}
{stat.value}
{rule.label}
{rule.desc}
{loading ? "Loading..." : `${filtered.length} alert${filtered.length !== 1 ? "s" : ""} · Click any row for AI explanation`}
{alerts.length === 0 ? "No fraud alerts detected" : "No alerts match this filter"}
{alerts.length === 0 ? "All transactions look clean. AI Shield is monitoring in real-time." : "Try a different filter."}
{safeCount} transaction{safeCount !== 1 ? "s" : ""} verified safe{" "}— risk score below threshold.