chore: 提交剩余开发改动
包含商务通消息映射、会话时间与未读状态、前端消息展示、数据库修复迁移、测试覆盖备份及 Captain 设计文档。
This commit is contained in:
@@ -7,6 +7,7 @@ import {
|
||||
getDayDifferenceFromNow,
|
||||
hasOneDayPassed,
|
||||
} from 'shared/helpers/timeHelper';
|
||||
import { zhCN } from 'date-fns/locale';
|
||||
|
||||
beforeEach(() => {
|
||||
process.env.TZ = 'UTC';
|
||||
@@ -35,12 +36,27 @@ describe('#messageTimestamp', () => {
|
||||
it('should return the message date and time in a different format if the message was sent in a different year', () => {
|
||||
expect(messageTimestamp(1612971343)).toEqual('Feb 10 2021, 3:35 PM');
|
||||
});
|
||||
it('supports Chinese message time formats for current and previous years', () => {
|
||||
expect(
|
||||
messageTimestamp(1680777464, 'M月d日 aaa h:mm', {
|
||||
locale: zhCN,
|
||||
fallbackFormat: 'yyyy年M月d日 aaa h:mm',
|
||||
})
|
||||
).toEqual('4月6日 上午 10:37');
|
||||
expect(
|
||||
messageTimestamp(1612971343, 'M月d日 aaa h:mm', {
|
||||
locale: zhCN,
|
||||
fallbackFormat: 'yyyy年M月d日 aaa h:mm',
|
||||
})
|
||||
).toEqual('2021年2月10日 下午 3:35');
|
||||
});
|
||||
});
|
||||
|
||||
describe('#dynamicTime', () => {
|
||||
it('returns correct value', () => {
|
||||
Date.now = vi.fn(() => new Date(Date.UTC(2023, 1, 14)).valueOf());
|
||||
expect(dynamicTime(1612971343)).toEqual('about 2 years ago');
|
||||
expect(dynamicTime(1612971343, { locale: zhCN })).toEqual('大约 2 年前');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -48,6 +64,9 @@ describe('#dateFormat', () => {
|
||||
it('returns correct value', () => {
|
||||
expect(dateFormat(1612971343)).toEqual('Feb 10, 2021');
|
||||
expect(dateFormat(1612971343, 'LLL d, yyyy')).toEqual('Feb 10, 2021');
|
||||
expect(dateFormat(1612971343, 'yyyy年M月d日', { locale: zhCN })).toEqual(
|
||||
'2021年2月10日'
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -21,14 +21,20 @@ export const messageStamp = (time, dateFormat = 'h:mm a') => {
|
||||
* Provides a formatted timestamp, adjusting the format based on the current year.
|
||||
* @param {number} time - Unix timestamp.
|
||||
* @param {string} [dateFormat='MMM d, yyyy'] - Desired date format.
|
||||
* @param {object} [options] - date-fns options and a different-year fallback format.
|
||||
* @returns {string} Formatted date string.
|
||||
*/
|
||||
export const messageTimestamp = (time, dateFormat = 'MMM d, yyyy') => {
|
||||
export const messageTimestamp = (
|
||||
time,
|
||||
dateFormat = 'MMM d, yyyy',
|
||||
options = {}
|
||||
) => {
|
||||
const messageTime = fromUnixTime(time);
|
||||
const now = new Date();
|
||||
const messageDate = format(messageTime, dateFormat);
|
||||
const { fallbackFormat = 'LLL d y, h:mm a', ...formatOptions } = options;
|
||||
const messageDate = format(messageTime, dateFormat, formatOptions);
|
||||
if (!isSameYear(messageTime, now)) {
|
||||
return format(messageTime, 'LLL d y, h:mm a');
|
||||
return format(messageTime, fallbackFormat, formatOptions);
|
||||
}
|
||||
return messageDate;
|
||||
};
|
||||
@@ -38,9 +44,9 @@ export const messageTimestamp = (time, dateFormat = 'MMM d, yyyy') => {
|
||||
* @param {number} time - Unix timestamp.
|
||||
* @returns {string} Relative time string.
|
||||
*/
|
||||
export const dynamicTime = time => {
|
||||
export const dynamicTime = (time, options = {}) => {
|
||||
const unixTime = fromUnixTime(time);
|
||||
return formatDistanceToNow(unixTime, { addSuffix: true });
|
||||
return formatDistanceToNow(unixTime, { addSuffix: true, ...options });
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -49,9 +55,9 @@ export const dynamicTime = time => {
|
||||
* @param {string} [dateFormat='MMM d, yyyy'] - Desired date format.
|
||||
* @returns {string} Formatted date string.
|
||||
*/
|
||||
export const dateFormat = (time, df = 'MMM d, yyyy') => {
|
||||
export const dateFormat = (time, df = 'MMM d, yyyy', options = {}) => {
|
||||
const unixTime = fromUnixTime(time);
|
||||
return format(unixTime, df);
|
||||
return format(unixTime, df, options);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user