import React from "react"; import { Button } from "@/components/ui/button.tsx"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "@/components/ui/table.tsx"; import { Monitor, Smartphone, Globe, Trash2 } from "lucide-react"; import { useTranslation } from "react-i18next"; import { toast } from "sonner"; import { useConfirmation } from "@/hooks/use-confirmation.ts"; import { getCookie, revokeSession, revokeAllUserSessions, } from "@/ui/main-axios.ts"; interface Session { id: string; userId: string; username?: string; deviceType: string; deviceInfo: string; createdAt: string; expiresAt: string; lastActiveAt: string; jwtToken: string; isRevoked?: boolean; } interface SessionManagementTabProps { sessions: Session[]; sessionsLoading: boolean; fetchSessions: () => void; } export function SessionManagementTab({ sessions, sessionsLoading, fetchSessions, }: SessionManagementTabProps): React.ReactElement { const { t } = useTranslation(); const { confirmWithToast } = useConfirmation(); const handleRevokeSession = async (sessionId: string) => { const currentJWT = getCookie("jwt"); const currentSession = sessions.find((s) => s.jwtToken === currentJWT); const isCurrentSession = currentSession?.id === sessionId; confirmWithToast( t("admin.confirmRevokeSession"), async () => { try { await revokeSession(sessionId); toast.success(t("admin.sessionRevokedSuccessfully")); if (isCurrentSession) { setTimeout(() => { window.location.reload(); }, 1000); } else { fetchSessions(); } } catch { toast.error(t("admin.failedToRevokeSession")); } }, "destructive", ); }; const handleRevokeAllUserSessions = async (userId: string) => { confirmWithToast( t("admin.confirmRevokeAllSessions"), async () => { try { const data = await revokeAllUserSessions(userId); toast.success(data.message || t("admin.sessionsRevokedSuccessfully")); fetchSessions(); } catch { toast.error(t("admin.failedToRevokeSessions")); } }, "destructive", ); }; const formatDate = (date: Date) => date.toLocaleDateString() + " " + date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", }); return (