/** Formatting helpers shared across screens (i18n-aware where it matters). */ import type { TFunction } from 'i18next'; /** * Relative timestamp: "just now" / "5m ago" / "3d ago" / absolute date past a week. * Avoids Intl.DateTimeFormat (not guaranteed on Hermes) for the common cases. */ export function formatRelativeTime(iso: string | null | undefined, t: TFunction): string { if (!iso) return t('common.never'); const then = new Date(iso).getTime(); if (Number.isNaN(then)) return t('common.unknown'); const diffMs = Date.now() - then; if (diffMs < 60_000) return t('time.justNow'); const minutes = Math.floor(diffMs / 60_000); if (minutes < 60) return t('time.minutesAgo', { count: minutes }); const hours = Math.floor(minutes / 60); if (hours < 24) return t('time.hoursAgo', { count: hours }); const days = Math.floor(hours / 24); if (days < 7) return t('time.daysAgo', { count: days }); const weeks = Math.floor(days / 7); if (days < 30) return t('time.weeksAgo', { count: weeks }); return iso.slice(0, 10); } /** Absolute date + time, ISO-based so it works without Intl. */ export function formatDateTime(iso: string | null | undefined): string { if (!iso) return '—'; const date = new Date(iso); if (Number.isNaN(date.getTime())) return '—'; const pad = (n: number) => String(n).padStart(2, '0'); return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad( date.getHours(), )}:${pad(date.getMinutes())}`; } /** Human file size: 512 B, 4.2 KB, 13.1 MB … */ export function formatBytes(bytes: number | null | undefined): string { if (bytes === null || bytes === undefined || Number.isNaN(bytes)) return '—'; if (bytes < 1024) return `${bytes} B`; const units = ['KB', 'MB', 'GB', 'TB']; let value = bytes; let unitIndex = -1; do { value /= 1024; unitIndex += 1; } while (value >= 1024 && unitIndex < units.length - 1); return `${value.toFixed(value >= 100 ? 0 : 1)} ${units[unitIndex]}`; } /** Compact count for badges/meta: 999, 1.2k, 34k, 1.1m */ export function formatCount(value: number | null | undefined): string { if (value === null || value === undefined || Number.isNaN(value)) return '0'; if (value < 1000) return String(value); if (value < 1_000_000) { const k = value / 1000; return `${k >= 100 ? Math.round(k) : k.toFixed(1)}k`; } const m = value / 1_000_000; return `${m >= 100 ? Math.round(m) : m.toFixed(1)}m`; } /** Short commit SHA (7 chars when long enough). */ export function shortSha(sha: string | undefined): string { if (!sha) return ''; return sha.length > 8 ? sha.slice(0, 7) : sha; } /** First line of a commit message. */ export function firstLine(message: string | undefined): string { if (!message) return ''; const line = message.split('\n', 1)[0]; return line.trim(); }