|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useState } from 'react'; |
| 4 | +import { motion } from 'framer-motion'; |
| 5 | +import { |
| 6 | + Trophy, |
| 7 | + Award, |
| 8 | + Users, |
| 9 | + Target, |
| 10 | + TrendingUp, |
| 11 | + Loader2, |
| 12 | +} from 'lucide-react'; |
| 13 | +import { formatDistanceToNow } from 'date-fns'; |
| 14 | +import { |
| 15 | + PublicEarningsResponse, |
| 16 | + EarningActivity, |
| 17 | + EarningSource, |
| 18 | +} from '@/types/earnings'; |
| 19 | +import { getPublicEarnings } from '@/lib/api/earnings'; |
| 20 | +import EmptyState from '@/components/EmptyState'; |
| 21 | + |
| 22 | +interface PublicEarningsTabProps { |
| 23 | + username: string; |
| 24 | +} |
| 25 | + |
| 26 | +const SOURCE_CONFIG: Record< |
| 27 | + EarningSource, |
| 28 | + { label: string; icon: typeof Trophy; color: string } |
| 29 | +> = { |
| 30 | + hackathons: { label: 'Hackathons', icon: Trophy, color: 'text-amber-400' }, |
| 31 | + grants: { label: 'Grants', icon: Award, color: 'text-green-400' }, |
| 32 | + crowdfunding: { label: 'Crowdfunding', icon: Users, color: 'text-blue-400' }, |
| 33 | + bounties: { label: 'Bounties', icon: Target, color: 'text-purple-400' }, |
| 34 | +}; |
| 35 | + |
| 36 | +const formatCurrency = (amount: number, currency = 'USD'): string => { |
| 37 | + return new Intl.NumberFormat('en-US', { |
| 38 | + style: 'currency', |
| 39 | + currency, |
| 40 | + minimumFractionDigits: 0, |
| 41 | + maximumFractionDigits: 0, |
| 42 | + }).format(amount); |
| 43 | +}; |
| 44 | + |
| 45 | +interface EarningActivityItemProps { |
| 46 | + activity: EarningActivity; |
| 47 | +} |
| 48 | + |
| 49 | +const EarningActivityItem = ({ |
| 50 | + activity, |
| 51 | +}: EarningActivityItemProps): React.ReactElement => { |
| 52 | + const config = SOURCE_CONFIG[activity.source]; |
| 53 | + const Icon = config.icon; |
| 54 | + |
| 55 | + return ( |
| 56 | + <div className='flex items-start gap-4 rounded-lg border border-zinc-800 bg-zinc-900/50 p-4'> |
| 57 | + <div |
| 58 | + className={`flex h-10 w-10 shrink-0 items-center justify-center rounded-lg bg-zinc-800 ${config.color}`} |
| 59 | + > |
| 60 | + <Icon className='h-5 w-5' /> |
| 61 | + </div> |
| 62 | + <div className='min-w-0 flex-1'> |
| 63 | + <p className='truncate text-sm text-white'>{activity.title}</p> |
| 64 | + <p className='mt-1 text-xs text-zinc-500'> |
| 65 | + {formatDistanceToNow(new Date(activity.occurredAt), { |
| 66 | + addSuffix: true, |
| 67 | + })} |
| 68 | + </p> |
| 69 | + </div> |
| 70 | + <div className='shrink-0 text-right'> |
| 71 | + <p className='text-primary text-sm font-bold'> |
| 72 | + {formatCurrency(activity.amount, activity.currency)} |
| 73 | + </p> |
| 74 | + <p className={`text-xs ${config.color}`}>{config.label}</p> |
| 75 | + </div> |
| 76 | + </div> |
| 77 | + ); |
| 78 | +}; |
| 79 | + |
| 80 | +const PublicEarningsTab = ({ |
| 81 | + username, |
| 82 | +}: PublicEarningsTabProps): React.ReactElement => { |
| 83 | + const [earnings, setEarnings] = useState<PublicEarningsResponse | null>(null); |
| 84 | + const [loading, setLoading] = useState(true); |
| 85 | + const [error, setError] = useState<string | null>(null); |
| 86 | + |
| 87 | + useEffect(() => { |
| 88 | + const controller = new AbortController(); |
| 89 | + |
| 90 | + const loadEarnings = async (): Promise<void> => { |
| 91 | + try { |
| 92 | + setLoading(true); |
| 93 | + setError(null); |
| 94 | + const response = await getPublicEarnings({ username }); |
| 95 | + if (!controller.signal.aborted) { |
| 96 | + setEarnings(response.data); |
| 97 | + } |
| 98 | + } catch (err) { |
| 99 | + if (!controller.signal.aborted) { |
| 100 | + setError('Unable to load earnings data'); |
| 101 | + console.error('Failed to load earnings:', err); |
| 102 | + } |
| 103 | + } finally { |
| 104 | + if (!controller.signal.aborted) { |
| 105 | + setLoading(false); |
| 106 | + } |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + loadEarnings(); |
| 111 | + |
| 112 | + return () => { |
| 113 | + controller.abort(); |
| 114 | + }; |
| 115 | + }, [username]); |
| 116 | + |
| 117 | + if (loading) { |
| 118 | + return ( |
| 119 | + <div className='flex items-center justify-center py-12'> |
| 120 | + <Loader2 className='h-8 w-8 animate-spin text-zinc-500' /> |
| 121 | + </div> |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + if (error || !earnings) { |
| 126 | + return ( |
| 127 | + <EmptyState |
| 128 | + type='compact' |
| 129 | + title='No Earnings Data' |
| 130 | + description={error || 'No earnings data available for this user yet.'} |
| 131 | + action={<></>} |
| 132 | + /> |
| 133 | + ); |
| 134 | + } |
| 135 | + |
| 136 | + const breakdown = earnings.breakdown ?? { |
| 137 | + hackathons: 0, |
| 138 | + grants: 0, |
| 139 | + crowdfunding: 0, |
| 140 | + bounties: 0, |
| 141 | + }; |
| 142 | + |
| 143 | + const breakdownItems = Object.entries(breakdown) |
| 144 | + .filter(([, amount]) => amount > 0) |
| 145 | + .sort(([, a], [, b]) => b - a) as [EarningSource, number][]; |
| 146 | + |
| 147 | + return ( |
| 148 | + <div className='space-y-6 rounded-xl border border-zinc-800 bg-zinc-900/30 p-6'> |
| 149 | + <div className='flex items-center gap-3'> |
| 150 | + <div className='flex h-12 w-12 items-center justify-center rounded-xl bg-zinc-800'> |
| 151 | + <TrendingUp className='text-primary h-6 w-6' /> |
| 152 | + </div> |
| 153 | + <div> |
| 154 | + <p className='text-xs text-zinc-500'>Total Earned</p> |
| 155 | + <p className='text-2xl font-bold text-white'> |
| 156 | + {formatCurrency(earnings.summary?.totalEarned ?? 0)} |
| 157 | + </p> |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + |
| 161 | + <div className='grid grid-cols-2 gap-4 lg:grid-cols-4'> |
| 162 | + {breakdownItems.map(([source, amount]) => { |
| 163 | + const config = SOURCE_CONFIG[source]; |
| 164 | + const Icon = config.icon; |
| 165 | + |
| 166 | + return ( |
| 167 | + <div key={source} className='space-y-2'> |
| 168 | + <div className='flex items-center gap-2'> |
| 169 | + <div |
| 170 | + className={`flex h-8 w-8 items-center justify-center rounded-lg bg-zinc-800 ${config.color}`} |
| 171 | + > |
| 172 | + <Icon className='h-4 w-4' /> |
| 173 | + </div> |
| 174 | + <span className='text-sm text-zinc-500'>{config.label}</span> |
| 175 | + </div> |
| 176 | + <p className='text-2xl font-bold text-white'> |
| 177 | + {formatCurrency(amount)} |
| 178 | + </p> |
| 179 | + </div> |
| 180 | + ); |
| 181 | + })} |
| 182 | + </div> |
| 183 | + |
| 184 | + <div className='flex flex-wrap gap-4 text-xs text-zinc-500'> |
| 185 | + {breakdownItems.map(([source]) => { |
| 186 | + const config = SOURCE_CONFIG[source]; |
| 187 | + return ( |
| 188 | + <span key={source} className='flex items-center gap-2'> |
| 189 | + <i |
| 190 | + className={`inline-block h-2 w-2 rounded-full ${config.color.replace('text-', 'bg-')}`} |
| 191 | + /> |
| 192 | + {config.label} |
| 193 | + </span> |
| 194 | + ); |
| 195 | + })} |
| 196 | + </div> |
| 197 | + |
| 198 | + {(earnings.activities?.length ?? 0) > 0 && ( |
| 199 | + <div className='space-y-4 border-t border-zinc-800 pt-6'> |
| 200 | + <h3 className='text-xs font-semibold uppercase tracking-wider text-zinc-500'> |
| 201 | + Verified Activity |
| 202 | + </h3> |
| 203 | + <div className='space-y-3'> |
| 204 | + {(earnings.activities ?? []).map((activity, index) => ( |
| 205 | + <motion.div |
| 206 | + key={`${activity.source}-${activity.occurredAt}-${index}`} |
| 207 | + initial={{ opacity: 0, y: 10 }} |
| 208 | + animate={{ opacity: 1, y: 0 }} |
| 209 | + > |
| 210 | + <EarningActivityItem activity={activity} /> |
| 211 | + </motion.div> |
| 212 | + ))} |
| 213 | + </div> |
| 214 | + </div> |
| 215 | + )} |
| 216 | + </div> |
| 217 | + ); |
| 218 | +}; |
| 219 | + |
| 220 | +export default PublicEarningsTab; |
0 commit comments