import { faServer } from "@fortawesome/free-solid-svg-icons"; import { createNotification } from "@app/components/notifications"; import { DeleteActionModal, EmptyState, Table, TableContainer, TableSkeleton, TBody, Td, Th, THead, Tooltip, Tr } from "@app/components/v2"; import { Button } from "@app/components/v2/Button"; import { useGetMySessions, useRevokeMySessionById } from "@app/hooks/api"; import { usePopUp } from "@app/hooks/usePopUp"; import { timeAgo } from "@app/lib/fn/date"; import { formatSessionUserAgent } from "@app/lib/fn/string"; const formatLocalDateTime = (date: Date): string => { return date.toLocaleString(undefined, { weekday: "long", year: "numeric", month: "long", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" }); }; export const SessionsTable = () => { const { data, isPending } = useGetMySessions(); const { mutateAsync: revokeMySessionById } = useRevokeMySessionById(); const { popUp, handlePopUpOpen, handlePopUpClose, handlePopUpToggle } = usePopUp([ "deleteSession" ] as const); const handleSignOut = async (sessionId: string) => { try { await revokeMySessionById(sessionId); createNotification({ text: "Session revoked successfully", type: "success" }); } catch (error) { console.error(error); createNotification({ text: "Failed to revoke session", type: "error" }); } handlePopUpClose("deleteSession"); }; return ( <> handlePopUpToggle("deleteSession", isOpen)} deleteKey="confirm" onDeleteApproved={() => handleSignOut((popUp?.deleteSession?.data as { sessionId: string })?.sessionId) } /> {isPending && } {!isPending && data && data.length > 0 && data.map(({ id, createdAt, lastUsed, ip, userAgent }) => { const { os, browser } = formatSessionUserAgent(userAgent); const lastUsedDate = new Date(lastUsed); const createdAtDate = new Date(createdAt); return ( ); })} {!isPending && data && data?.length === 0 && ( )}
IP & Session ID OS & Browser Last accessed Manage
{ip} ID: {id}
{os} {browser}
{timeAgo(lastUsedDate, new Date())} Created {timeAgo(createdAtDate, new Date())}
); };