Redesign academy analytics with compact metrics and chart overview

This commit is contained in:
Yared Yemane 2026-11-03 12:43:50 +03:00
parent fedb7b0fbe
commit 66028a434b
3 changed files with 153 additions and 87 deletions

View File

@ -0,0 +1,98 @@
import { ArrowUpRight, BookOpen, CreditCard, Users } from "lucide-react"
import { Link } from "react-router-dom"
import { Area, Bar, BarChart, CartesianGrid, ComposedChart, Line, ResponsiveContainer, Tooltip, XAxis, YAxis } from "recharts"
import type { ReactNode } from "react"
import { SensitiveChart, SensitiveValue } from "../ui/sensitive-value"
import { formatAnalyticsSeriesDate, formatRevenueAxisTick, getSubscriptionMetrics } from "../../lib/analytics"
import type { DashboardData } from "../../types/analytics.types"
const tooltipStyle = { background: "var(--analytics-panel)", border: "1px solid var(--analytics-line)", borderRadius: 4, color: "var(--gs-900)", fontSize: 12 }
function Metric({ label, value, detail }: { label: string; value: ReactNode; detail: string }) {
return <div className="analytics-strip-metric"><p>{label}</p><strong>{value}</strong><span>{detail}</span></div>
}
export function AcademyAnalyticsOverview({ dashboard, periodLabel, revenuePoints, revenueLabel, monthly }: {
dashboard: DashboardData
periodLabel: string
revenuePoints: { revenue: number; month?: string; date?: string }[]
revenueLabel: string
monthly: boolean
}) {
const { users, subscriptions, payments, courses, content } = dashboard
const metrics = getSubscriptionMetrics(subscriptions)
const active = subscriptions.snapshot?.active_learners ?? metrics.active
const expiring = subscriptions.snapshot?.expiring_within_7_days
const successRate = payments.total_payments > 0 ? payments.successful_payments / payments.total_payments * 100 : null
const activity = new Map<string, { date: string; registrations: number; subscriptions: number }>()
for (const row of users.registrations_last_30_days) activity.set(row.date, { date: row.date, registrations: row.count, subscriptions: 0 })
for (const row of subscriptions.new_subscriptions_last_30_days) {
const current = activity.get(row.date) ?? { date: row.date, registrations: 0, subscriptions: 0 }
current.subscriptions = row.count; activity.set(row.date, current)
}
const activityData = [...activity.values()].sort((a, b) => a.date.localeCompare(b.date))
const catalogCount = courses.exam_prep?.catalog_courses ?? courses.total_courses
const lessonCount = courses.exam_prep?.lessons ?? courses.total_videos
return <div className="space-y-5">
<div className="analytics-metric-strip">
<Metric label="Registered learners" value={users.total_users.toLocaleString()} detail={`${users.new_month.toLocaleString()} new this month`} />
<Metric label="Active learners" value={active.toLocaleString()} detail="Current subscription access" />
<Metric label="Academy revenue" value={<SensitiveValue>{payments.total_revenue.toLocaleString()} <small>ETB</small></SensitiveValue>} detail="Successful payments in period" />
<Metric label="Payment success" value={successRate === null ? "N/A" : `${successRate.toFixed(1)}%`} detail={`${payments.successful_payments.toLocaleString()} of ${payments.total_payments.toLocaleString()} payments`} />
<Metric label="Makeup courses" value={catalogCount.toLocaleString()} detail={`${lessonCount.toLocaleString()} lessons in catalog`} />
<Metric label="Question bank" value={content.total_questions.toLocaleString()} detail={`${content.total_question_sets.toLocaleString()} assessment sets`} />
</div>
<div className="grid min-w-0 gap-5 xl:grid-cols-[minmax(250px,0.85fr)_minmax(0,1.75fr)]">
<div className="flex min-w-0 flex-col gap-4">
<section className="analytics-status-panel">
<div className="flex items-center justify-between gap-3"><h2>Academy pulse</h2><span className="analytics-live-label">Current snapshot</span></div>
<p className="mt-6 text-xs text-white/65">Learners with active access</p>
<div className="mt-2 flex items-end justify-between gap-3"><strong className="text-4xl font-semibold tracking-tight">{active.toLocaleString()}</strong><Users className="h-10 w-10 text-white/25" aria-hidden="true" /></div>
<div className="mt-6 border-t border-white/20 pt-4 text-xs leading-5 text-white/75">{expiring === undefined ? "Subscription access reflects the latest academy snapshot." : `${expiring.toLocaleString()} subscriptions expire within the next 7 days.`}</div>
</section>
<div className="analytics-panel grid flex-1 grid-cols-2 divide-x divide-grayScale-100 p-5">
<div><p className="text-[11px] text-grayScale-400">New learners today</p><p className="mt-2 text-xl font-semibold">{users.new_today.toLocaleString()}</p></div>
<div className="pl-5"><p className="text-[11px] text-grayScale-400">New subscriptions today</p><p className="mt-2 text-xl font-semibold">{subscriptions.new_today.toLocaleString()}</p></div>
</div>
</div>
<section className="analytics-panel min-w-0 p-5">
<div className="flex flex-wrap items-start justify-between gap-3"><div><h2>Learner activity</h2><p className="analytics-panel-caption">Registrations and new subscriptions / {periodLabel}</p></div><div className="analytics-chart-legend"><span><i style={{background:"#4f46cf"}} />Registrations</span><span><i style={{background:"#36b7df"}} />Subscriptions</span></div></div>
<div className="mt-5 h-[240px] min-w-0" role="img" aria-label="Registrations and new subscriptions over the selected period">
{activityData.length ? <ResponsiveContainer width="100%" height="100%" minWidth={0}>
<ComposedChart data={activityData} margin={{top:12,right:10,left:0,bottom:0}}>
<defs><linearGradient id="academyActivityFill" x1="0" y1="0" x2="0" y2="1"><stop offset="0%" stopColor="#655ce0" stopOpacity={0.2} /><stop offset="100%" stopColor="#655ce0" stopOpacity={0.01} /></linearGradient></defs>
<CartesianGrid vertical={false} stroke="var(--analytics-line)" strokeDasharray="3 4" />
<XAxis dataKey="date" tickFormatter={formatAnalyticsSeriesDate} tickLine={false} axisLine={false} fontSize={10} minTickGap={25} tick={{fill:"var(--gs-500)"}} />
<YAxis allowDecimals={false} tickLine={false} axisLine={false} width={35} fontSize={10} tick={{fill:"var(--gs-500)"}} />
<Tooltip contentStyle={tooltipStyle} labelFormatter={label=>formatAnalyticsSeriesDate(String(label))} />
<Area type="monotone" dataKey="registrations" name="Registrations" stroke="#4f46cf" strokeWidth={2} fill="url(#academyActivityFill)" isAnimationActive={false} />
<Line type="monotone" dataKey="subscriptions" name="Subscriptions" stroke="#36b7df" strokeWidth={2} dot={false} isAnimationActive={false} />
</ComposedChart>
</ResponsiveContainer> : <p className="analytics-empty">No learner activity in this period.</p>}
</div>
</section>
</div>
<div className="grid min-w-0 gap-5 xl:grid-cols-[minmax(0,1.75fr)_minmax(250px,0.85fr)]">
<section className="analytics-panel min-w-0 p-5">
<div className="flex flex-wrap items-start justify-between gap-3"><div><h2>Revenue overview</h2><p className="analytics-panel-caption">Successful payments / ETB / {revenueLabel}</p></div><span className="analytics-period-badge">{monthly ? "Monthly" : "Daily"}</span></div>
<div className="mt-5 h-[220px] min-w-0"><SensitiveChart>
{revenuePoints.some(point=>point.revenue>0) ? <ResponsiveContainer width="100%" height="100%" minWidth={0}>
<BarChart data={revenuePoints} margin={{top:12,right:10,left:0,bottom:0}} barCategoryGap="35%">
<CartesianGrid vertical={false} stroke="var(--analytics-line)" />
<XAxis dataKey={monthly ? "month" : "date"} tickLine={false} axisLine={false} fontSize={10} minTickGap={20} tick={{fill:"var(--gs-500)"}} />
<YAxis tickFormatter={formatRevenueAxisTick} tickLine={false} axisLine={false} width={45} fontSize={10} tick={{fill:"var(--gs-500)"}} />
<Tooltip contentStyle={tooltipStyle} cursor={{fill:"var(--gs-100)"}} formatter={value=>[`ETB ${Number(value).toLocaleString()}`,"Revenue"]} />
<Bar dataKey="revenue" name="Revenue" fill="#4f46cf" radius={[2,2,0,0]} maxBarSize={25} isAnimationActive={false} />
</BarChart>
</ResponsiveContainer> : <p className="analytics-empty">No revenue recorded in this period.</p>}
</SensitiveChart></div>
</section>
<section className="analytics-panel min-w-0 p-5">
<h2>Academy at a glance</h2><p className="analytics-panel-caption">Your teaching workspace</p>
<div className="mt-4 divide-y divide-grayScale-100">
{[{label:"Makeup curriculum",detail:`${catalogCount.toLocaleString()} courses / ${lessonCount.toLocaleString()} lessons`,path:"/new-content/courses",icon:BookOpen},{label:"Learner community",detail:`${users.total_users.toLocaleString()} registered learners`,path:"/users/list",icon:Users},{label:"Payments",detail:`${payments.successful_payments.toLocaleString()} successful transactions`,path:"/payments",icon:CreditCard}].map(({label,detail,path,icon:Icon})=><Link key={path} to={path} className="group flex items-center gap-3 py-4 text-sm"><span className="analytics-shortcut-icon"><Icon size={16} /></span><span className="min-w-0 flex-1"><span className="block text-xs font-medium">{label}</span><span className="mt-1 block text-[11px] text-grayScale-400">{detail}</span></span><ArrowUpRight size={14} className="text-grayScale-400 group-hover:text-grayScale-900" /></Link>)}
</div>
</section>
</div>
</div>
}

View File

@ -936,3 +936,36 @@
@media (min-width: 1600px) { .academy-login-brand { padding-left: 28%; } }
@media (max-width: 1023px) { .academy-login { grid-template-columns: 1fr; } .academy-login-art { display: none; } .academy-login-panel { min-height: 100dvh; padding: 48px 24px; } }
@media (max-width: 359px) { .academy-login-panel { padding: 32px 20px; } }
/* Analytics workspace: compact metrics, restrained panels and readable chart accents. */
.academy-analytics { --analytics-panel: #fff; --analytics-line: #eceef3; color: var(--gs-900); }
.academy-analytics .analytics-page-heading h1 { font-family: "DM Sans", sans-serif; font-size: 24px; font-weight: 500; }
.analytics-tabs { padding: 0 0 2px; }
.analytics-tabs + .space-y-5 { padding-top: 4px; }
.analytics-metric-strip { display: grid; grid-template-columns: repeat(6,minmax(0,1fr)); padding: 22px 0; border-bottom: 1px solid var(--analytics-line); }
.analytics-strip-metric { min-width: 0; padding: 0 18px; border-right: 1px solid var(--analytics-line); }
.analytics-strip-metric:first-child { padding-left: 0; }
.analytics-strip-metric:last-child { border-right: 0; }
.analytics-strip-metric > p { font-size: 11px; color: var(--gs-500); }
.analytics-strip-metric > strong { display: block; margin-top: 7px; font-size: 21px; font-weight: 600; line-height: 1.3; letter-spacing: -.04em; overflow-wrap: anywhere; }
.analytics-strip-metric small { font-size: 10px; font-weight: 500; letter-spacing: 0; }
.analytics-strip-metric > span { display: block; margin-top: 6px; font-size: 10px; color: var(--gs-500); line-height: 1.6; }
.analytics-panel { border: 1px solid var(--analytics-line); border-radius: 5px; background: var(--analytics-panel); box-shadow: 0 2px 5px rgb(20 24 60 / .015); }
.academy-analytics .analytics-panel h2, .academy-analytics .analytics-status-panel h2 { font-family: "DM Sans", sans-serif; font-size: 12px; font-weight: 600; letter-spacing: 0; }
.analytics-panel-caption { font-size: 11px; color: var(--gs-500); margin-top: 5px; line-height: 1.6; }
.analytics-status-panel { padding: 22px; color: white; background: linear-gradient(115deg,#3836b0,#282878); border-radius: 5px; }
.analytics-live-label { font-size: 9px; color: rgb(255 255 255 / .65); }
.analytics-chart-legend { display: flex; flex-wrap: wrap; gap: 12px; font-size: 10px; color: var(--gs-500); }
.analytics-chart-legend span { display: inline-flex; align-items: center; gap: 5px; }
.analytics-chart-legend i { width: 6px; height: 6px; border-radius: 50%; }
.analytics-period-badge { font-size: 10px; background: var(--gs-100); color: var(--gs-600); padding: 5px 9px; border-radius: 3px; }
.analytics-shortcut-icon { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 4px; color: #5755af; background: #f0effb; flex-shrink: 0; }
.analytics-empty { display: grid; height: 100%; place-content: center; font-size: 12px; color: var(--gs-500); text-align: center; padding: 20px; }
.academy-analytics .analytics-detail-section { border-radius: 5px; }
html.dark .academy-analytics { --analytics-panel: #202124; --analytics-line: #36373c; }
html.dark .analytics-shortcut-icon { background: #32304a; color: #bab6fb; }
@media(max-width:1199px) { .analytics-metric-strip { grid-template-columns: repeat(3,minmax(0,1fr)); row-gap: 22px; } .analytics-strip-metric:nth-child(3) { border-right: 0; } .analytics-strip-metric:nth-child(4) { padding-left: 0; } }
@media(max-width:539px) { .analytics-metric-strip { grid-template-columns: repeat(2,minmax(0,1fr)); } .analytics-strip-metric:nth-child(odd) { padding-left: 0; border-right: 1px solid var(--analytics-line); } .analytics-strip-metric:nth-child(even) { padding-left: 16px; border-right: 0; } .analytics-strip-metric > strong { font-size: 20px; } }
.academy-analytics .analytics-detail-section h3 { font-family: "DM Sans", sans-serif; font-size: 13px; font-weight: 600; }
.academy-analytics .analytics-kpi { border-radius: 5px; }

View File

@ -36,6 +36,7 @@ import {
import { getDashboard } from "../../api/analytics.api";
import spinnerSrc from "../../assets/05d37a91-04f9-41d8-99a2-b11bf20a6af2.png";
import alertSrc from "../../assets/Alert.svg";
import { AcademyAnalyticsOverview } from "../../components/analytics/AcademyAnalyticsOverview";
import { AnalyticsTimeRangeFilter } from "../../components/analytics/AnalyticsTimeRangeFilter";
import { getDashboardFilterLabel } from "../../components/analytics/AnalyticsTimeRangeFilter.shared";
@ -48,7 +49,6 @@ import { Card, CardContent, CardHeader, CardTitle } from "../../components/ui/ca
import {
SensitiveChart,
SensitiveRevealProvider,
SensitiveValue,
} from "../../components/ui/sensitive-value";
import {
aggregateRevenueByMonth,
@ -120,7 +120,7 @@ function KpiCard({
return (
<Card
className={cn(
"border-grayScale-100/90 bg-white shadow-sm transition-all duration-200 hover:-translate-y-0.5 hover:shadow-md",
"analytics-kpi border-grayScale-100 bg-white shadow-none",
className,
)}
>
@ -311,11 +311,12 @@ function Section({
const [open, setOpen] = useState(defaultOpen)
return (
<div className="rounded-2xl border border-grayScale-100 bg-white shadow-sm">
<div className="analytics-detail-section rounded-md border border-grayScale-100 bg-white shadow-none">
<button
type="button"
onClick={() => setOpen((v) => !v)}
className="flex w-full items-center gap-3 px-6 py-4 text-left transition-colors hover:bg-grayScale-50/80"
aria-expanded={open}
className="flex w-full items-center gap-3 px-5 py-3 text-left transition-colors hover:bg-grayScale-50/80"
>
<div className="grid h-9 w-9 shrink-0 place-items-center rounded-lg bg-brand-100/60 text-brand-600">
<Icon className="h-4 w-4" />
@ -472,12 +473,12 @@ export function AnalyticsPage() {
return (
<SensitiveRevealProvider>
<div className="mx-auto w-full max-w-[1280px] px-2 pb-6 sm:px-4">
<div className="academy-analytics mx-auto w-full max-w-[1500px] px-2 pb-6 sm:px-4">
{/* Header */}
<div className="mb-7 flex flex-wrap items-end justify-between gap-4">
<div className="analytics-page-heading mb-6 flex flex-wrap items-end justify-between gap-4">
<div>
<div className="mb-1 text-xs font-semibold uppercase tracking-wide text-grayScale-400">Analytics</div>
<h1 className="text-3xl font-semibold tracking-tight text-grayScale-900">Platform Overview</h1>
<h1 className="text-3xl font-semibold tracking-tight text-grayScale-900">Academy performance</h1>
</div>
<div className="flex flex-wrap items-center gap-3">
<span className="text-xs text-grayScale-400">
@ -498,87 +499,21 @@ export function AnalyticsPage() {
</div>
)}
{/* Summary Tabs */}
<div className="mb-6 rounded-2xl border border-grayScale-100 bg-white px-5 pt-4 shadow-sm">
<div className="-mb-px flex gap-6">
<button
onClick={() => setActiveSummaryTab("key")}
className={cn(
"relative px-1 pb-3.5 pt-1 text-sm font-semibold transition-all",
activeSummaryTab === "key" ? "text-brand-600" : "text-grayScale-400 hover:text-grayScale-700",
)}
>
Key Metrics
{activeSummaryTab === "key" && (
<span className="absolute inset-x-0 bottom-0 h-0.5 rounded-full bg-brand-500" />
)}
</button>
<button
onClick={() => setActiveSummaryTab("content")}
className={cn(
"relative px-1 pb-3.5 pt-1 text-sm font-semibold transition-all",
activeSummaryTab === "content" ? "text-brand-600" : "text-grayScale-400 hover:text-grayScale-700",
)}
>
Content &amp; Platform
{activeSummaryTab === "content" && (
<span className="absolute inset-x-0 bottom-0 h-0.5 rounded-full bg-brand-500" />
)}
</button>
<button
onClick={() => setActiveSummaryTab("operations")}
className={cn(
"relative px-1 pb-3.5 pt-1 text-sm font-semibold transition-all",
activeSummaryTab === "operations" ? "text-brand-600" : "text-grayScale-400 hover:text-grayScale-700",
)}
>
Operations
{activeSummaryTab === "operations" && (
<span className="absolute inset-x-0 bottom-0 h-0.5 rounded-full bg-brand-500" />
)}
</button>
</div>
</div>
<nav className="analytics-tabs mb-0 flex flex-wrap gap-6 border-b border-grayScale-200" aria-label="Analytics views">
{([{key:"key", label:"Overview"}, {key:"content", label:"Content & Platform"}, {key:"operations", label:"Operations"}] as const).map(tab => <button
key={tab.key} type="button" aria-pressed={activeSummaryTab === tab.key} onClick={() => setActiveSummaryTab(tab.key)}
className={cn("relative px-1 pb-3.5 pt-1 text-xs font-medium transition-colors",activeSummaryTab === tab.key ? "text-brand-600" : "text-grayScale-400 hover:text-grayScale-700")}>
{tab.label}{activeSummaryTab === tab.key && <span className="absolute inset-x-0 bottom-0 h-0.5 bg-brand-500" />}
</button>)}
</nav>
<div className="space-y-5">
{activeSummaryTab === "key" && (
<Section title="Key Metrics" icon={TrendingUp} defaultOpen>
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-4">
<KpiCard
icon={Users}
label="Total Users"
value={formatNumber(users.total_users)}
sub={`+${users.new_today} today · +${users.new_week} this week · +${users.new_month} this month`}
trend={users.new_month > 0 ? "up" : "neutral"}
/>
<KpiCard
icon={CreditCard}
label="Active Learners"
value={formatNumber(snapshot?.active_learners ?? subscriptionMetrics.active)}
sub={`${formatNumber(snapshot?.active_now ?? subscriptionMetrics.active)} active subs · ${subscriptionMetrics.inactive} inactive`}
trend={subscriptions.new_month > 0 ? "up" : "neutral"}
/>
<KpiCard
icon={DollarSign}
label="Total Revenue"
value={
<SensitiveValue>
{`ETB ${formatNumber(payments.total_revenue)}`}
</SensitiveValue>
}
sub={
<>
{payments.successful_payments}/{payments.total_payments} successful · Avg{" "}
<SensitiveValue showToggle={false}>
ETB {payments.avg_transaction_value.toLocaleString()}
</SensitiveValue>
</>
}
trend={payments.total_revenue > 0 ? "up" : "neutral"}
/>
</div>
</Section>
)}
{activeSummaryTab === "key" && <AcademyAnalyticsOverview
dashboard={dashboard}
periodLabel={periodFilterLabel}
revenuePoints={monthlyRevenueTrendData}
revenueLabel={useMonthlyRevenueTrend ? String(monthlyTrendYear) : periodFilterLabel}
monthly={useMonthlyRevenueTrend}
/>}
{activeSummaryTab === "operations" && (
<>