import { useState, useEffect } from 'react' import { motion } from 'framer-motion' import { BarChart3, TrendingUp, TrendingDown, Activity, Users, HardDrive, Zap, Clock } from 'lucide-react' import { useAnimatedNumber, useLiveData, useInterval } from '../hooks/useUtils.js' function Sparkline({ data, max, color = '#FF8A00', height = 40 }) { const points = data.map((v, i) => { const x = (i / (data.length - 1)) * 100 const y = 100 - (v / max) * 90 - 5 return `${x},${y}` }).join(' ') return ( ) } function MetricCard({ icon: Icon, label, value, unit, trend, sparkData, sparkColor, delay }) { const animated = useAnimatedNumber(value, 600) return (
{label}
{trend !== undefined && ( 0 ? 'text-success' : 'text-critical'}`}> {trend > 0 ? : } {Math.abs(trend).toFixed(1)}% )}
{typeof animated === 'number' && animated % 1 !== 0 ? animated.toFixed(1) : Math.round(animated)} {unit && {unit}}
{sparkData && }
) } export default function Analytics() { const [storageHistory, setStorageHistory] = useState(Array.from({ length: 30 }, (_, i) => 0.8 + i * 0.015 + Math.random() * 0.05)) const [transferHistory, setTransferHistory] = useState(Array.from({ length: 30 }, () => 150 + Math.random() * 120)) const [userHistory, setUserHistory] = useState(Array.from({ length: 30 }, () => 5 + Math.random() * 8)) const [apiHistory, setApiHistory] = useState(Array.from({ length: 30 }, () => 35 + Math.random() * 15)) const liveData = useLiveData({ storage: 1.2, transfers: 241, users: 11, apiCalls: 42, }, 3000, 0.02) useInterval(() => { setStorageHistory(prev => [...prev.slice(1), liveData.storage]) setTransferHistory(prev => [...prev.slice(1), liveData.transfers]) setUserHistory(prev => [...prev.slice(1), liveData.users]) setApiHistory(prev => [...prev.slice(1), liveData.apiCalls]) }, 3000) const storageTrend = ((storageHistory[storageHistory.length - 1] - storageHistory[0]) / storageHistory[0]) * 100 const transferTrend = ((transferHistory[transferHistory.length - 1] - transferHistory[0]) / transferHistory[0]) * 100 const userTrend = ((userHistory[userHistory.length - 1] - userHistory[0]) / userHistory[0]) * 100 const apiTrend = ((apiHistory[apiHistory.length - 1] - apiHistory[0]) / apiHistory[0]) * 100 return (
{/* Top metrics */}
{/* Throughput timeline */}
Throughput Timeline
Upload Download
{Array.from({ length: 48 }).map((_, i) => { const upload = 30 + Math.sin(i * 0.3) * 20 + Math.random() * 15 const download = 25 + Math.cos(i * 0.25) * 18 + Math.random() * 12 return (
) })}
00:0006:0012:0018:0024:00
{/* Recent activity table */}
Recent Activity
{[ { time: '2m ago', event: 'File indexed', detail: 'dataset.zip → 4.2 GB', status: 'success' }, { time: '5m ago', event: 'Share link created', detail: 'report.pdf', status: 'accent' }, { time: '12m ago', event: 'SQL query executed', detail: 'SELECT * FROM chunks', status: 'primary' }, { time: '18m ago', event: 'Access revoked', detail: 'file_id: 9df7315ee91f', status: 'critical' }, { time: '31m ago', event: 'Chunk retrieved', detail: 'idx=12, 45 lines', status: 'secondary' }, { time: '45m ago', event: 'Search performed', detail: '"merkle root"', status: 'primary' }, ].map((row, i) => ( {row.time} {row.event} {row.detail} ))}
) }