import { useEffect, useState } from 'react'; import { useNavigate, Link } from 'react-router'; import { Button, buttonVariants } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Skeleton } from '@/components/ui/skeleton'; import { Separator } from '@/components/ui/separator'; import PageLayout from '@/components/layout/PageLayout'; import { useAuthStore } from '@/stores/auth-store'; import { getUserStats, type UserStats } from '@/api/leaderboards'; import { updateProfile } from '@/api/users'; import { User, Flame, Trophy, BarChart3, LogOut, Pencil, Check, X } from 'lucide-react'; function formatTime(seconds: number) { const m = Math.floor(seconds / 60); const s = seconds % 60; return `${m}:${s.toString().padStart(2, '0')}`; } export default function Profile() { const user = useAuthStore((s) => s.user); const logout = useAuthStore((s) => s.logout); const loadUser = useAuthStore((s) => s.loadUser); const navigate = useNavigate(); const [stats, setStats] = useState(null); const [loading, setLoading] = useState(true); const [editing, setEditing] = useState(false); const [displayName, setDisplayName] = useState(''); useEffect(() => { if (!user) { navigate('/login'); return; } getUserStats() .then(setStats) .catch(() => setStats(null)) .finally(() => setLoading(false)); }, [user, navigate]); const handleLogout = () => { logout(); navigate('/'); }; const handleSaveProfile = async () => { try { await updateProfile({ displayName: displayName || undefined }); await loadUser(); setEditing(false); } catch { // Error handled by API client } }; if (!user) return null; return (
{editing ? (
setDisplayName(e.target.value)} placeholder="Display name" className="h-8 w-40" />
) : (

{user.username}

)}

{user.email}

{loading && (
{Array.from({ length: 4 }).map((_, i) => ( ))}
)} {stats && ( <>
} label="Games Played" value={stats.totalGamesPlayed.toString()} /> } label="Best Score" value={stats.bestScore.toLocaleString()} /> } label="Current Streak" value={`${stats.currentStreak} day${stats.currentStreak !== 1 ? 's' : ''}`} /> } label="Longest Streak" value={`${stats.longestStreak} day${stats.longestStreak !== 1 ? 's' : ''}`} />
Average Score {stats.averageScore.toLocaleString()}
Total Score {stats.totalScore.toLocaleString()}
View Achievements {stats.recentScores.length > 0 && (

Recent Games

Date Score Time Chain
{stats.recentScores.map((entry, i) => (
{new Date(entry.date).toLocaleDateString()} {entry.score.toLocaleString()} {formatTime(entry.timeSeconds)} {entry.chainLength}
))}
)} )} {!loading && !stats && (

No stats yet. Play a daily challenge to get started!

)}
); } function StatCard({ icon, label, value, }: { icon: React.ReactNode; label: string; value: string; }) { return (
{icon} {label}

{value}

); }