'use client' import { useEffect, useState } from 'react' import { useRouter } from 'next/navigation' import { authFetch, clearToken, getApiBase, getToken } from '../lib/auth' type ProfileInfo = { username: string role: string auth_provider: string } type ProfileStats = { total: number ready: number pending: number in_progress: number declined: number working: number partial: number approved: number last_request_at?: string | null share: number global_total: number most_active_user?: { username: string; total: number } | null } type ActivityEntry = { ip: string user_agent: string first_seen_at: string last_seen_at: string hit_count: number } type ProfileActivity = { last_ip?: string | null last_user_agent?: string | null last_seen_at?: string | null device_count: number recent: ActivityEntry[] } type ProfileResponse = { user: ProfileInfo stats: ProfileStats activity: ProfileActivity } const formatDate = (value?: string | null) => { if (!value) return 'Never' const date = new Date(value) if (Number.isNaN(date.valueOf())) return value return date.toLocaleString() } const parseBrowser = (agent?: string | null) => { if (!agent) return 'Unknown' const value = agent.toLowerCase() if (value.includes('edg/')) return 'Edge' if (value.includes('chrome/') && !value.includes('edg/')) return 'Chrome' if (value.includes('firefox/')) return 'Firefox' if (value.includes('safari/') && !value.includes('chrome/')) return 'Safari' return 'Unknown' } export default function ProfilePage() { const router = useRouter() const [profile, setProfile] = useState(null) const [stats, setStats] = useState(null) const [activity, setActivity] = useState(null) const [currentPassword, setCurrentPassword] = useState('') const [newPassword, setNewPassword] = useState('') const [status, setStatus] = useState(null) const [loading, setLoading] = useState(true) useEffect(() => { if (!getToken()) { router.push('/login') return } const load = async () => { try { const baseUrl = getApiBase() const response = await authFetch(`${baseUrl}/auth/profile`) if (!response.ok) { clearToken() router.push('/login') return } const data = await response.json() const user = data?.user ?? {} setProfile({ username: user?.username ?? 'Unknown', role: user?.role ?? 'user', auth_provider: user?.auth_provider ?? 'local', }) setStats(data?.stats ?? null) setActivity(data?.activity ?? null) } catch (err) { console.error(err) setStatus('Could not load your profile.') } finally { setLoading(false) } } void load() }, [router]) const submit = async (event: React.FormEvent) => { event.preventDefault() setStatus(null) if (!currentPassword || !newPassword) { setStatus('Enter your current password and a new password.') return } try { const baseUrl = getApiBase() const response = await authFetch(`${baseUrl}/auth/password`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ current_password: currentPassword, new_password: newPassword, }), }) if (!response.ok) { const text = await response.text() throw new Error(text || 'Update failed') } setCurrentPassword('') setNewPassword('') setStatus('Password updated.') } catch (err) { console.error(err) setStatus('Could not update password. Check your current password.') } } if (loading) { return
Loading profile...
} return (

My profile

{profile && (
Signed in as {profile.username} ({profile.role}). Login type:{' '} {profile.auth_provider}.
)}

Account stats

Requests submitted
{stats?.total ?? 0}
Ready to watch
{stats?.ready ?? 0}
In progress
{stats?.in_progress ?? 0}
Pending approval
{stats?.pending ?? 0}
Declined
{stats?.declined ?? 0}
Last request
{formatDate(stats?.last_request_at)}
Share of all requests
{stats?.global_total ? `${Math.round((stats.share || 0) * 1000) / 10}%` : '0%'}
Most active user
{stats?.most_active_user ? `${stats.most_active_user.username} (${stats.most_active_user.total})` : 'N/A'}

Connection history

Last seen {formatDate(activity?.last_seen_at)} from {activity?.last_ip ?? 'Unknown'}.
{(activity?.recent ?? []).map((entry, index) => (
{parseBrowser(entry.user_agent)}
IP: {entry.ip}
Last seen: {formatDate(entry.last_seen_at)}
{entry.hit_count} visits
))} {activity && activity.recent.length === 0 ? (
No connection history yet.
) : null}
{profile?.auth_provider !== 'local' ? (
Password changes are only available for local Magent accounts.
) : (
{status &&
{status}
}
)}
) }