All files / src/app/admin/referrals/leaderboard page.tsx

0% Statements 0/239
100% Branches 0/0
0% Functions 0/1
0% Lines 0/239

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
"use client";

import { useState, useEffect } from "react";
import { clientLogger } from "@/lib/logging/clientLogger";
import Link from "next/link";
import { Icon } from "@/components/ui/icons";
import { Button } from "@/components/ui/Button";

interface Referrer {
  id: number;
  name: string;
  email: string;
  referralCount: number;
  rewardsEarned: number;
}

interface LeaderboardData {
  topReferrers: Referrer[];
  totalReferrals: number;
  totalRewardsIssued: number;
}

export default function ReferralLeaderboardPage() {
  const [data, setData] = useState<LeaderboardData | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    fetchLeaderboard();
  }, []);

  const fetchLeaderboard = async () => {
    try {
      setLoading(true);
      const response = await fetch("/api/admin/referrals/analytics");

      if (response.ok) {
        const statsData = await response.json();
        setData(statsData);
      } else {
        setError("Failed to load leaderboard data");
      }
    } catch (err) {
      setError("Failed to load leaderboard data");
      clientLogger.error("Failed to load leaderboard data", err instanceof Error ? err : undefined);
    } finally {
      setLoading(false);
    }
  };

  const formatCurrency = (amount: number) => {
    return new Intl.NumberFormat("en-US", {
      style: "currency",
      currency: "USD",
    }).format(amount);
  };

  const getRankBadge = (rank: number) => {
    if (rank === 1) {
      return (
        <span className="inline-flex items-center justify-center w-8 h-8 rounded-full bg-yellow-100 dark:bg-yellow-900/30 text-yellow-600 dark:text-yellow-400">
          <Icon name="star" className="w-5 h-5" />
        </span>
      );
    }
    if (rank === 2) {
      return (
        <span className="inline-flex items-center justify-center w-8 h-8 rounded-full bg-gray-100 dark:bg-gray-700 text-gray-600 dark:text-gray-400">
          <Icon name="star" className="w-5 h-5" />
        </span>
      );
    }
    if (rank === 3) {
      return (
        <span className="inline-flex items-center justify-center w-8 h-8 rounded-full bg-orange-100 dark:bg-orange-900/30 text-orange-600 dark:text-orange-400">
          <Icon name="star" className="w-5 h-5" />
        </span>
      );
    }
    return (
      <span className="inline-flex items-center justify-center w-8 h-8 rounded-full bg-gray-50 dark:bg-gray-800 text-gray-500 dark:text-gray-400 font-medium">
        {rank}
      </span>
    );
  };

  if (loading) {
    return (
      <div className="p-6">
        <div className="animate-pulse space-y-4">
          <div className="h-8 bg-gray-200 dark:bg-gray-700 rounded w-1/4"></div>
          <div className="h-96 bg-gray-200 dark:bg-gray-700 rounded"></div>
        </div>
      </div>
    );
  }

  if (error) {
    return (
      <div className="p-6">
        <div className="bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 rounded-lg p-4 text-red-700 dark:text-red-300">
          {error}
        </div>
      </div>
    );
  }

  return (
    <div className="p-6 space-y-6">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-4">
          <Link href="/admin/referrals">
            <Button variant="ghost" size="sm">
              <Icon name="chevron-left" className="w-4 h-4 mr-1" />
              Back
            </Button>
          </Link>
          <div>
            <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">
              Referral Leaderboard
            </h1>
            <p className="text-gray-600 dark:text-gray-400 mt-1">
              Top referrers ranked by total successful referrals
            </p>
          </div>
        </div>
      </div>

      {/* Summary Cards */}
      <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-blue-100 dark:bg-blue-900/30 rounded-lg">
              <Icon name="user" className="w-5 h-5 text-blue-600 dark:text-blue-400" />
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Total Referrers</p>
              <p className="text-xl font-bold dark:text-gray-100">
                {data?.topReferrers?.length || 0}
              </p>
            </div>
          </div>
        </div>

        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-green-100 dark:bg-green-900/30 rounded-lg">
              <Icon name="check" className="w-5 h-5 text-green-600 dark:text-green-400" />
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Total Referrals</p>
              <p className="text-xl font-bold dark:text-gray-100">
                {data?.totalReferrals || 0}
              </p>
            </div>
          </div>
        </div>

        <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700 p-4">
          <div className="flex items-center gap-3">
            <div className="p-2 bg-purple-100 dark:bg-purple-900/30 rounded-lg">
              <Icon name="reload" className="w-5 h-5 text-purple-600 dark:text-purple-400" />
            </div>
            <div>
              <p className="text-sm text-gray-600 dark:text-gray-400">Total Rewards Issued</p>
              <p className="text-xl font-bold dark:text-gray-100">
                {formatCurrency(data?.totalRewardsIssued || 0)}
              </p>
            </div>
          </div>
        </div>
      </div>

      {/* Leaderboard Table */}
      <div className="bg-white dark:bg-gray-800 rounded-lg border border-gray-200 dark:border-gray-700">
        <div className="p-4 border-b border-gray-200 dark:border-gray-700">
          <h2 className="text-lg font-semibold dark:text-gray-100">All Referrers</h2>
        </div>
        <div className="overflow-x-auto">
          {data?.topReferrers && data.topReferrers.length > 0 ? (
            <table className="w-full">
              <thead className="bg-gray-50 dark:bg-gray-700">
                <tr>
                  <th className="px-6 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">
                    Rank
                  </th>
                  <th className="px-6 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">
                    User
                  </th>
                  <th className="px-6 py-3 text-left text-sm font-medium text-gray-600 dark:text-gray-400">
                    Email
                  </th>
                  <th className="px-6 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400">
                    Referrals
                  </th>
                  <th className="px-6 py-3 text-right text-sm font-medium text-gray-600 dark:text-gray-400">
                    Rewards Earned
                  </th>
                </tr>
              </thead>
              <tbody className="divide-y divide-gray-200 dark:divide-gray-700">
                {data.topReferrers.map((referrer, index) => (
                  <tr key={referrer.id} className="hover:bg-gray-50 dark:hover:bg-gray-700/50">
                    <td className="px-6 py-4">
                      {getRankBadge(index + 1)}
                    </td>
                    <td className="px-6 py-4">
                      <span className="font-medium dark:text-gray-100">{referrer.name}</span>
                    </td>
                    <td className="px-6 py-4 text-gray-600 dark:text-gray-400">
                      {referrer.email}
                    </td>
                    <td className="px-6 py-4 text-right">
                      <span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-sm font-medium bg-blue-100 dark:bg-blue-900/30 text-blue-800 dark:text-blue-300">
                        {referrer.referralCount}
                      </span>
                    </td>
                    <td className="px-6 py-4 text-right font-medium text-green-600 dark:text-green-400">
                      {formatCurrency(referrer.rewardsEarned)}
                    </td>
                  </tr>
                ))}
              </tbody>
            </table>
          ) : (
            <div className="p-8 text-center text-gray-500 dark:text-gray-400">
              <Icon name="user" className="w-12 h-12 mx-auto mb-3 opacity-50" />
              <p>No referrers yet</p>
              <p className="text-sm mt-1">
                Referrers will appear here once they start inviting friends
              </p>
            </div>
          )}
        </div>
      </div>
    </div>
  );
}