import { ConfirmationModal, DeleteIcon, IconButton, Placeholder } from "@cv/ui"; import { useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { useActiveSessionsQuery, useDeleteSessionMutation, } from "@/generated/graphql"; import { clearAuthCookies } from "@/utils/cookies"; export const ActiveSessions = () => { const queryClient = useQueryClient(); const { data, isLoading, error } = useActiveSessionsQuery(); const deleteSessionMutation = useDeleteSessionMutation({ onSuccess: () => { queryClient.invalidateQueries({ queryKey: useActiveSessionsQuery.getKey(), }); }, }); const [sessionToDelete, setSessionToDelete] = useState<{ id: string; isCurrent: boolean; } | null>(null); if (isLoading) { return (

Active Sessions

); } if (error) { return (

Active Sessions

); } const sessions = data?.activeSessions ?? []; if (sessions.length === 0) { return (

Active Sessions

); } return (

Active Sessions

{sessions.map((session) => (

{session.deviceName ?? "Unknown Device"}

{session.isCurrentSession && ( Current Session )} {session.deviceType && ( {session.deviceType} )}
{session.city && session.country && (

{session.city}, {session.country}

)}

Last active: {new Date(session.createdAt).toLocaleString()}

Expires: {new Date(session.expiresAt).toLocaleString()}

} label="Delete session" onClick={() => setSessionToDelete({ id: session.id, isCurrent: session.isCurrentSession, }) } />
))}
setSessionToDelete(null)} onConfirm={() => { if (sessionToDelete) { deleteSessionMutation.mutate( { sessionId: sessionToDelete.id }, { onSuccess: async () => { setSessionToDelete(null); if (sessionToDelete.isCurrent) { await clearAuthCookies(); queryClient.clear(); window.location.href = "/auth/login"; } }, }, ); } }} title="Delete Session" message={ sessionToDelete?.isCurrent ? "Are you sure you want to delete your current session? You will be logged out and redirected to the login page." : "Are you sure you want to delete this session? You will be logged out from this device." } confirmText="Delete" cancelText="Cancel" variant="danger" />
); };