|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { createClient } from "@/app/lib/supabase/client"; |
| 4 | +import { Database } from "@/app/supabase-types"; |
| 5 | +import { User } from "@supabase/supabase-js"; |
| 6 | +import { useEffect, useState } from "react"; |
| 7 | +import TopInsights from "./Widgets/TopInsights"; |
| 8 | +import FeatureInsights from "./Widgets/FeatureInsights"; |
| 9 | +import RankingInsights, { |
| 10 | + AICoderStat, |
| 11 | + CoderStats, |
| 12 | +} from "./Widgets/RankingInsights"; |
| 13 | +import UserLists from "./Widgets/UserLists"; |
| 14 | + |
| 15 | +const supabase = createClient(); |
| 16 | + |
| 17 | +type UserStat = Database["public"]["Views"]["top_user_stats"]["Row"]; |
| 18 | +type CategoryStat = { |
| 19 | + name: string; |
| 20 | + users: Set<string>; |
| 21 | + totalSeconds: number; |
| 22 | +}; |
| 23 | + |
| 24 | +export default function Dashboard({ user }: { user: User }) { |
| 25 | + const [loading, setLoading] = useState(false); |
| 26 | + const [users, setUsers] = useState<UserStat[]>([]); |
| 27 | + const [totalThreads, setTotalThreads] = useState(0); |
| 28 | + const [totalMessages, setTotalMessages] = useState(0); |
| 29 | + const [totalLeaderboards, setTotalLeaderboards] = useState(0); |
| 30 | + const [totalFlexes, setTotalFlexes] = useState(0); |
| 31 | + const categoryMap: Record<string, CategoryStat> = {}; |
| 32 | + |
| 33 | + useEffect(() => { |
| 34 | + async function fetchUsers() { |
| 35 | + setLoading(true); |
| 36 | + const [ |
| 37 | + { data: topUserStats }, |
| 38 | + { count: threads }, |
| 39 | + { count: messages }, |
| 40 | + { count: leaderboard }, |
| 41 | + { count: userFlexes }, |
| 42 | + ] = await Promise.all([ |
| 43 | + supabase.from("top_user_stats").select("*"), |
| 44 | + supabase |
| 45 | + .from("conversations") |
| 46 | + .select("*", { count: "exact", head: true }), |
| 47 | + supabase.from("messages").select("*", { count: "exact", head: true }), |
| 48 | + supabase |
| 49 | + .from("leaderboards") |
| 50 | + .select("*", { count: "exact", head: true }), |
| 51 | + supabase |
| 52 | + .from("user_flexes") |
| 53 | + .select("*", { count: "exact", head: true }), |
| 54 | + ]); |
| 55 | + |
| 56 | + setUsers(topUserStats || []); |
| 57 | + setTotalThreads(threads || 0); |
| 58 | + setTotalMessages(messages || 0); |
| 59 | + setTotalLeaderboards(leaderboard || 0); |
| 60 | + setTotalFlexes(userFlexes || 0); |
| 61 | + setLoading(false); |
| 62 | + } |
| 63 | + |
| 64 | + fetchUsers(); |
| 65 | + |
| 66 | + const interval = setInterval(fetchUsers, 5000); |
| 67 | + return () => clearInterval(interval); |
| 68 | + }, [user.id]); |
| 69 | + |
| 70 | + /* |
| 71 | + * total users and coding time |
| 72 | + */ |
| 73 | + const totalUsers = users.length; |
| 74 | + const totalSeconds = users.reduce( |
| 75 | + (sum, u) => sum + (u.total_seconds || 0), |
| 76 | + 0, |
| 77 | + ); |
| 78 | + const sortedUsers = [...users].sort( |
| 79 | + (a, b) => (b.total_seconds || 0) - (a.total_seconds || 0), |
| 80 | + ); |
| 81 | + |
| 82 | + /* |
| 83 | + * get the top and least coders |
| 84 | + */ |
| 85 | + const top3 = sortedUsers.slice(0, 3); |
| 86 | + const bottom3 = [...sortedUsers].reverse().slice(0, 3); |
| 87 | + |
| 88 | + /* |
| 89 | + * category stats |
| 90 | + */ |
| 91 | + users.forEach((u) => { |
| 92 | + const categories = (u.categories || []) as { |
| 93 | + name: string; |
| 94 | + total_seconds: number; |
| 95 | + }[]; |
| 96 | + |
| 97 | + categories.forEach((c) => { |
| 98 | + if (!categoryMap[c.name]) { |
| 99 | + categoryMap[c.name] = { |
| 100 | + name: c.name, |
| 101 | + users: new Set(), |
| 102 | + totalSeconds: 0, |
| 103 | + }; |
| 104 | + } |
| 105 | + |
| 106 | + categoryMap[c.name].users.add(u.email || u.user_id || "unknown"); |
| 107 | + categoryMap[c.name].totalSeconds += c.total_seconds || 0; |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + const categoryStats = Object.values(categoryMap).map((c) => ({ |
| 112 | + name: c.name, |
| 113 | + userCount: c.users.size, |
| 114 | + hours: Math.floor(c.totalSeconds / 3600), |
| 115 | + })); |
| 116 | + |
| 117 | + /* |
| 118 | + * vibe coders |
| 119 | + */ |
| 120 | + const aiCoders = users |
| 121 | + .map((u) => { |
| 122 | + const categories = (u.categories || []) as { |
| 123 | + name: string; |
| 124 | + total_seconds: number; |
| 125 | + }[]; |
| 126 | + |
| 127 | + const aiTotalSeconds = categories |
| 128 | + .filter((c) => c.name.toLowerCase().includes("ai")) |
| 129 | + .reduce((sum, c) => sum + (c.total_seconds || 0), 0); |
| 130 | + |
| 131 | + return { |
| 132 | + ...u, |
| 133 | + aiTotalSeconds, |
| 134 | + }; |
| 135 | + }) |
| 136 | + .filter((u) => u.aiTotalSeconds > 0) |
| 137 | + .sort((a, b) => b.aiTotalSeconds - a.aiTotalSeconds) |
| 138 | + .slice(0, 6); |
| 139 | + |
| 140 | + return ( |
| 141 | + <div className="p-6 md:p-8 space-y-6"> |
| 142 | + {/* Header */} |
| 143 | + <div className="flex flex-row justify-between items-center w-full"> |
| 144 | + <div> |
| 145 | + <h1 className="text-3xl font-bold text-indigo-400">Admin Panel</h1> |
| 146 | + </div> |
| 147 | + </div> |
| 148 | + |
| 149 | + <TopInsights |
| 150 | + totalUsers={totalUsers} |
| 151 | + totalSeconds={totalSeconds} |
| 152 | + totalThreads={totalThreads} |
| 153 | + totalMessages={totalMessages} |
| 154 | + /> |
| 155 | + |
| 156 | + <FeatureInsights |
| 157 | + totalLeaderboards={totalLeaderboards} |
| 158 | + totalUsers={totalUsers} |
| 159 | + totalFlexes={totalFlexes} |
| 160 | + /> |
| 161 | + |
| 162 | + <RankingInsights |
| 163 | + top3={top3 as CoderStats[]} |
| 164 | + bottom3={bottom3 as CoderStats[]} |
| 165 | + categoryStats={categoryStats} |
| 166 | + aiCoders={aiCoders as AICoderStat[]} |
| 167 | + /> |
| 168 | + |
| 169 | + <UserLists users={users as UserStat[]} loading={loading} /> |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments