| a011f1e | | | 1 | "use client"; |
| a011f1e | | | 2 | |
| a011f1e | | | 3 | import { useState, useEffect } from "react"; |
| a011f1e | | | 4 | |
| a011f1e | | | 5 | function computeTimeAgo(dateStr: string): string { |
| a011f1e | | | 6 | // SQLite datetime('now') returns UTC without Z suffix — append Z if missing |
| a011f1e | | | 7 | const normalized = dateStr.endsWith("Z") || dateStr.includes("+") || dateStr.includes("T") |
| a011f1e | | | 8 | ? dateStr |
| a011f1e | | | 9 | : dateStr + "Z"; |
| a011f1e | | | 10 | const seconds = Math.floor((Date.now() - new Date(normalized).getTime()) / 1000); |
| a011f1e | | | 11 | if (seconds < 60) return "just now"; |
| a011f1e | | | 12 | if (seconds < 3600) return `${Math.floor(seconds / 60)}m ago`; |
| a011f1e | | | 13 | if (seconds < 86400) return `${Math.floor(seconds / 3600)}h ago`; |
| a011f1e | | | 14 | if (seconds < 2592000) return `${Math.floor(seconds / 86400)}d ago`; |
| a011f1e | | | 15 | return new Date(normalized).toLocaleDateString(); |
| a011f1e | | | 16 | } |
| a011f1e | | | 17 | |
| a011f1e | | | 18 | export function TimeAgo({ date }: { date: string }) { |
| a011f1e | | | 19 | const [text, setText] = useState(() => computeTimeAgo(date)); |
| a011f1e | | | 20 | |
| a011f1e | | | 21 | useEffect(() => { |
| a011f1e | | | 22 | // Re-compute immediately on mount (fixes SSR mismatch) |
| a011f1e | | | 23 | setText(computeTimeAgo(date)); |
| a011f1e | | | 24 | const interval = setInterval(() => { |
| a011f1e | | | 25 | setText(computeTimeAgo(date)); |
| a011f1e | | | 26 | }, 10_000); |
| a011f1e | | | 27 | return () => clearInterval(interval); |
| a011f1e | | | 28 | }, [date]); |
| a011f1e | | | 29 | |
| fe3b509 | | | 30 | return <span suppressHydrationWarning>{text}</span>; |
| a011f1e | | | 31 | } |