From 921eb5e07dfd975c4f3206374ece993f8313b8c9 Mon Sep 17 00:00:00 2001 From: Arunavo Ray Date: Fri, 29 Aug 2025 19:08:48 +0530 Subject: [PATCH] util --- src/lib/utils.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 1dbd483..e674517 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -29,6 +29,31 @@ export function formatDate(date?: Date | string | null): string { }).format(new Date(date)); } +export function formatLastSyncTime(date: Date | string | null): string { + if (!date) return "Never"; + + const now = new Date(); + const syncDate = new Date(date); + const diffMs = now.getTime() - syncDate.getTime(); + const diffMins = Math.floor(diffMs / 60000); + const diffHours = Math.floor(diffMs / 3600000); + const diffDays = Math.floor(diffMs / 86400000); + + // Show relative time for recent syncs + if (diffMins < 1) return "Just now"; + if (diffMins < 60) return `${diffMins} min ago`; + if (diffHours < 24) return `${diffHours} hr${diffHours === 1 ? '' : 's'} ago`; + if (diffDays < 7) return `${diffDays} day${diffDays === 1 ? '' : 's'} ago`; + + // For older syncs, show week count + const diffWeeks = Math.floor(diffDays / 7); + if (diffWeeks < 4) return `${diffWeeks} week${diffWeeks === 1 ? '' : 's'} ago`; + + // For even older, show month count + const diffMonths = Math.floor(diffDays / 30); + return `${diffMonths} month${diffMonths === 1 ? '' : 's'} ago`; +} + export function truncate(str: string, length: number): string { if (str.length <= length) return str; return str.slice(0, length) + "...";