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 | 1x 1x 1x 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 { cn } from "@/lib/core";
import type { LoyaltyBalance } from "@/lib/promotions/types";
interface PointsBalanceProps {
userId?: string;
showTier?: boolean;
showProgress?: boolean;
variant?: "card" | "compact" | "inline";
className?: string;
}
export function PointsBalance({
showTier = true,
showProgress = true,
variant = "card",
className}: PointsBalanceProps) {
const [balance, setBalance] = useState<LoyaltyBalance | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
async function fetchBalance() {
try {
const response = await fetch("/api/loyalty/balance");
const data = await response.json();
if (data.success) {
setBalance(data.data);
} else {
setError(data.error || "Failed to load points balance");
}
} catch {
setError("Failed to load points balance");
} finally {
setLoading(false);
}
}
fetchBalance();
}, []);
if (loading) {
return (
<div className={cn("animate-pulse", className)}>
<div className="h-20 bg-gray-200 rounded-lg" />
</div>
);
}
if (error || !balance) {
return null;
}
const formatPoints = (points: number) => {
return new Intl.NumberFormat("en-US").format(points);
};
const tierProgress = balance.currentTier && balance.nextTier && balance.pointsToNextTier
? ((balance.totalPoints - (balance.currentTier.minPoints || 0)) /
((balance.nextTier.minPoints || 0) - (balance.currentTier.minPoints || 0))) * 100
: 0;
if (variant === "inline") {
return (
<div className={cn("flex items-center gap-2", className)}>
<Icon name="star" size={16} className="text-yellow-500" />
<span className="font-semibold">{formatPoints(balance.totalPoints)}</span>
<span className="text-gray-500 text-sm">points</span>
</div>
);
}
if (variant === "compact") {
return (
<div className={cn("flex items-center justify-between p-3 bg-gradient-to-r from-yellow-50 to-orange-50 border border-yellow-200 rounded-lg", className)}>
<div className="flex items-center gap-2">
<div className="p-2 bg-yellow-100 rounded-full">
<Icon name="star" size={18} className="text-yellow-600" />
</div>
<div>
<p className="text-sm text-gray-600">Your Points</p>
<p className="text-lg font-bold text-gray-900">
{formatPoints(balance.totalPoints)}
</p>
</div>
</div>
{showTier && balance.currentTier && (
<div className="text-right">
<span className="text-sm font-medium text-yellow-700">
{balance.currentTier.name}
</span>
</div>
)}
</div>
);
}
// Card variant
return (
<div className={cn("p-4 bg-gradient-to-br from-yellow-50 via-orange-50 to-yellow-100 border border-yellow-200 rounded-xl", className)}>
<div className="flex items-start justify-between mb-4">
<div>
<p className="text-sm text-gray-600 mb-1">Your Loyalty Points</p>
<p className="text-3xl font-bold text-gray-900">
{formatPoints(balance.totalPoints)}
</p>
</div>
<div className="p-3 bg-yellow-100 rounded-full">
<Icon name="star" size={24} className="text-yellow-600" />
</div>
</div>
{showTier && balance.currentTier && (
<div className="mb-4">
<div className="flex items-center justify-between mb-1">
<span className="text-sm font-medium text-yellow-800">
{balance.currentTier.name} Member
</span>
{balance.nextTier && (
<span className="text-xs text-gray-500">
{formatPoints(balance.pointsToNextTier || 0)} pts to {balance.nextTier.name}
</span>
)}
</div>
{showProgress && balance.nextTier && (
<div className="w-full bg-yellow-200 rounded-full h-2 overflow-hidden">
<div
className="bg-gradient-to-r from-yellow-400 to-orange-500 h-full rounded-full transition-all duration-300"
style={{ width: `${Math.min(100, tierProgress)}%` }}
/>
</div>
)}
</div>
)}
<div className="flex items-center justify-between text-sm">
<span className="text-gray-600">Lifetime Points</span>
<span className="font-medium text-gray-700">
{formatPoints(balance.lifetimePoints)}
</span>
</div>
</div>
);
}
export default PointsBalance;
|