import { useEffect, useState } from 'react'; import { Navigate } from 'react-router-dom'; import { api, formatCredits } from '../api'; import { useAuth } from '../AuthContext'; export default function ProfilePage() { const { user, loading, refresh } = useAuth(); const [profile, setProfile] = useState(null); const [bio, setBio] = useState(''); const [currentPassword, setCurrentPassword] = useState(''); const [newPassword, setNewPassword] = useState(''); const [message, setMessage] = useState(''); const [error, setError] = useState(''); const [busy, setBusy] = useState(false); useEffect(() => { if (!user || user.role === 'admin') return; let cancelled = false; (async () => { try { const data = await api.profile(); if (!cancelled) { setProfile(data.profile); setBio(data.user.bio || ''); } } catch (err) { if (!cancelled) setError(err.message); } })(); return () => { cancelled = true; }; }, [user]); const saveBio = async (e) => { e.preventDefault(); setBusy(true); setError(''); setMessage(''); try { await api.updateProfile({ bio }); await refresh(); setMessage('Profile updated.'); } catch (err) { setError(err.message); } finally { setBusy(false); } }; const changePassword = async (e) => { e.preventDefault(); setBusy(true); setError(''); setMessage(''); try { await api.changePassword(currentPassword, newPassword); setCurrentPassword(''); setNewPassword(''); setMessage('Password changed.'); } catch (err) { setError(err.message); } finally { setBusy(false); } }; const onAvatar = async (e) => { const file = e.target.files?.[0]; if (!file) return; setBusy(true); setError(''); setMessage(''); try { await api.uploadAvatar(file); await refresh(); setMessage('Avatar updated.'); } catch (err) { setError(err.message); } finally { setBusy(false); e.target.value = ''; } }; if (loading) return

Loading…

; if (!user) return ; if (user.role === 'admin') return ; return (

Profile

@{user.username}

{error &&
{error}
} {message &&
{message}
}
{user.avatarUrl ? ( ) : ( {user.username.slice(0, 2).toUpperCase()} )}
Balance {formatCredits(user.balance)} cr

Stats

{profile ? (
  • Joined{' '} {new Date(profile.createdAt).toLocaleDateString()}
  • Inventory{' '} {profile.inventoryCount} items · {formatCredits(profile.inventoryValue)} cr
  • Cases opened {profile.casesOpened}
  • Jackpot wins {profile.jackpotWins}
  • Duel wins {profile.duelWins}
) : (

Loading stats…

)}

Bio