import React, { useEffect, useState } from 'react';
import axios from 'axios';
import { useTranslation } from 'react-i18next';
import {
Activity, Users, DollarSign, Server, Cpu,
ArrowUpRight, ArrowDownRight, RefreshCw, Layers
} from 'lucide-react';
import Layout from '../components/Layout';
import { useAuth } from '../AuthContext';
// Simple simulated chart component
const MicroChart = ({ color, data }: { color: string, data: number[] }) => (
{data.map((val, i) => (
))}
);
export default function AnalyticsPage() {
const { t } = useTranslation();
const { user } = useAuth();
const [stats, setStats] = useState(null);
const [loading, setLoading] = useState(true);
const [refreshKey, setRefreshKey] = useState(0);
// Mock Chart Data
const [chartData] = useState(() => Array.from({ length: 12 }, () => Math.floor(Math.random() * 60) + 20));
useEffect(() => {
const fetchStats = async () => {
try {
const res = await axios.get('/api/admin/analytics');
setStats(res.data);
} catch (err) {
console.error("Failed to load analytics", err);
} finally {
setLoading(false);
}
};
fetchStats();
// Auto-refresh every 30s
const interval = setInterval(fetchStats, 30000);
return () => clearInterval(interval);
}, [refreshKey]);
const formatCurrency = (val: number) => {
return new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }).format(val);
};
const formatTime = (seconds: number) => {
const h = Math.floor(seconds / 3600);
const m = Math.floor((seconds % 3600) / 60);
return `${h}h ${m}m`;
};
if (loading && !stats) return (
);
return (
{/* Background Ambient Glow */}
{/* Header Section */}
v{stats.system.version}
System Operational
Engine Analytics
Mission Control & System Health
{/* KPI Grid */}
{/* KPI 1: Users */}
Total Users
{stats.users.total}
{/* KPI 2: Projects */}
{stats.projects.completionRate}% Done
Total Projects
{stats.projects.total}
{/* KPI 3: Liability */}
Credit Liability
{stats.financials.creditsLiability.toLocaleString()}
Value: {formatCurrency(stats.financials.estimatedValue)}
{/* KPI 4: System Health */}
{stats.system.cpuLoad}% Load
Uptime
{formatTime(stats.system.uptime)}
Mem: {stats.system.memory}MB
{/* Detailed Sections */}
{/* Live Feed */}
{stats.activityLog.map((log: any) => (
{log.user.substring(0, 2).toUpperCase()}
{log.action.replace('_', ' ')}
{log.user}
{new Date(log.timestamp).toLocaleTimeString()}
{log.details}
))}
{stats.activityLog.length === 0 && (
No recent activity found.
)}
{/* System Resources */}
Resource Monitor
CPU Usage
{stats.system.cpuLoad}%
Memory Allocation
{stats.system.memory} MB
Storage (Projects)
{stats.projects.total} Items
System Status
All services operate within nominal parameters. No critical warnings detected in the last 24 hours.
);
}