🚀 Phase 4: AI Usage Tracking, Security Hardening, and Risk Monitor Dashboard

This commit is contained in:
Hamza-Ayed
2026-04-22 17:32:22 +03:00
parent 7e0e271be2
commit 6c1d67c695
11 changed files with 361 additions and 13 deletions

View File

@@ -10,6 +10,7 @@ import { CompaniesPage } from './pages/companies/CompaniesPage';
import { StaffPage } from './pages/staff/StaffPage';
import { SettingsPage } from './pages/settings/SettingsPage';
import { MultiEntityDashboard } from './pages/dashboard/MultiEntityDashboard';
import { RiskMonitorPage } from './pages/dashboard/RiskMonitorPage';
import { TrojanHorseConverter } from './pages/Public/TrojanHorseConverter';
@@ -33,6 +34,7 @@ export default function App() {
<Route index element={<Navigate to="/dashboard" replace />} />
<Route path="dashboard" element={<DashboardPage />} />
<Route path="elite-dashboard" element={<MultiEntityDashboard />} />
<Route path="risk-monitor" element={<RiskMonitorPage />} />
<Route path="invoices" element={<InvoicesPage />} />
<Route path="companies" element={<CompaniesPage />} />
<Route path="staff" element={<StaffPage />} />

View File

@@ -12,13 +12,15 @@ import {
Users,
Settings,
LogOut,
Crown
Crown,
AlertTriangle
} from 'lucide-react';
import { useAuthStore } from '../../store/authStore';
const menuItems = [
{ icon: LayoutDashboard, label: 'الرئيسية', path: '/dashboard' },
{ icon: Crown, label: 'المركز الضريبي الموحد', path: '/elite-dashboard' },
{ icon: AlertTriangle, label: 'مراقبة المخاطر', path: '/risk-monitor' },
{ icon: FileText, label: 'الفواتير', path: '/invoices' },
{ icon: Building2, label: 'الشركات', path: '/companies' },
{ icon: Users, label: 'الموظفون', path: '/staff' },

View File

@@ -11,6 +11,10 @@ interface CompanyStats {
totalTax: number;
failedCount: number;
riskScore: number;
aiStats: {
totalTokens: number;
totalCost: number;
};
}
const getRiskStatus = (score: number) => {
@@ -62,6 +66,31 @@ const RiskGauge = ({ score }: { score: number }) => {
);
};
const Cpu = ({ className }: { className?: string }) => (
<svg
xmlns="http://www.w3.org/2000/svg"
width="24" height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
className={className}
>
<rect x="4" y="4" width="16" height="16" rx="2" ry="2"/>
<rect x="9" y="9" width="6" height="6"/>
<line x1="9" y1="1" x2="9" y2="4"/>
<line x1="15" y1="1" x2="15" y2="4"/>
<line x1="9" y1="20" x2="9" y2="23"/>
<line x1="15" y1="20" x2="15" y2="23"/>
<line x1="20" y1="9" x2="23" y2="9"/>
<line x1="20" y1="15" x2="23" y2="15"/>
<line x1="1" y1="9" x2="4" y2="9"/>
<line x1="1" y1="15" x2="4" y2="15"/>
</svg>
);
export const MultiEntityDashboard = () => {
const [companies, setCompanies] = useState<CompanyStats[]>([]);
const [isLoading, setIsLoading] = useState(true);
@@ -158,6 +187,14 @@ export const MultiEntityDashboard = () => {
key={company.id}
className="card-premium p-6 relative overflow-hidden group"
>
{/* AI Usage Badge */}
<div className="absolute top-0 right-0 p-2">
<div className="flex items-center gap-1 bg-slate-100 dark:bg-slate-800 px-2 py-1 rounded-md text-[10px] font-bold text-slate-500 dark:text-slate-400 border border-slate-200 dark:border-slate-700">
<Cpu className="w-3 h-3 text-purple-400" />
<span>{company.aiStats?.totalTokens > 1000 ? `${(company.aiStats.totalTokens / 1000).toFixed(1)}k` : company.aiStats?.totalTokens || 0} tokens</span>
</div>
</div>
{/* Ambient glow */}
<div className={`absolute -inset-20 opacity-0 group-hover:opacity-10 blur-3xl transition-opacity duration-500 rounded-full
${status === 'High' ? 'bg-red-500' : status === 'Medium' ? 'bg-orange-500' : 'bg-emerald-500'}
@@ -197,11 +234,18 @@ export const MultiEntityDashboard = () => {
<div className="pt-4 border-t border-slate-100 dark:border-slate-800/50">
<div className="flex justify-between items-center mb-2">
<p className="text-sm text-slate-600 dark:text-slate-300">{company.totalInvoices} فاتورة</p>
{company.failedCount > 0 && (
<span className="text-xs text-red-400 flex items-center gap-1">
<AlertTriangle className="w-3 h-3" /> {company.failedCount} مرفوضة
</span>
)}
<div className="flex items-center gap-2">
{company.aiStats?.totalCost > 0 && (
<span className="text-[10px] font-black text-purple-400 bg-purple-500/10 px-2 py-0.5 rounded border border-purple-500/20">
${company.aiStats.totalCost.toFixed(3)}
</span>
)}
{company.failedCount > 0 && (
<span className="text-xs text-red-400 flex items-center gap-1">
<AlertTriangle className="w-3 h-3" /> {company.failedCount} مرفوضة
</span>
)}
</div>
</div>
{/* Progress Bar */}

View File

@@ -0,0 +1,209 @@
import { useState, useEffect } from 'react';
import {
AlertTriangle,
ShieldAlert,
FileWarning,
ChevronRight,
Search,
Filter,
Loader2,
Building2,
Calendar,
ArrowRight
} from 'lucide-react';
import { motion, AnimatePresence } from 'framer-motion';
import apiClient from '../../api/client';
import { Link } from 'react-router-dom';
interface RiskInvoice {
id: string;
invoice_number: string;
invoice_date: string;
status: string;
validation_errors: string[];
grand_total: number;
company: {
name: string;
};
updated_at: string;
}
export const RiskMonitorPage = () => {
const [invoices, setInvoices] = useState<RiskInvoice[]>([]);
const [isLoading, setIsLoading] = useState(true);
const [searchTerm, setSearchTerm] = useState('');
useEffect(() => {
const fetchRisks = async () => {
try {
const { data } = await apiClient.get('/dashboard/risk-invoices');
setInvoices(data);
} catch (err) {
console.error('Failed to fetch risk invoices', err);
} finally {
setIsLoading(false);
}
};
fetchRisks();
}, []);
const filteredInvoices = invoices.filter(inv =>
inv.invoice_number?.toLowerCase().includes(searchTerm.toLowerCase()) ||
inv.company?.name.toLowerCase().includes(searchTerm.toLowerCase())
);
if (isLoading) {
return (
<div className="flex-1 flex flex-col justify-center items-center py-32">
<Loader2 className="w-12 h-12 text-emerald-400 animate-spin mb-4" />
<p className="text-slate-400">جاري تحليل المخاطر الضريبية...</p>
</div>
);
}
return (
<div className="space-y-10 animate-in fade-in duration-700">
{/* Header Section */}
<header className="relative p-10 rounded-[2.5rem] bg-slate-900 border border-slate-800 overflow-hidden">
<div className="absolute top-0 right-0 w-1/3 h-full bg-gradient-to-l from-red-500/10 to-transparent pointer-events-none" />
<div className="relative z-10">
<div className="flex items-center gap-4 mb-6">
<div className="w-14 h-14 rounded-2xl bg-red-500/20 flex items-center justify-center border border-red-500/30">
<ShieldAlert className="w-8 h-8 text-red-500" />
</div>
<div>
<h1 className="text-4xl font-black text-white">مراقبة المخاطر</h1>
<p className="text-slate-400 mt-1 font-medium italic">Risk & Anomaly Monitoring Center</p>
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-3 gap-6 mt-10">
<div className="bg-slate-800/40 p-6 rounded-3xl border border-slate-700/50">
<p className="text-slate-500 text-sm font-bold uppercase tracking-widest mb-2">إجمالي المخاطر</p>
<p className="text-4xl font-black text-white">{invoices.length}</p>
<div className="h-1 w-full bg-red-500/20 rounded-full mt-4">
<div className="h-full bg-red-500 rounded-full" style={{ width: '100%' }} />
</div>
</div>
<div className="bg-slate-800/40 p-6 rounded-3xl border border-slate-700/50">
<p className="text-slate-500 text-sm font-bold uppercase tracking-widest mb-2">فشل التحقق (Validation)</p>
<p className="text-4xl font-black text-white">
{invoices.filter(i => i.status === 'validation_failed').length}
</p>
<div className="h-1 w-full bg-orange-500/20 rounded-full mt-4">
<div className="h-full bg-orange-500 rounded-full" style={{ width: '60%' }} />
</div>
</div>
<div className="bg-slate-800/40 p-6 rounded-3xl border border-slate-700/50">
<p className="text-slate-500 text-sm font-bold uppercase tracking-widest mb-2">مرفوضة من JoFotara</p>
<p className="text-4xl font-black text-white">
{invoices.filter(i => i.status === 'rejected').length}
</p>
<div className="h-1 w-full bg-slate-700 rounded-full mt-4">
<div className="h-full bg-slate-500 rounded-full" style={{ width: '20%' }} />
</div>
</div>
</div>
</div>
</header>
{/* Main Content Card */}
<div className="bg-slate-900/50 border border-slate-800 rounded-[2.5rem] overflow-hidden backdrop-blur-xl">
<div className="p-8 border-b border-slate-800 flex flex-col md:flex-row justify-between items-center gap-6">
<div className="relative w-full md:w-96">
<Search className="absolute left-4 top-1/2 -translate-y-1/2 w-5 h-5 text-slate-500" />
<input
type="text"
placeholder="البحث عن شركة أو رقم فاتورة..."
className="w-full bg-slate-800/50 border border-slate-700 rounded-2xl py-3 pl-12 pr-6 text-white placeholder-slate-500 focus:outline-none focus:ring-2 focus:ring-red-500/50 transition-all font-medium"
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
/>
</div>
<div className="flex gap-4">
<button className="px-6 py-3 bg-slate-800 text-slate-300 rounded-2xl border border-slate-700 flex items-center gap-2 hover:bg-slate-700 transition-all font-bold">
<Filter className="w-5 h-5" /> تصفية النتائج
</button>
</div>
</div>
<div className="overflow-x-auto">
<table className="w-full">
<thead>
<tr className="text-right border-b border-slate-800">
<th className="px-8 py-6 text-slate-500 font-bold uppercase tracking-tighter text-xs">الشركة والفاتورة</th>
<th className="px-8 py-6 text-slate-500 font-bold uppercase tracking-tighter text-xs">نوع الخطر</th>
<th className="px-8 py-6 text-slate-500 font-bold uppercase tracking-tighter text-xs">التاريخ والقيمة</th>
<th className="px-8 py-6 text-slate-500 font-bold uppercase tracking-tighter text-xs text-left">الإجراء</th>
</tr>
</thead>
<tbody>
<AnimatePresence>
{filteredInvoices.map((inv, index) => (
<motion.tr
key={inv.id}
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
transition={{ delay: index * 0.05 }}
className="border-b border-slate-800/50 hover:bg-slate-800/20 transition-colors group"
>
<td className="px-8 py-6">
<div className="flex items-center gap-4">
<div className="w-12 h-12 rounded-xl bg-slate-800 flex items-center justify-center border border-slate-700">
<Building2 className="w-6 h-6 text-slate-400" />
</div>
<div>
<p className="text-white font-black">{inv.company?.name}</p>
<p className="text-slate-500 text-sm font-bold">#{inv.invoice_number || 'بدون رقم'}</p>
</div>
</div>
</td>
<td className="px-8 py-6">
<div className="space-y-2">
<div className={`inline-flex items-center gap-2 px-3 py-1 rounded-full text-[10px] font-black uppercase tracking-widest ${
inv.status === 'validation_failed' ? 'bg-orange-500/10 text-orange-500 border border-orange-500/20' : 'bg-red-500/10 text-red-500 border border-red-500/20'
}`}>
<AlertTriangle className="w-3 h-3" />
{inv.status === 'validation_failed' ? 'فشل المطابقة' : 'مرفوضة ضريبياً'}
</div>
{inv.validation_errors && inv.validation_errors.length > 0 && (
<p className="text-xs text-slate-400 max-w-xs line-clamp-1 italic">
{inv.validation_errors[0]}
</p>
)}
</div>
</td>
<td className="px-8 py-6">
<div className="flex flex-col">
<span className="text-white font-black">{Number(inv.grand_total).toFixed(3)} JOD</span>
<span className="text-slate-500 text-xs flex items-center gap-1 font-bold">
<Calendar className="w-3 h-3" /> {new Date(inv.invoice_date).toLocaleDateString('ar-JO')}
</span>
</div>
</td>
<td className="px-8 py-6 text-left">
<Link
to={`/invoices`}
className="inline-flex items-center gap-2 text-emerald-500 font-black hover:text-emerald-400 transition-colors group/btn"
>
<span>مراجعة وتعديل</span>
<ArrowRight className="w-4 h-4 transform group-hover/btn:translate-x-1 transition-transform" />
</Link>
</td>
</motion.tr>
))}
</AnimatePresence>
</tbody>
</table>
{filteredInvoices.length === 0 && (
<div className="py-20 text-center">
<FileWarning className="w-16 h-16 text-slate-700 mx-auto mb-4" />
<p className="text-slate-500 font-bold">لا توجد مخاطر مكتشفة حالياً في هذا النطاق.</p>
</div>
)}
</div>
</div>
</div>
);
};