import React, { useEffect, useState } from "react"; import { Copy, TrendingUp, Users, DollarSign, Clock, CheckCircle2, } from "lucide-react"; import { useUserStore } from "@/stores/userStore"; import { useAffiliateStore } from "@/stores/affiliateStore"; type MetricsCardProps = { title: string; value: string | number; subtitle?: string; icon: React.ElementType; trend?: string; bgColor: string; }; const MetricsCard = ({ title, value, subtitle, icon: Icon, trend, bgColor, }: MetricsCardProps) => (
{trend && ( {trend} )}

{title}

{value}

{subtitle && (

{subtitle}

)}
); const Overview = () => { const [copied, setCopied] = useState(false); const { user, fetchUser, isLoading: userLoading } = useUserStore(); const { data: affiliateData, fetchEarnings, isLoading: earningsLoading, } = useAffiliateStore(); useEffect(() => { // Get user ID from localStorage const userStr = localStorage.getItem("aff-user"); if (userStr) { const userData = JSON.parse(userStr); if (userData.id) { fetchUser(userData.id); fetchEarnings(userData.id); } } }, [fetchUser, fetchEarnings]); const referralLink = `https://dashboard.planpostai.com/sign-up?ref=${user?.referral_code}`; const handleCopy = () => { navigator.clipboard.writeText(referralLink); setCopied(true); setTimeout(() => setCopied(false), 2000); }; // Calculate metrics from affiliate data const totalEarnings = affiliateData?.summary.total_earning || 0; const totalWithdraw = affiliateData?.summary.total_withdraw || 0; const pendingAmount = affiliateData?.summary.pending_amount || 0; const totalReferrals = affiliateData?.total || 0; // Calculate average commission const averageCommission = totalReferrals > 0 ? totalEarnings / totalReferrals : 0; const isLoading = userLoading || earningsLoading; if (isLoading) { return (
Loading...
); } return (
{/* Header */}

Dashboard Overview

Track your affiliate performance and earnings

{/* Metrics Grid */}
{/* Referral Link Section */}

Your Referral Link

Share this link with your network to start earning commissions

{/* Quick Stats */}

Average Commission

${averageCommission.toFixed(2)}

Per referral

Available Balance

${(totalEarnings - totalWithdraw).toFixed(2)}

Ready to withdraw

Latest Transaction

${affiliateData?.history[0]?.amount.toFixed(2) || "0.00"}

{affiliateData?.history[0] ? new Date( affiliateData.history[0].created_at ).toLocaleDateString() : "No transactions yet"}

); }; export default Overview;