| 6 | } |
| 7 | |
| 8 | export function formatDate(timestamp: number): string { |
| 9 | const date = new Date(timestamp); |
| 10 | const now = new Date(); |
| 11 | const diffMs = now.getTime() - date.getTime(); |
| 12 | const diffMins = Math.floor(diffMs / 60000); |
| 13 | const diffHours = Math.floor(diffMs / 3600000); |
| 14 | const diffDays = Math.floor(diffMs / 86400000); |
| 15 | |
| 16 | if (diffMins < 1) return "just now"; |
| 17 | if (diffMins < 60) return `${diffMins}m ago`; |
| 18 | if (diffHours < 24) return `${diffHours}h ago`; |
| 19 | if (diffDays < 7) return `${diffDays}d ago`; |
| 20 | |
| 21 | return date.toLocaleDateString("en-US", { |
| 22 | month: "short", |
| 23 | day: "numeric", |
| 24 | year: date.getFullYear() !== now.getFullYear() ? "numeric" : undefined, |
| 25 | }); |
| 26 | } |
| 27 | |
| 28 | export function truncate(str: string, maxLength: number): string { |
| 29 | if (str.length <= maxLength) return str; |