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 | "use client"; import { useState, useEffect } from "react"; import { Icon } from "@/components/ui/icons"; import { IconName } from "@/components/ui/icons/registry"; interface ReportsData { overview: { totalReferrals: number; signedUp: number; purchased: number; rewarded: number; conversionRate: string; rewardsGiven: number }; topReferrers: { userId: number; name: string; email: string; referrals: number }[]; monthlyTrends: { month: string; created: number; converted: number }[]; campaignPerformance: { id: number; name: string; isActive: boolean; totalReferrals: number; conversions: number; conversionRate: string }[]; } export default function ReferralReports() { const [data, setData] = useState<ReportsData | null>(null); const [loading, setLoading] = useState(true); const [error, setError] = useState<string | null>(null); useEffect(() => { fetchReports(); }, []); const fetchReports = async () => { setLoading(true); try { const res = await fetch("/api/admin/referrals/reports"); const result = await res.json(); if (!res.ok || result.success === false) throw new Error(result.error || "Failed to fetch reports"); setData(result.data); } catch (err) { setError(err instanceof Error ? err.message : "Failed to fetch reports"); } finally { setLoading(false); } }; if (loading) return <div className="flex items-center justify-center h-64"><p className="text-gray-500">Loading reports...</p></div>; if (error) return <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-400">{error}</div>; if (!data) return null; return ( <div className="space-y-6"> <div className="flex items-center justify-between"> <div> <h1 className="text-2xl font-bold text-gray-900 dark:text-gray-100">Referral Reports</h1> <p className="text-gray-600 dark:text-gray-400 mt-1">Analytics for your referral program</p> </div> <button onClick={fetchReports} className="flex items-center gap-2 px-4 py-2 border border-gray-300 dark:border-gray-600 rounded-lg text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700"> <Icon name="reload" className="w-5 h-5" />Refresh </button> </div> {/* Stats Cards */} <div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4"> {([ { label: "Total Referrals", value: data.overview.totalReferrals, icon: "users" as IconName, bgColor: "bg-blue-100 dark:bg-blue-900/30", iconColor: "text-blue-600 dark:text-blue-400" }, { label: "Conversions", value: data.overview.purchased, icon: "check-circle" as IconName, bgColor: "bg-green-100 dark:bg-green-900/30", iconColor: "text-green-600 dark:text-green-400" }, { label: "Conversion Rate", value: data.overview.conversionRate, icon: "star" as IconName, bgColor: "bg-purple-100 dark:bg-purple-900/30", iconColor: "text-purple-600 dark:text-purple-400" }, { label: "Rewards Given", value: data.overview.rewardsGiven, icon: "tag" as IconName, bgColor: "bg-orange-100 dark:bg-orange-900/30", iconColor: "text-orange-600 dark:text-orange-400" }, ]).map((stat) => ( <div key={stat.label} 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 ${stat.bgColor} rounded-lg`}> <Icon name={stat.icon} className={`w-5 h-5 ${stat.iconColor}`} /> </div> <div> <p className="text-sm text-gray-600 dark:text-gray-400">{stat.label}</p> <p className="text-2xl font-bold text-gray-900 dark:text-gray-100">{typeof stat.value === "number" ? stat.value.toLocaleString() : stat.value}</p> </div> </div> </div> ))} </div> <div className="grid grid-cols-1 lg:grid-cols-2 gap-6"> {/* Top Referrers */} <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 text-gray-900 dark:text-gray-100">Top Referrers</h2> </div> <div className="overflow-x-auto"> <table className="w-full"> <thead className="bg-gray-50 dark:bg-gray-900"> <tr> <th className="px-4 py-2 text-left text-xs font-medium text-gray-600 dark:text-gray-400">Member</th> <th className="px-4 py-2 text-right text-xs font-medium text-gray-600 dark:text-gray-400">Referrals</th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {data.topReferrers.length === 0 ? ( <tr><td colSpan={2} className="px-4 py-4 text-center text-gray-500">No referrers yet</td></tr> ) : data.topReferrers.map((member, idx) => ( <tr key={member.userId}> <td className="px-4 py-2"> <div className="flex items-center gap-2"> <span className="w-5 h-5 flex items-center justify-center text-xs font-bold text-gray-500">{idx + 1}</span> <div> <p className="font-medium text-gray-900 dark:text-gray-100 text-sm">{member.name}</p> <p className="text-xs text-gray-500">{member.email}</p> </div> </div> </td> <td className="px-4 py-2 text-right font-medium text-gray-900 dark:text-gray-100">{member.referrals}</td> </tr> ))} </tbody> </table> </div> </div> {/* Campaign Performance */} <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 text-gray-900 dark:text-gray-100">Campaign Performance</h2> </div> <div className="overflow-x-auto"> <table className="w-full"> <thead className="bg-gray-50 dark:bg-gray-900"> <tr> <th className="px-4 py-2 text-left text-xs font-medium text-gray-600 dark:text-gray-400">Campaign</th> <th className="px-4 py-2 text-right text-xs font-medium text-gray-600 dark:text-gray-400">Referrals</th> <th className="px-4 py-2 text-right text-xs font-medium text-gray-600 dark:text-gray-400">Conv. Rate</th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {data.campaignPerformance.length === 0 ? ( <tr><td colSpan={3} className="px-4 py-4 text-center text-gray-500">No campaigns yet</td></tr> ) : data.campaignPerformance.map((campaign) => ( <tr key={campaign.id}> <td className="px-4 py-2"> <div className="flex items-center gap-2"> <span className={`w-2 h-2 rounded-full ${campaign.isActive ? "bg-green-500" : "bg-gray-400"}`} /> <span className="font-medium text-gray-900 dark:text-gray-100 text-sm">{campaign.name}</span> </div> </td> <td className="px-4 py-2 text-right text-gray-900 dark:text-gray-100">{campaign.totalReferrals}</td> <td className="px-4 py-2 text-right text-gray-600 dark:text-gray-400">{campaign.conversionRate}%</td> </tr> ))} </tbody> </table> </div> </div> </div> {/* Monthly Trends */} <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 text-gray-900 dark:text-gray-100">Monthly Activity</h2> </div> <div className="p-4 overflow-x-auto"> <table className="w-full"> <thead> <tr> <th className="px-4 py-2 text-left text-sm font-medium text-gray-600 dark:text-gray-400">Month</th> <th className="px-4 py-2 text-right text-sm font-medium text-gray-600 dark:text-gray-400">Created</th> <th className="px-4 py-2 text-right text-sm font-medium text-gray-600 dark:text-gray-400">Converted</th> </tr> </thead> <tbody className="divide-y divide-gray-200 dark:divide-gray-700"> {data.monthlyTrends.length === 0 ? ( <tr><td colSpan={3} className="px-4 py-4 text-center text-gray-500">No data available</td></tr> ) : data.monthlyTrends.map((month) => ( <tr key={month.month} className="hover:bg-gray-50 dark:hover:bg-gray-700"> <td className="px-4 py-2 font-medium text-gray-900 dark:text-gray-100"> {new Date(month.month + "-01").toLocaleDateString("en-US", { year: "numeric", month: "long" })} </td> <td className="px-4 py-2 text-right text-gray-900 dark:text-gray-100">{month.created}</td> <td className="px-4 py-2 text-right text-green-600 dark:text-green-400">{month.converted}</td> </tr> ))} </tbody> </table> </div> </div> </div> ); } |