- Inventory: end of support and replacement cost per asset; a Lifecycle page showing what ages out year by year and what replacing it will cost; support ending within 90 days in the weekly digest - Lists: a shared empty state saying what is missing, why, and the one thing to do; "nothing matches" with a clear button; skeleton rows instead of empty-looking lists while loading Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
177 lines
6.6 KiB
TypeScript
177 lines
6.6 KiB
TypeScript
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<SessionSummary[] | null>(null);
|
|
const [currentSessionId, setCurrentSessionId] = useState<string | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [revokingId, setRevokingId] = useState<string | null>(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 (
|
|
<>
|
|
<div className="d-flex align-items-center mb-3">
|
|
<div>
|
|
<h2 className="page-title mb-0">Sessions</h2>
|
|
<div className="text-secondary small mt-1">Everyone currently signed in, across every device.</div>
|
|
</div>
|
|
<button className="btn btn-outline-secondary ms-auto" onClick={exportCsv} disabled={!sorted || sorted.length === 0}>
|
|
Export CSV
|
|
</button>
|
|
</div>
|
|
<ErrorNotice error={error} />
|
|
<div className="card">
|
|
<div className="card-header">
|
|
<SearchBox value={tableSearch.query} onChange={tableSearch.setQuery} />
|
|
</div>
|
|
<div className="table-responsive">
|
|
<table className="table table-vcenter card-table">
|
|
<thead>
|
|
<tr>
|
|
<SortableTh<SessionSummary> label="Name" sortKeyName="name" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<SessionSummary> label="Email" sortKeyName="email" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<SessionSummary> label="Role" sortKeyName="role" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<SortableTh<SessionSummary> label="IP" sortKeyName="ip" activeKey={sortKey} direction={sortDir} onSort={requestSort} />
|
|
<th>Device</th>
|
|
<SortableTh<SessionSummary>
|
|
label="Last active"
|
|
sortKeyName="lastAccess"
|
|
activeKey={sortKey}
|
|
direction={sortDir}
|
|
onSort={requestSort}
|
|
/>
|
|
<SortableTh<SessionSummary>
|
|
label="Expires"
|
|
sortKeyName="expiresAt"
|
|
activeKey={sortKey}
|
|
direction={sortDir}
|
|
onSort={requestSort}
|
|
/>
|
|
<th className="w-1">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{sorted?.map((s) => (
|
|
<tr key={s.id}>
|
|
<td>
|
|
{s.name ?? "—"}
|
|
{s.id === currentSessionId && <span className="badge bg-blue-lt text-blue ms-2">This device</span>}
|
|
</td>
|
|
<td>{s.email ?? "—"}</td>
|
|
<td>{s.role ?? <span className="text-secondary">unknown user</span>}</td>
|
|
<td>{s.ip ?? "—"}</td>
|
|
<td className="text-secondary">{friendlyUserAgent(s.userAgent)}</td>
|
|
<td><TimeAgo value={s.lastAccess} /></td>
|
|
<td>{s.expiresAt ? formatDateTime(new Date(s.expiresAt)) : "—"}</td>
|
|
<td>
|
|
<button
|
|
className="btn btn-sm btn-outline-danger"
|
|
onClick={() => revoke(s)}
|
|
disabled={revokingId === s.id}
|
|
>
|
|
{revokingId === s.id ? "Revoking…" : "Revoke"}
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
))}
|
|
{sessions === null && <LoadingRow colSpan={8} />}
|
|
{sorted?.length === 0 && (
|
|
<EmptyRow colSpan={8} title="No active sessions" hint="Everyone who is signed in appears here, with where from and until when." filtered={tableSearch.active ? () => tableSearch.setQuery("") : undefined} />
|
|
)}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
}
|