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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | "use client";
import { useState, useEffect } from "react";
import { Icon } from "@/components/ui/icons";
import { clientLogger } from '@/lib/logging/clientLogger';
import { cn } from "@/lib/core";
import type { ReferralStats as ReferralStatsType } from "@/lib/promotions/types";
interface ReferralStatsProps {
className?: string;
}
export function ReferralStats({ className }: ReferralStatsProps) {
const [stats, setStats] = useState<ReferralStatsType | null>(null);
const [loading, setLoading] = useState(true);
useEffect(() => {
async function fetchStats() {
try {
const response = await fetch("/api/referrals/stats");
const data = await response.json();
if (data.success) {
setStats(data.data);
}
} catch (error) {
clientLogger.error('Failed to fetch referral stats', error instanceof Error ? error : new Error(String(error)));
} finally {
setLoading(false);
}
}
fetchStats();
}, []);
if (loading) {
return (
<div className={cn("animate-pulse", className)}>
<div className="h-40 bg-gray-200 rounded-lg" />
</div>
);
}
if (!stats) {
return null;
}
const formatNumber = (num: number) => {
return new Intl.NumberFormat("en-US").format(num);
};
const statItems: Array<{
label: string;
value: number;
icon: "users" | "user" | "shopping-bag" | "star";
color: string;
bgColor: string;
}> = [
{
label: "Total Referrals",
value: stats.totalReferrals,
icon: "users",
color: "text-purple-600",
bgColor: "bg-purple-100"},
{
label: "Signed Up",
value: stats.signedUp,
icon: "user",
color: "text-blue-600",
bgColor: "bg-blue-100"},
{
label: "Purchased",
value: stats.purchased,
icon: "shopping-bag",
color: "text-green-600",
bgColor: "bg-green-100"},
{
label: "Rewarded",
value: stats.rewarded,
icon: "star",
color: "text-yellow-600",
bgColor: "bg-yellow-100"},
];
return (
<div className={cn("p-4 bg-white border border-gray-200 rounded-xl", className)}>
<h4 className="font-semibold text-gray-800 mb-4 flex items-center gap-2">
<Icon name="dashboard" size={18} className="text-purple-600" />
Your Referral Stats
</h4>
<div className="grid grid-cols-2 gap-3 mb-4">
{statItems.map((item) => (
<div key={item.label} className="p-3 bg-gray-50 rounded-lg">
<div className="flex items-center gap-2 mb-1">
<div className={cn("p-1 rounded", item.bgColor)}>
<Icon name={item.icon} size={14} className={item.color} />
</div>
<span className="text-xs text-gray-500">{item.label}</span>
</div>
<p className="text-xl font-bold text-gray-800">
{formatNumber(item.value)}
</p>
</div>
))}
</div>
{/* Earnings summary */}
<div className="p-3 bg-gradient-to-r from-purple-50 to-pink-50 rounded-lg border border-purple-100">
<div className="flex justify-between items-center">
<div className="flex items-center gap-2">
<Icon name="star" size={18} className="text-yellow-500" />
<span className="text-sm text-gray-700">Total Earned</span>
</div>
<span className="text-lg font-bold text-purple-700">
{formatNumber(stats.totalEarned)} pts
</span>
</div>
{stats.pendingRewards > 0 && (
<div className="mt-2 pt-2 border-t border-purple-200 flex justify-between items-center">
<span className="text-xs text-gray-500">Pending Rewards</span>
<span className="text-sm font-medium text-orange-600">
{formatNumber(stats.pendingRewards)} pts
</span>
</div>
)}
</div>
{/* Conversion funnel visualization */}
{stats.totalReferrals > 0 && (
<div className="mt-4">
<p className="text-xs text-gray-500 mb-2">Conversion Funnel</p>
<div className="relative h-6 bg-gray-100 rounded-full overflow-hidden">
{/* Signed up */}
<div
className="absolute left-0 top-0 h-full bg-blue-200"
style={{ width: `${(stats.signedUp / stats.totalReferrals) * 100}%` }}
/>
{/* Purchased */}
<div
className="absolute left-0 top-0 h-full bg-green-300"
style={{ width: `${(stats.purchased / stats.totalReferrals) * 100}%` }}
/>
{/* Rewarded */}
<div
className="absolute left-0 top-0 h-full bg-purple-400"
style={{ width: `${(stats.rewarded / stats.totalReferrals) * 100}%` }}
/>
</div>
<div className="flex justify-between mt-1 text-xs text-gray-500">
<span>0%</span>
<span>
{Math.round((stats.purchased / Math.max(stats.totalReferrals, 1)) * 100)}% conversion
</span>
<span>100%</span>
</div>
</div>
)}
</div>
);
}
export default ReferralStats;
|