import { useState, useEffect } from 'react'; import { supabase } from '@/integrations/supabase/client'; import { useAuth } from '@/hooks/useAuth'; import { Button } from '@/components/ui/button'; import { Card } from '@/components/ui/card'; import { useToast } from '@/hooks/use-toast'; import { Monitor, Smartphone, Tablet, Trash2 } from 'lucide-react'; import { format } from 'date-fns'; interface UserSession { id: string; device_info: any; last_activity: string; created_at: string; expires_at: string; session_token: string; } export function SessionsTab() { const { user } = useAuth(); const { toast } = useToast(); const [sessions, setSessions] = useState([]); const [loading, setLoading] = useState(true); const fetchSessions = async () => { if (!user) return; const { data, error } = await supabase .from('user_sessions') .select('*') .eq('user_id', user.id) .order('last_activity', { ascending: false }); if (error) { console.error('Error fetching sessions:', error); } else { setSessions(data || []); } setLoading(false); }; useEffect(() => { fetchSessions(); }, [user]); const revokeSession = async (sessionId: string) => { const { error } = await supabase .from('user_sessions') .delete() .eq('id', sessionId); if (error) { toast({ title: 'Error', description: 'Failed to revoke session', variant: 'destructive' }); } else { toast({ title: 'Success', description: 'Session revoked successfully' }); fetchSessions(); } }; const getDeviceIcon = (deviceInfo: any) => { const ua = deviceInfo?.userAgent?.toLowerCase() || ''; if (ua.includes('mobile')) return ; if (ua.includes('tablet')) return ; return ; }; if (loading) { return
Loading sessions...
; } return (

Active Sessions

Manage your active login sessions across devices

{sessions.map((session) => (
{getDeviceIcon(session.device_info)}
{session.device_info?.browser || 'Unknown Browser'}
Last active: {format(new Date(session.last_activity), 'PPpp')}
Expires: {format(new Date(session.expires_at), 'PPpp')}
))} {sessions.length === 0 && ( No active sessions found )}
); }