import { useEffect, useState } from "react"; import TimeAgo from "../components/TimeAgo"; import { api, type SessionSummary } from "../api/client"; import { formatDateTime } from "../utils/date"; import { useSortable } from "../hooks/useSortable"; import SortableTh from "../components/SortableTh"; import { downloadCsv } from "../utils/csv"; import { confirmDialog } from "../utils/dialogs"; import { ErrorNotice } from "../components/Notice"; import { SearchBox } from "../components/TableToolbar"; import { useTableSearch } from "../hooks/useTableSearch"; import { EmptyRow, LoadingRow } from "../components/EmptyState"; function friendlyUserAgent(ua: string | null): string { if (!ua) return "—"; const os = /Windows/.test(ua) ? "Windows" : /Mac OS X/.test(ua) ? "macOS" : /Android/.test(ua) ? "Android" : /iPhone|iPad/.test(ua) ? "iOS" : /Linux/.test(ua) ? "Linux" : "unknown OS"; const browser = /Edg\//.test(ua) ? "Edge" : /Chrome\//.test(ua) ? "Chrome" : /Firefox\//.test(ua) ? "Firefox" : /Safari\//.test(ua) ? "Safari" : "unknown browser"; return `${browser} on ${os}`; } export default function Sessions() { const [sessions, setSessions] = useState(null); const [currentSessionId, setCurrentSessionId] = useState(null); const [error, setError] = useState(null); const [revokingId, setRevokingId] = useState(null); function load() { api.sessions .list() .then((res) => { setSessions(res.sessions); setCurrentSessionId(res.currentSessionId); }) .catch((err) => setError(String(err))); } useEffect(load, []); async function revoke(s: SessionSummary) { const isSelf = s.id === currentSessionId; const label = s.name ?? s.email ?? s.sub; const confirmMsg = isSelf ? `This is your current session — revoking it will sign you out immediately. Continue?` : `Revoke ${label}'s session? They'll be signed out immediately.`; if (!(await confirmDialog({ title: isSelf ? "Sign out of this session" : "End session", message: confirmMsg, confirmLabel: isSelf ? "Sign me out" : "Revoke session", danger: true }))) return; setError(null); setRevokingId(s.id); try { await api.sessions.revoke(s.id); if (isSelf) { window.location.href = "/"; return; } load(); } catch (err) { setError(err instanceof Error ? err.message : String(err)); } finally { setRevokingId(null); } } const tableSearch = useTableSearch(sessions); const { sorted, sortKey, sortDir, requestSort } = useSortable(tableSearch.filtered, undefined, "asc", { url: true }); function exportCsv() { if (!sorted) return; downloadCsv( "sessions.csv", ["Name", "Email", "Role", "IP", "Device", "Last active", "Expires"], sorted.map((s) => [ s.name ?? "", s.email ?? "", s.role ?? "", s.ip ?? "", friendlyUserAgent(s.userAgent), formatDateTime(new Date(s.lastAccess)), s.expiresAt ? formatDateTime(new Date(s.expiresAt)) : "", ]), ); } return ( <>

Sessions

Everyone currently signed in, across every device.
label="Name" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Email" sortKeyName="email" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Role" sortKeyName="role" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="IP" sortKeyName="ip" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Last active" sortKeyName="lastAccess" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> label="Expires" sortKeyName="expiresAt" activeKey={sortKey} direction={sortDir} onSort={requestSort} /> {sorted?.map((s) => ( ))} {sessions === null && } {sorted?.length === 0 && ( tableSearch.setQuery("") : undefined} /> )}
Device Actions
{s.name ?? "—"} {s.id === currentSessionId && This device} {s.email ?? "—"} {s.role ?? unknown user} {s.ip ?? "—"} {friendlyUserAgent(s.userAgent)} {s.expiresAt ? formatDateTime(new Date(s.expiresAt)) : "—"}
); }