import { onMounted, onUnmounted, ref } from 'vue'; /** * Composable for real-time timestamp updates * Updates every second to show live "time ago" timestamps */ export function useRealTimeUpdate() { const currentTime = ref(Date.now()); let intervalId: NodeJS.Timeout | null = null; const updateTime = () => { currentTime.value = Date.now(); }; onMounted(() => { // Update every second for real-time display intervalId = setInterval(updateTime, 1000); }); onUnmounted(() => { if (intervalId) { clearInterval(intervalId); } }); return { currentTime }; } /** * Format timestamp to "time ago" string * @param timestamp - Unix timestamp in seconds * @param currentTime - Current time for real-time updates */ export function formatTimeAgo(timestamp: number, currentTime: number): string { const now = Math.floor(currentTime / 1000); const then = Math.floor(timestamp); const diffSeconds = Math.max(0, now - then); // Prevent negative values if (diffSeconds < 60) { return `${diffSeconds}s ago`; } const diffMinutes = Math.floor(diffSeconds / 60); if (diffMinutes < 60) { return `${diffMinutes}m ago`; } const diffHours = Math.floor(diffMinutes / 60); if (diffHours < 24) { return `${diffHours}h ago`; } const diffDays = Math.floor(diffHours / 24); return `${diffDays}d ago`; }