|
| 1 | +"use client"; |
| 2 | +import { useMemo } from "react"; |
| 3 | +import { usePolling } from "@/lib/use-polling"; |
| 4 | +import { api, Transaction } from "@/lib/api"; |
| 5 | +import { |
| 6 | + AreaChart, Area, BarChart, Bar, PieChart, Pie, Cell, |
| 7 | + XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, Legend, |
| 8 | +} from "recharts"; |
| 9 | +import { format, parseISO, startOfDay } from "date-fns"; |
| 10 | + |
| 11 | +const COLORS = ["#6366f1", "#22c55e", "#f59e0b", "#ef4444", "#8b5cf6"]; |
| 12 | + |
| 13 | +export default function AnalyticsPage() { |
| 14 | + const { data: txs, loading } = usePolling(() => api.transactions(), 30000); |
| 15 | + |
| 16 | + const { dailyVolume, statusDist, assetDist } = useMemo(() => { |
| 17 | + if (!txs) return { dailyVolume: [], statusDist: [], assetDist: [] }; |
| 18 | + |
| 19 | + // Daily volume (last 30 days) |
| 20 | + const byDay: Record<string, number> = {}; |
| 21 | + txs.forEach((t) => { |
| 22 | + const day = format(startOfDay(parseISO(t.created_at)), "MMM d"); |
| 23 | + byDay[day] = (byDay[day] ?? 0) + t.send_amount / 1_000_000; |
| 24 | + }); |
| 25 | + const dailyVolume = Object.entries(byDay) |
| 26 | + .slice(-30) |
| 27 | + .map(([date, volume]) => ({ date, volume: Number(volume.toFixed(2)) })); |
| 28 | + |
| 29 | + // Status distribution |
| 30 | + const bySt: Record<string, number> = {}; |
| 31 | + txs.forEach((t) => { bySt[t.status] = (bySt[t.status] ?? 0) + 1; }); |
| 32 | + const statusDist = Object.entries(bySt).map(([name, value]) => ({ name, value })); |
| 33 | + |
| 34 | + // Asset distribution |
| 35 | + const byAsset: Record<string, number> = {}; |
| 36 | + txs.forEach((t) => { byAsset[t.send_asset] = (byAsset[t.send_asset] ?? 0) + t.send_amount / 1_000_000; }); |
| 37 | + const assetDist = Object.entries(byAsset).map(([name, value]) => ({ name, value: Number(value.toFixed(2)) })); |
| 38 | + |
| 39 | + return { dailyVolume, statusDist, assetDist }; |
| 40 | + }, [txs]); |
| 41 | + |
| 42 | + if (loading && !txs) { |
| 43 | + return ( |
| 44 | + <div> |
| 45 | + <h1 className="text-2xl font-bold text-slate-900 mb-6">Analytics</h1> |
| 46 | + <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> |
| 47 | + {Array.from({ length: 3 }).map((_, i) => ( |
| 48 | + <div key={i} className="bg-white border border-slate-200 rounded-xl p-5 h-72 animate-pulse" /> |
| 49 | + ))} |
| 50 | + </div> |
| 51 | + </div> |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + return ( |
| 56 | + <div> |
| 57 | + <h1 className="text-2xl font-bold text-slate-900 mb-6">Analytics</h1> |
| 58 | + <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> |
| 59 | + |
| 60 | + {/* Daily Volume */} |
| 61 | + <div className="bg-white border border-slate-200 rounded-xl p-5 shadow-sm lg:col-span-2"> |
| 62 | + <h2 className="font-semibold text-slate-800 mb-4">Daily Transaction Volume</h2> |
| 63 | + <ResponsiveContainer width="100%" height={240}> |
| 64 | + <AreaChart data={dailyVolume}> |
| 65 | + <defs> |
| 66 | + <linearGradient id="vol" x1="0" y1="0" x2="0" y2="1"> |
| 67 | + <stop offset="5%" stopColor="#6366f1" stopOpacity={0.2} /> |
| 68 | + <stop offset="95%" stopColor="#6366f1" stopOpacity={0} /> |
| 69 | + </linearGradient> |
| 70 | + </defs> |
| 71 | + <CartesianGrid strokeDasharray="3 3" stroke="#f1f5f9" /> |
| 72 | + <XAxis dataKey="date" tick={{ fontSize: 11 }} /> |
| 73 | + <YAxis tick={{ fontSize: 11 }} /> |
| 74 | + <Tooltip formatter={(v) => [`${Number(v).toLocaleString()}`, "Volume"]} /> |
| 75 | + <Area type="monotone" dataKey="volume" stroke="#6366f1" fill="url(#vol)" strokeWidth={2} /> |
| 76 | + </AreaChart> |
| 77 | + </ResponsiveContainer> |
| 78 | + </div> |
| 79 | + |
| 80 | + {/* Status Distribution */} |
| 81 | + <div className="bg-white border border-slate-200 rounded-xl p-5 shadow-sm"> |
| 82 | + <h2 className="font-semibold text-slate-800 mb-4">Transaction Status</h2> |
| 83 | + <ResponsiveContainer width="100%" height={220}> |
| 84 | + <PieChart> |
| 85 | + <Pie data={statusDist} dataKey="value" nameKey="name" cx="50%" cy="50%" outerRadius={80} label={({ name, percent }) => `${name} ${((percent ?? 0) * 100).toFixed(0)}%`}> |
| 86 | + {statusDist.map((_, i) => <Cell key={i} fill={COLORS[i % COLORS.length]} />)} |
| 87 | + </Pie> |
| 88 | + <Tooltip /> |
| 89 | + </PieChart> |
| 90 | + </ResponsiveContainer> |
| 91 | + </div> |
| 92 | + |
| 93 | + {/* Asset Distribution */} |
| 94 | + <div className="bg-white border border-slate-200 rounded-xl p-5 shadow-sm"> |
| 95 | + <h2 className="font-semibold text-slate-800 mb-4">Volume by Asset</h2> |
| 96 | + <ResponsiveContainer width="100%" height={220}> |
| 97 | + <BarChart data={assetDist}> |
| 98 | + <CartesianGrid strokeDasharray="3 3" stroke="#f1f5f9" /> |
| 99 | + <XAxis dataKey="name" tick={{ fontSize: 11 }} /> |
| 100 | + <YAxis tick={{ fontSize: 11 }} /> |
| 101 | + <Tooltip formatter={(v) => [Number(v).toLocaleString(), "Volume"]} /> |
| 102 | + <Bar dataKey="value" radius={[4, 4, 0, 0]}> |
| 103 | + {assetDist.map((_, i) => <Cell key={i} fill={COLORS[i % COLORS.length]} />)} |
| 104 | + </Bar> |
| 105 | + </BarChart> |
| 106 | + </ResponsiveContainer> |
| 107 | + </div> |
| 108 | + |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + ); |
| 112 | +} |
0 commit comments