mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 20:18:08 +00:00
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:
@@ -175,7 +175,7 @@ export default {
|
|||||||
return MESSAGE_STATUS.SENT === this.messageStatus;
|
return MESSAGE_STATUS.SENT === this.messageStatus;
|
||||||
},
|
},
|
||||||
readableTime() {
|
readableTime() {
|
||||||
return this.messageStamp(this.createdAt, 'LLL d, h:mm a');
|
return this.messageTimestamp(this.createdAt, 'LLL d, h:mm a');
|
||||||
},
|
},
|
||||||
screenName() {
|
screenName() {
|
||||||
const { additional_attributes: additionalAttributes = {} } =
|
const { additional_attributes: additionalAttributes = {} } =
|
||||||
|
|||||||
@@ -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', () => {
|
describe('#dynamicTime', () => {
|
||||||
it('returns correct value', () => {
|
it('returns correct value', () => {
|
||||||
expect(TimeMixin.methods.dynamicTime(1612971343)).toEqual(
|
expect(TimeMixin.methods.dynamicTime(1612971343)).toEqual(
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import fromUnixTime from 'date-fns/fromUnixTime';
|
import {
|
||||||
import format from 'date-fns/format';
|
format,
|
||||||
import formatDistanceToNow from 'date-fns/formatDistanceToNow';
|
isSameYear,
|
||||||
|
fromUnixTime,
|
||||||
|
formatDistanceToNow,
|
||||||
|
} from 'date-fns';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
methods: {
|
methods: {
|
||||||
@@ -8,6 +11,15 @@ export default {
|
|||||||
const unixTime = fromUnixTime(time);
|
const unixTime = fromUnixTime(time);
|
||||||
return format(unixTime, dateFormat);
|
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) {
|
dynamicTime(time) {
|
||||||
const unixTime = fromUnixTime(time);
|
const unixTime = fromUnixTime(time);
|
||||||
return formatDistanceToNow(unixTime, { addSuffix: true });
|
return formatDistanceToNow(unixTime, { addSuffix: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user