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
This commit is contained in:
Sivin Varghese
2023-04-06 15:09:38 +05:30
committed by GitHub
parent ee131011f9
commit ab6276327a
3 changed files with 30 additions and 4 deletions

View File

@@ -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 = {} } =

View File

@@ -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(

View File

@@ -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 });