From b13e4f258ecb1aea4bd2351448aedd4602c3b147 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Sat, 7 Jun 2025 16:32:45 +0100 Subject: [PATCH] feat(cms): relative date format in ama --- cms/schemaTypes/ama.ts | 44 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/cms/schemaTypes/ama.ts b/cms/schemaTypes/ama.ts index ff3b7516..b9660a6d 100644 --- a/cms/schemaTypes/ama.ts +++ b/cms/schemaTypes/ama.ts @@ -8,11 +8,49 @@ export default defineType({ select: { title: 'content', answered: 'answered', + createdAt: '_createdAt', }, - prepare ({ title, answered }) { + prepare ({ title, answered, createdAt }) { + const now = new Date() + const created = new Date(createdAt) + const diffMs = now.getTime() - created.getTime() + + const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' }) + + let timeAgo + const diffSeconds = Math.floor(diffMs / 1000) + const diffMinutes = Math.floor(diffSeconds / 60) + const diffHours = Math.floor(diffMinutes / 60) + const diffDays = Math.floor(diffHours / 24) + const diffWeeks = Math.floor(diffDays / 7) + const diffMonths = Math.floor(diffDays / 30) + const diffYears = Math.floor(diffDays / 365) + + if (diffYears > 0) { + timeAgo = rtf.format(-diffYears, 'year') + } + else if (diffMonths > 0) { + timeAgo = rtf.format(-diffMonths, 'month') + } + else if (diffWeeks > 0) { + timeAgo = rtf.format(-diffWeeks, 'week') + } + else if (diffDays > 0) { + timeAgo = rtf.format(-diffDays, 'day') + } + else if (diffHours > 0) { + timeAgo = rtf.format(-diffHours, 'hour') + } + else if (diffMinutes > 0) { + timeAgo = rtf.format(-diffMinutes, 'minute') + } + else { + timeAgo = 'now' + } + return { - title: title.slice(0, 30), - subtitle: answered ? '✅ Answered' : '✨ Not answered', + title: title.slice(0, 50), + subtitle: `${answered ? '✅ Answered' : '✨ Not answered'} • ${timeAgo}`, } }, }, -- 2.51.2