From ab6276327a75a1413413f9595b43c68568076453 Mon Sep 17 00:00:00 2001 From: Sivin Varghese <64252451+iamsivin@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:09:38 +0530 Subject: [PATCH] feat: Show year in message timestamp if the message is not from the current year (#6830) * feat: Shows year in message timestamp if the message is not from the current year * chore: Naming fix --- .../widgets/conversation/bubble/Actions.vue | 2 +- .../dashboard/mixins/specs/time.spec.js | 14 ++++++++++++++ app/javascript/dashboard/mixins/time.js | 18 +++++++++++++++--- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue b/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue index 1695cedfe..eb51af58a 100644 --- a/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue +++ b/app/javascript/dashboard/components/widgets/conversation/bubble/Actions.vue @@ -175,7 +175,7 @@ export default { return MESSAGE_STATUS.SENT === this.messageStatus; }, readableTime() { - return this.messageStamp(this.createdAt, 'LLL d, h:mm a'); + return this.messageTimestamp(this.createdAt, 'LLL d, h:mm a'); }, screenName() { const { additional_attributes: additionalAttributes = {} } = diff --git a/app/javascript/dashboard/mixins/specs/time.spec.js b/app/javascript/dashboard/mixins/specs/time.spec.js index f185a0a66..4df849efc 100644 --- a/app/javascript/dashboard/mixins/specs/time.spec.js +++ b/app/javascript/dashboard/mixins/specs/time.spec.js @@ -9,6 +9,20 @@ describe('#messageStamp', () => { }); }); +describe('#messageTimestamp', () => { + it('should return the message date in the specified format if the message was sent in the current year', () => { + const now = new Date(); + expect(TimeMixin.methods.messageTimestamp(now.getTime() / 1000)).toEqual( + 'Apr 5, 2023' + ); + }); + it('should return the message date and time in a different format if the message was sent in a different year', () => { + expect(TimeMixin.methods.messageTimestamp(1612971343)).toEqual( + 'Feb 10 2021, 3:35 PM' + ); + }); +}); + describe('#dynamicTime', () => { it('returns correct value', () => { expect(TimeMixin.methods.dynamicTime(1612971343)).toEqual( diff --git a/app/javascript/dashboard/mixins/time.js b/app/javascript/dashboard/mixins/time.js index eec357b88..6b63f97f9 100644 --- a/app/javascript/dashboard/mixins/time.js +++ b/app/javascript/dashboard/mixins/time.js @@ -1,6 +1,9 @@ -import fromUnixTime from 'date-fns/fromUnixTime'; -import format from 'date-fns/format'; -import formatDistanceToNow from 'date-fns/formatDistanceToNow'; +import { + format, + isSameYear, + fromUnixTime, + formatDistanceToNow, +} from 'date-fns'; export default { methods: { @@ -8,6 +11,15 @@ export default { const unixTime = fromUnixTime(time); return format(unixTime, dateFormat); }, + messageTimestamp(time, dateFormat = 'MMM d, yyyy') { + const messageTime = fromUnixTime(time); + const now = new Date(); + const messageDate = format(messageTime, dateFormat); + if (!isSameYear(messageTime, now)) { + return format(messageTime, 'LLL d y, h:mm a'); + } + return messageDate; + }, dynamicTime(time) { const unixTime = fromUnixTime(time); return formatDistanceToNow(unixTime, { addSuffix: true });