|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useRef } from "react"; |
| 4 | +import { |
| 5 | + Bar, |
| 6 | + BarChart, |
| 7 | + CartesianGrid, |
| 8 | + ResponsiveContainer, |
| 9 | + Tooltip, |
| 10 | + XAxis, |
| 11 | + YAxis, |
| 12 | +} from "recharts"; |
| 13 | +import { ChartExportButton } from "./ChartExportButton"; |
| 14 | +import { getTooltipContentStyle } from "@/lib/chart-utils"; |
| 15 | +import type { NetworkNewAccountsPoint } from "@/lib/network-api"; |
| 16 | + |
| 17 | +interface NewAccountsChartProps { |
| 18 | + data: NetworkNewAccountsPoint[]; |
| 19 | + loading?: boolean; |
| 20 | +} |
| 21 | + |
| 22 | +function formatCompact(value: number): string { |
| 23 | + return new Intl.NumberFormat("en-US", { |
| 24 | + notation: "compact", |
| 25 | + maximumFractionDigits: 1, |
| 26 | + }).format(value); |
| 27 | +} |
| 28 | + |
| 29 | +export function NewAccountsChart({ |
| 30 | + data, |
| 31 | + loading = false, |
| 32 | +}: NewAccountsChartProps) { |
| 33 | + const chartRef = useRef<HTMLDivElement>(null); |
| 34 | + |
| 35 | + const chartData = data.map((point) => ({ |
| 36 | + label: new Date(point.date).toLocaleDateString("en-US", { |
| 37 | + month: "short", |
| 38 | + day: "numeric", |
| 39 | + }), |
| 40 | + date: point.date, |
| 41 | + count: point.count, |
| 42 | + })); |
| 43 | + |
| 44 | + const total = chartData.reduce((sum, d) => sum + d.count, 0); |
| 45 | + const latest = chartData[chartData.length - 1]?.count ?? 0; |
| 46 | + const peak = chartData.length |
| 47 | + ? Math.max(...chartData.map((d) => d.count)) |
| 48 | + : 0; |
| 49 | + |
| 50 | + if (loading) { |
| 51 | + return ( |
| 52 | + <div |
| 53 | + className="glass-card rounded-2xl p-6 border border-border/50 h-[420px] animate-pulse" |
| 54 | + aria-busy="true" |
| 55 | + aria-label="Loading new accounts" |
| 56 | + > |
| 57 | + <div className="h-4 w-40 bg-white/5 rounded mb-4" /> |
| 58 | + <div className="h-8 w-64 bg-white/5 rounded mb-8" /> |
| 59 | + <div className="h-[260px] w-full bg-white/5 rounded-xl" /> |
| 60 | + </div> |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + if (chartData.length === 0) { |
| 65 | + return ( |
| 66 | + <section |
| 67 | + aria-labelledby="new-accounts-heading" |
| 68 | + className="glass-card rounded-2xl p-6 border border-border/50 flex flex-col items-center justify-center h-[420px]" |
| 69 | + > |
| 70 | + <div className="text-[10px] font-mono text-accent uppercase tracking-[0.2em] mb-2"> |
| 71 | + Network // New Accounts |
| 72 | + </div> |
| 73 | + <h2 |
| 74 | + id="new-accounts-heading" |
| 75 | + className="text-xl font-black tracking-tighter uppercase italic mb-2 opacity-50" |
| 76 | + > |
| 77 | + New Accounts |
| 78 | + </h2> |
| 79 | + <p className="text-sm font-mono text-muted-foreground uppercase tracking-widest text-center max-w-md"> |
| 80 | + No new-account series yet. Data appears once{" "} |
| 81 | + <code className="text-accent">/api/v1/network/new-accounts</code>{" "} |
| 82 | + returns daily account-creation counts. |
| 83 | + </p> |
| 84 | + </section> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + return ( |
| 89 | + <section |
| 90 | + ref={chartRef} |
| 91 | + aria-labelledby="new-accounts-heading" |
| 92 | + className="glass-card rounded-2xl p-6 border border-border/50" |
| 93 | + > |
| 94 | + <div className="flex flex-col md:flex-row md:items-start justify-between mb-8 gap-4"> |
| 95 | + <div className="flex-1"> |
| 96 | + <div className="text-[10px] font-mono text-accent uppercase tracking-[0.2em] mb-2"> |
| 97 | + Network // New Accounts |
| 98 | + </div> |
| 99 | + <h2 |
| 100 | + id="new-accounts-heading" |
| 101 | + className="text-xl font-black tracking-tighter uppercase italic mb-2" |
| 102 | + > |
| 103 | + New Accounts |
| 104 | + </h2> |
| 105 | + <p className="text-[10px] font-mono text-muted-foreground uppercase tracking-widest"> |
| 106 | + Accounts created per day |
| 107 | + </p> |
| 108 | + </div> |
| 109 | + <ChartExportButton chartRef={chartRef} chartName="New Accounts" /> |
| 110 | + </div> |
| 111 | + |
| 112 | + <div className="grid grid-cols-3 gap-4 mb-8"> |
| 113 | + <div className="p-3 rounded-xl bg-slate-900/30 border border-white/5"> |
| 114 | + <p className="text-[9px] font-mono text-muted-foreground uppercase tracking-wider mb-1"> |
| 115 | + Latest day |
| 116 | + </p> |
| 117 | + <p className="text-xl font-black font-mono tracking-tighter text-emerald-400"> |
| 118 | + {formatCompact(latest)} |
| 119 | + </p> |
| 120 | + </div> |
| 121 | + <div className="p-3 rounded-xl bg-slate-900/30 border border-white/5"> |
| 122 | + <p className="text-[9px] font-mono text-muted-foreground uppercase tracking-wider mb-1"> |
| 123 | + Period total |
| 124 | + </p> |
| 125 | + <p className="text-xl font-black font-mono tracking-tighter text-foreground/80"> |
| 126 | + {formatCompact(total)} |
| 127 | + </p> |
| 128 | + </div> |
| 129 | + <div className="p-3 rounded-xl bg-slate-900/30 border border-white/5"> |
| 130 | + <p className="text-[9px] font-mono text-muted-foreground uppercase tracking-wider mb-1"> |
| 131 | + Peak day |
| 132 | + </p> |
| 133 | + <p className="text-xl font-black font-mono tracking-tighter text-accent"> |
| 134 | + {formatCompact(peak)} |
| 135 | + </p> |
| 136 | + </div> |
| 137 | + </div> |
| 138 | + |
| 139 | + <div className="h-[300px] w-full"> |
| 140 | + <ResponsiveContainer width="100%" height="100%"> |
| 141 | + <BarChart |
| 142 | + data={chartData} |
| 143 | + margin={{ top: 10, right: 10, left: 0, bottom: 0 }} |
| 144 | + > |
| 145 | + <CartesianGrid |
| 146 | + strokeDasharray="3 3" |
| 147 | + stroke="rgba(255,255,255,0.05)" |
| 148 | + vertical={false} |
| 149 | + /> |
| 150 | + <XAxis |
| 151 | + dataKey="label" |
| 152 | + stroke="rgba(255,255,255,0.3)" |
| 153 | + tick={{ fontSize: 10, fontFamily: "monospace" }} |
| 154 | + axisLine={false} |
| 155 | + tickLine={false} |
| 156 | + dy={10} |
| 157 | + /> |
| 158 | + <YAxis |
| 159 | + stroke="rgba(255,255,255,0.3)" |
| 160 | + tickFormatter={formatCompact} |
| 161 | + tick={{ fontSize: 10, fontFamily: "monospace" }} |
| 162 | + axisLine={false} |
| 163 | + tickLine={false} |
| 164 | + dx={-10} |
| 165 | + width={48} |
| 166 | + /> |
| 167 | + <Tooltip |
| 168 | + contentStyle={getTooltipContentStyle({ |
| 169 | + backgroundColor: "rgba(15, 23, 42, 0.9)", |
| 170 | + border: "1px solid rgba(255, 255, 255, 0.1)", |
| 171 | + borderRadius: "12px", |
| 172 | + fontSize: "10px", |
| 173 | + fontFamily: "monospace", |
| 174 | + })} |
| 175 | + labelStyle={{ color: "#94a3b8", marginBottom: "4px" }} |
| 176 | + formatter={(value) => [ |
| 177 | + formatCompact(typeof value === "number" ? value : Number(value)), |
| 178 | + "New accounts", |
| 179 | + ]} |
| 180 | + /> |
| 181 | + <Bar |
| 182 | + dataKey="count" |
| 183 | + name="New accounts" |
| 184 | + fill="#f59e0b" |
| 185 | + radius={[4, 4, 0, 0]} |
| 186 | + /> |
| 187 | + </BarChart> |
| 188 | + </ResponsiveContainer> |
| 189 | + </div> |
| 190 | + </section> |
| 191 | + ); |
| 192 | +} |
0 commit comments