'use client' import { useAuth } from '@thedaviddias/auth' import { Badge } from '@thedaviddias/design-system/badge' import { Button } from '@thedaviddias/design-system/button' import { Progress } from '@thedaviddias/design-system/progress' import { logger } from '@thedaviddias/logging' import { Award, Calendar, Github, Star, TrendingUp, Users, Zap } from 'lucide-react' import Link from 'next/link' import { useEffect, useState } from 'react' import { AuthTierIndicator } from '@/components/auth/auth-tier-indicator' import { ProgressiveAuthBanner } from '@/components/auth/progressive-auth-banner' import { Card } from '@/components/ui/card' import { useFavorites } from '@/contexts/favorites-context' import { CommunityAnonymousPanel } from './community-anonymous-panel' interface CommunityStats { totalProjects: number totalMembers: number thisWeekAdditions: number trendingProjects: Array<{ name: string; slug: string; growth: number }> } interface UserStats { favoritesCount: number joinDate?: string contributionsCount?: number profileViews?: number } /** * Renders the community dashboard with stats, engagement, and auth prompts */ export function CommunityDashboard() { const { user } = useAuth() const { favorites } = useFavorites() const [communityStats, setCommunityStats] = useState(null) const [userStats, setUserStats] = useState(null) const [showAuthBanner, setShowAuthBanner] = useState(true) // Fetch community stats useEffect(() => { const controller = new AbortController() fetch('/api/stats/community', { signal: controller.signal }) .then(res => res.json()) .then(setCommunityStats) .catch(err => { if (err instanceof DOMException && err.name === 'AbortError') return logger.error(err instanceof Error ? err : new Error(String(err)), { data: err, tags: { type: 'component', component: 'community-dashboard' } }) }) return () => controller.abort() }, []) // Derive user stats: fetch from API if authenticated, otherwise compute from favorites useEffect(() => { if (!user) { setUserStats({ favoritesCount: favorites.length }) return } const controller = new AbortController() fetch('/api/user/stats', { signal: controller.signal }) .then(res => res.json()) .then(setUserStats) .catch(err => { if (err instanceof DOMException && err.name === 'AbortError') return logger.error(err instanceof Error ? err : new Error(String(err)), { data: err, tags: { type: 'component', component: 'community-dashboard' } }) }) return () => controller.abort() }, [user, favorites.length]) const hasGitHubAuth = user && (user.user_metadata?.github_username || user.user_metadata?.user_name) // Calculate engagement level /** * Calculates the user's engagement level score as a percentage */ const getEngagementLevel = () => { if (!userStats) return 0 let score = 0 score += Math.min(userStats.favoritesCount * 2, 20) // Up to 20 points for favorites if (user) score += 30 // Account creation bonus if (hasGitHubAuth) score += 30 // GitHub connection bonus if (userStats.contributionsCount) score += userStats.contributionsCount * 10 // Contributions return Math.min(score, 100) } const engagementLevel = getEngagementLevel() return (
{/* Auth banner for anonymous users */} {!user && showAuthBanner && ( setShowAuthBanner(false)} /> )}
{/* Community Stats */}

Community Pulse

Total Projects

{communityStats?.totalProjects || '...'}

Community Members

{communityStats?.totalMembers || '...'}

Added This Week

{communityStats?.thisWeekAdditions || '...'}

+ {Math.round( ((communityStats?.thisWeekAdditions || 0) / (communityStats?.totalProjects || 1)) * 100 )} %
{/* Trending Projects */} {communityStats?.trendingProjects && (

Trending This Week

{communityStats.trendingProjects.map((project, index) => (
{index + 1}

{project.name}

View project
+{project.growth}% growth
))}
)}
{/* User Panel */}
{user ? ( <> {/* User stats */}

Your Engagement

Community involvement level

Progress {engagementLevel}%

{userStats?.favoritesCount || 0}

Favorites

{userStats?.contributionsCount || 0}

Contributions

{engagementLevel < 50 && (

{!hasGitHubAuth ? 'Connect GitHub to boost your level!' : 'Keep exploring to level up!'}

{!hasGitHubAuth && ( )}
)}
{/* Auth tier indicator */} ) : ( )} {/* Quick actions */}

Quick Actions

{hasGitHubAuth ? ( ) : ( )}
) }