'use client' import { useEffect, useMemo, 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 invite_management_enabled?: boolean } 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 } type ProfileTab = 'overview' | 'activity' | 'security' const normalizeProfileTab = (value?: string | null): ProfileTab => { if (value === 'activity' || value === 'security') { return value } return 'overview' } 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 [activeTab, setActiveTab] = useState('overview') const [loading, setLoading] = useState(true) const inviteLink = useMemo(() => '/profile/invites', []) useEffect(() => { if (typeof window === 'undefined') return const syncTabFromLocation = () => { const params = new URLSearchParams(window.location.search) setActiveTab(normalizeProfileTab(params.get('tab'))) } syncTabFromLocation() window.addEventListener('popstate', syncTabFromLocation) return () => window.removeEventListener('popstate', syncTabFromLocation) }, []) const selectTab = (tab: ProfileTab) => { setActiveTab(tab) router.replace(tab === 'overview' ? '/profile' : `/profile?tab=${tab}`) } useEffect(() => { if (!getToken()) { router.push('/login') return } const load = async () => { try { const baseUrl = getApiBase() const profileResponse = await authFetch(`${baseUrl}/auth/profile`) if (!profileResponse.ok) { clearToken() router.push('/login') return } const data = (await profileResponse.json()) as ProfileResponse const user = data?.user ?? {} setProfile({ username: user?.username ?? 'Unknown', role: user?.role ?? 'user', auth_provider: user?.auth_provider ?? 'local', invite_management_enabled: Boolean(user?.invite_management_enabled ?? false), }) 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) { let detail = 'Update failed' try { const payload = await response.json() if (typeof payload?.detail === 'string' && payload.detail.trim()) { detail = payload.detail } } catch { const text = await response.text().catch(() => '') if (text?.trim()) detail = text } throw new Error(detail) } const data = await response.json().catch(() => ({})) setCurrentPassword('') setNewPassword('') setStatus( data?.provider === 'jellyfin' ? 'Password updated in Jellyfin (and Magent cache).' : 'Password updated.' ) } catch (err) { console.error(err) if (err instanceof Error && err.message) { setStatus(`Could not update password. ${err.message}`) } else { setStatus('Could not update password. Check your current password.') } } } const authProvider = profile?.auth_provider ?? 'local' const canManageInvites = profile?.role === 'admin' || Boolean(profile?.invite_management_enabled) const canChangePassword = authProvider === 'local' || authProvider === 'jellyfin' const securityHelpText = authProvider === 'jellyfin' ? 'Changing your password here updates your Jellyfin account and refreshes Magent’s cached sign-in.' : authProvider === 'local' ? 'Change your Magent account password.' : 'Password changes are not available for this sign-in provider.' if (loading) { return
Loading profile...
} return (

My profile

Review your account, activity, and security settings.

{canManageInvites ? (
) : null}
{profile && (
Signed in as {profile.username} ({profile.role}). Login type:{' '} {profile.auth_provider}.
)}
{canManageInvites ? ( ) : null}
{activeTab === 'overview' && (
{canManageInvites ? (

Invite tools

Create invite links, send them by email, and track who you have invited from a dedicated page.

) : null}

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}
Working
{stats?.working ?? 0}
Partial
{stats?.partial ?? 0}
Approved
{stats?.approved ?? 0}
Last request
{formatDate(stats?.last_request_at)}
Share of all requests
{stats?.global_total ? `${Math.round((stats.share || 0) * 1000) / 10}%` : '0%'}
Total requests (global)
{stats?.global_total ?? 0}
{profile?.role === 'admin' ? (
Most active user
{stats?.most_active_user ? `${stats.most_active_user.username} (${stats.most_active_user.total})` : 'N/A'}
) : null}
)} {activeTab === 'activity' && (

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}
First seen: {formatDate(entry.first_seen_at)}
Last seen: {formatDate(entry.last_seen_at)}
{entry.hit_count} visits
))} {activity && activity.recent.length === 0 ? (
No connection history yet.
) : null}
)} {activeTab === 'security' && (

Security

{securityHelpText}
{canChangePassword ? (
{status &&
{status}
}
) : (
Password changes are not available for {authProvider} sign-in accounts from Magent.
)}
)}
) }