import type { SessionRecord } from "../../db"; import { escapeHtml } from "./html"; type SessionsMessage = { text: string; type?: "success" | "error"; }; function summarizeUserAgent(userAgent: string | null): string { if (!userAgent) return "Unknown"; return userAgent.length > 80 ? `${userAgent.slice(0, 77)}...` : userAgent; } function renderMessage(message?: SessionsMessage): string { if (!message) return ""; const classes = message.type === "error" ? "text-rose-200 border-rose-700 bg-rose-900/30" : "text-emerald-200 border-emerald-700 bg-emerald-900/30"; return `

${escapeHtml(message.text)}

`; } function renderSessionRow( session: SessionRecord, currentTokenHash: string | null, now: number, ): string { const current = session.tokenHash === currentTokenHash; const expired = session.expiresAt <= now; const canRevoke = !session.revoked && !expired; const status = session.revoked ? 'Revoked' : expired ? 'Expired' : 'Active'; const currentBadge = current ? ' Current' : ""; const confirmation = current ? "This will revoke your current session and log you out. Continue?" : "Revoke this session?"; return ` ${escapeHtml(session.ip || "—")} ${escapeHtml(summarizeUserAgent(session.userAgent))} ${status}${currentBadge} ${canRevoke ? `
` : ""} `; } export function renderSessionsFrame( sessions: SessionRecord[], currentTokenHash: string | null, message?: SessionsMessage, ): string { const now = Math.floor(Date.now() / 1000); const rows = sessions.length > 0 ? sessions.map((session) => renderSessionRow(session, currentTokenHash, now)).join("") : 'No sessions found'; return `

Active Sessions

View and manage your active login sessions.

Refresh
${rows}
IP Browser / Client Created Last Active Status Action
${renderMessage(message)}
`; }