"use client"; import { useEffect, useMemo, useState } from "react"; import { NoPermissionState } from "@/components/auth/no-permission-state"; import { useSuperAdminSession } from "@/components/auth/session-context"; import { ChannelPieChart, InsightBarChart } from "@/components/dashboard/charts"; import { PageHeader } from "@/components/dashboard/page-header"; import { StatCard } from "@/components/dashboard/stat-card"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { EmptyState } from "@/components/ui/empty-state"; import { useToast } from "@/components/ui/toast"; import { getSuperAdminCharts, getSuperAdminOverview } from "@/lib/api/superadmin"; import { SUPERADMIN_PERMISSIONS, hasPermission } from "@/lib/permissions"; import type { SuperAdminBreakdownItem, SuperAdminChartsResponse, SuperAdminChartPoint, SuperAdminOverviewResponse, } from "@/types/api"; import type { ChannelPoint, Insight, StatMetric } from "@/types"; type AnalyticsSnapshot = { overview: SuperAdminOverviewResponse | null; charts: SuperAdminChartsResponse | null; }; function toInsight(points: SuperAdminChartPoint[]): Insight[] { return points.map((point) => ({ label: point.label, value: point.count })); } function toInsightBreakdown(items: SuperAdminBreakdownItem[]): Insight[] { return items.map((item) => ({ label: item.label, value: item.value })); } function toChannel(items: SuperAdminBreakdownItem[]): ChannelPoint[] { return items.map((item) => ({ name: item.label, value: item.value })); } export default function AnalyticsPage() { const { permissions } = useSuperAdminSession(); const [snapshot, setSnapshot] = useState({ overview: null, charts: null, }); const [loading, setLoading] = useState(true); const { toast } = useToast(); const canReadAnalytics = hasPermission(permissions, SUPERADMIN_PERMISSIONS.ANALYTICS_READ); const canReadOverview = hasPermission(permissions, SUPERADMIN_PERMISSIONS.OVERVIEW_READ); useEffect(() => { let active = true; const loadAnalytics = async () => { if (!canReadAnalytics) { setLoading(false); return; } setLoading(true); try { const [overview, charts] = await Promise.all([ canReadOverview ? getSuperAdminOverview() : Promise.resolve(null), getSuperAdminCharts({ range: "30d" }), ]); if (!active) return; setSnapshot({ overview, charts }); } catch (error) { if (!active) return; toast({ title: "Failed to load analytics", description: String(error), variant: "danger" }); } finally { if (active) setLoading(false); } }; void loadAnalytics(); return () => { active = false; }; }, [canReadAnalytics, canReadOverview, toast]); const metrics = snapshot.overview?.metrics; const dashboardMetrics: StatMetric[] = useMemo( () => [ { id: "users", label: "Total users", value: loading ? "..." : String(metrics?.usersCount ?? 0), delta: `${metrics?.adminsCount ?? 0} admin accounts`, trend: "up", }, { id: "posts", label: "Published content", value: loading ? "..." : String(metrics?.postsCount ?? 0), delta: `${metrics?.commentsCount ?? 0} tracked comments`, trend: "up", }, { id: "marketplace", label: "Marketplace", value: loading ? "..." : String(metrics?.marketplaceListingsCount ?? 0), delta: `${metrics?.repairShopsCount ?? 0} repair shops`, trend: "neutral", }, { id: "moderation", label: "Moderation load", value: loading ? "..." : String((metrics?.flaggedPostsCount ?? 0) + (metrics?.flaggedCommentsCount ?? 0)), delta: `${metrics?.hiddenPostsCount ?? 0} hidden posts and ${metrics?.hiddenCommentsCount ?? 0} hidden comments`, trend: (metrics?.flaggedPostsCount ?? 0) + (metrics?.flaggedCommentsCount ?? 0) > 0 ? "down" : "neutral", }, ], [loading, metrics], ); const charts = snapshot.charts; const userSeries = toInsight(charts?.series.users ?? []); const postSeries = toInsight(charts?.series.posts ?? []); const listingSeries = toInsight(charts?.series.listings ?? []); const roleBreakdown = toChannel(charts?.breakdowns.userRoles ?? []); const listingBreakdown = toInsightBreakdown(charts?.breakdowns.listingCategories ?? []); const moderationBreakdown = toInsightBreakdown(charts?.breakdowns.moderation ?? []); if (!canReadAnalytics) { return (
); } return (
{canReadOverview ? (
{dashboardMetrics.map((metric) => ( ))}
) : null}
User signups - last 30 days {userSeries.length ? ( ) : ( )} Published content - last 30 days {postSeries.length ? ( ) : ( )}
User role distribution {roleBreakdown.length ? ( ) : ( )} Marketplace categories {listingBreakdown.length ? ( ) : ( )}
Marketplace creation - last 30 days {listingSeries.length ? ( ) : ( )} Current moderation states {moderationBreakdown.length ? ( ) : ( )}
); }