import type { ClientIdentity } from '../../shared/api-types'; /** * Format an ISO timestamp relative to `nowMs` (e.g. "just now", "4s ago", "3m ago", "2h ago"). */ export function relativeTime(iso: string | undefined | null, nowMs: number = Date.now()): string { if (!iso) return '—'; const ts = Date.parse(iso); if (Number.isNaN(ts)) return '—'; const diffSec = Math.round((nowMs - ts) / 1000); if (diffSec < 0) return 'just now'; if (diffSec < 2) return 'just now'; if (diffSec < 60) return `${diffSec}s ago`; const diffMin = Math.floor(diffSec / 60); if (diffMin < 60) return `${diffMin}m ago`; const diffHr = Math.floor(diffMin / 60); if (diffHr < 24) return `${diffHr}h ago`; const diffDay = Math.floor(diffHr / 24); return `${diffDay}d ago`; } /** * Format an elapsed duration from `startedIso` to `nowMs` (e.g. "12s", "4m 18s", "2h 10m"). */ export function formatUptime(startedIso: string | undefined | null, nowMs: number = Date.now()): string { if (!startedIso) return '—'; const ts = Date.parse(startedIso); if (Number.isNaN(ts)) return '—'; const totalSec = Math.max(0, Math.floor((nowMs - ts) / 1000)); if (totalSec < 60) return `${totalSec}s`; const mins = Math.floor(totalSec / 60); const secs = totalSec % 60; if (mins < 60) return secs > 0 ? `${mins}m ${secs}s` : `${mins}m`; const hrs = Math.floor(mins / 60); const remMins = mins % 60; return remMins > 0 ? `${hrs}h ${remMins}m` : `${hrs}h`; } /** * Format a compact local wall-clock time (HH:MM:SS). */ export function formatClock(iso: string | undefined | null): string { if (!iso) return '—'; const d = new Date(iso); if (Number.isNaN(d.getTime())) return '—'; return d.toLocaleTimeString(undefined, { hour12: false, hour: '2-digit', minute: '2-digit', second: '2-digit', }); } /** * Shorten an event/stream/tab identifier while keeping it recognizable. */ export function shortId(id: string | undefined | null, maxLen = 14): string { if (!id) return '—'; if (id.length <= maxLen) return id; const keepHead = Math.max(4, Math.ceil((maxLen - 1) * 0.6)); const keepTail = Math.max(3, maxLen - 1 - keepHead); return `${id.slice(0, keepHead)}…${id.slice(-keepTail)}`; } /** * Human-readable byte size (e.g. "42.3 KB", "1.4 MB"). */ export function formatBytes(bytes: number | undefined | null): string { if (bytes == null || !Number.isFinite(bytes) || bytes < 0) return '0 B'; if (bytes < 1024) return `${Math.round(bytes)} B`; const kb = bytes / 1024; if (kb < 1024) return `${kb < 10 ? kb.toFixed(1) : Math.round(kb)} KB`; const mb = kb / 1024; return `${mb.toFixed(1)} MB`; } /** * Compact display for a cursor string (keeps head and trailing seq/suffix). */ export function prettyCursor(cursor: string | undefined | null): string { if (!cursor) return '∅'; if (cursor.length <= 16) return cursor; return `${cursor.slice(0, 7)}…${cursor.slice(-6)}`; } /** * Format duration in milliseconds cleanly (e.g. "15s", "3s", "500ms"). */ export function formatMs(ms: number | undefined | null): string { if (ms == null || !Number.isFinite(ms)) return '—'; if (ms < 1000) return `${Math.round(ms)}ms`; const sec = ms / 1000; return Number.isInteger(sec) ? `${sec}s` : `${sec.toFixed(1)}s`; } /** * Parse a concise browser/OS label from a User-Agent string. */ export function parseBrowser(ua: string | undefined | null): string { if (!ua) return 'Unknown browser'; const browser = /Edg\//.test(ua) ? 'Edge' : /Firefox\//.test(ua) ? 'Firefox' : /Chrome\//.test(ua) && !/Chromium\//.test(ua) ? 'Chrome' : /Safari\//.test(ua) && !/Chrome\//.test(ua) ? 'Safari' : 'Browser'; const os = /Mac OS X|Macintosh/.test(ua) ? 'macOS' : /Windows/.test(ua) ? 'Windows' : /Linux/.test(ua) ? 'Linux' : /iPhone|iPad/.test(ua) ? 'iOS' : /Android/.test(ua) ? 'Android' : ''; return os ? `${browser} · ${os}` : browser; } /** * Display label for a ClientIdentity. */ export function formatClientName(client: ClientIdentity): string { if (client.title) return client.title; if (client.name && client.version) return `${client.name} ${client.version}`; if (client.name) return client.name; if (client.userAgent) return shortId(client.userAgent, 24); if (client.transport === 'stdio') return 'stdio client'; return shortId(client.key, 18); }