'use client' import { useState, useEffect } from 'react' import { useAllauthAPI } from '../../contexts/APIContext' import { SettingsSection, SettingsItem, SettingsList, Badge, Button } from './SettingsComponents' import type { Session } from '../../types' function parseUserAgent(ua: string): string { if (ua.includes('Chrome')) return 'Chrome' if (ua.includes('Firefox')) return 'Firefox' if (ua.includes('Safari')) return 'Safari' if (ua.includes('Edge')) return 'Edge' return 'Unknown Browser' } export function SessionsSection() { const api = useAllauthAPI() const [sessions, setSessions] = useState([]) const [loading, setLoading] = useState(true) const [available, setAvailable] = useState(true) const fetchSessions = async () => { try { const res = await api.session.list() if (res.status === 200 && res.data) { setSessions(res.data as Session[]) } else { // Non-200 status means sessions feature not available setAvailable(false) } } catch { setAvailable(false) } setLoading(false) } useEffect(() => { fetchSessions() }, []) const handleEnd = async (id: number) => { if (!confirm('End this session?')) return await api.session.remove([id]) fetchSessions() } const handleEndAllOthers = async () => { const otherIds = sessions.filter(s => !s.is_current).map(s => s.id) if (otherIds.length === 0) return if (!confirm(`End ${otherIds.length} other session(s)?`)) return await api.session.remove(otherIds) fetchSessions() } if (loading || !available) return null const otherSessions = sessions.filter(s => !s.is_current) return ( {sessions.map(session => ( {parseUserAgent(session.user_agent)} {session.is_current && Current} } meta={`${session.ip} ยท ${session.last_seen_at ? new Date(session.last_seen_at * 1000).toLocaleString() : 'Unknown'}`} actions={ !session.is_current && ( ) } /> ))} {otherSessions.length > 0 && ( )} ) }