import React, { useState, useEffect } from 'react'; import { Card } from '../../ui/card'; import { Button } from '../../ui/button'; import { Smartphone, Monitor, Clock } from 'lucide-react'; import { useToast } from '../../../context/ToastContext'; import { sessionService, Session } from '../../../services/sessionService'; import { logger } from '@/utils/logger'; export const SessionManagement: React.FC = () => { const { addToast } = useToast(); const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(true); useEffect(() => { loadSessions(); }, []); const loadSessions = async () => { try { setLoading(true); const res = await sessionService.getSessions(); setSessions(res.sessions); } catch (error) { logger.error('Error loading sessions', { error: error instanceof Error ? error.message : String(error), stack: error instanceof Error ? error.stack : undefined, }); } finally { setLoading(false); } }; const handleRevoke = async (id: string) => { try { await sessionService.revokeSession(id); setSessions((prev) => prev.filter((s) => s.id !== id)); addToast('Session revoked successfully', 'success'); } catch (error) { addToast('Failed to revoke session', 'error'); } }; const handleRevokeAll = async () => { try { await sessionService.logoutAll(); // Ideally reload or clear all except current, but for safety re-fetch loadSessions(); addToast('All other sessions have been logged out', 'success'); } catch (error) { addToast('Failed to log out all devices', 'error'); } }; if (loading) return (
Loading sessions...
); return (

Active Sessions

Manage devices logged into your account.

{sessions.map((session) => { // Simple heuristics for icon since backend might not provide device type explicitly yet const isMobile = session.user_agent.toLowerCase().includes('mobile'); return (
{isMobile ? ( ) : ( )}

{session.ip_address}

{session.is_current && ( CURRENT DEVICE )}

{session.user_agent}

Active:{' '} {new Date(session.last_activity).toLocaleString()}
{!session.is_current && ( )}
); })} {sessions.length === 0 && (

No active sessions found.

)}
); };