Message Meta: Fix time format in alt text (#2938)

This commit is contained in:
Alexander Zinchuk 2023-04-09 04:02:50 +02:00
parent 46b182bcb1
commit 494b8021f4
2 changed files with 14 additions and 5 deletions

View File

@ -66,9 +66,11 @@ const MessageMeta: FC<OwnProps> = ({
const title = useMemo(() => {
if (!isActivated) return undefined;
const createDateTime = formatDateTimeToString(message.date * 1000, lang.code);
const editDateTime = message.isEdited && formatDateTimeToString(message.editDate! * 1000, lang.code);
const forwardedDateTime = message.forwardInfo && formatDateTimeToString(message.forwardInfo.date * 1000, lang.code);
const createDateTime = formatDateTimeToString(message.date * 1000, lang.code, undefined, lang.timeFormat);
const editDateTime = message.isEdited
&& formatDateTimeToString(message.editDate! * 1000, lang.code, undefined, lang.timeFormat);
const forwardedDateTime = message.forwardInfo
&& formatDateTimeToString(message.forwardInfo.date * 1000, lang.code, undefined, lang.timeFormat);
let text = createDateTime;
if (editDateTime) {
@ -81,7 +83,9 @@ const MessageMeta: FC<OwnProps> = ({
}
return text;
}, [isActivated, lang, message]);
// We need to listen to timeformat change
// eslint-disable-next-line react-hooks-static-deps/exhaustive-deps
}, [isActivated, lang, message, lang.timeFormat]);
const fullClassName = buildClassName(
'MessageMeta',

View File

@ -1,4 +1,5 @@
import type { LangFn } from '../hooks/useLang';
import type { TimeFormat } from '../types';
import withCache from './withCache';
const WEEKDAYS_FULL = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
@ -290,7 +291,10 @@ export function formatDateToString(
return formatDayToStringWithCache(dayStartAt, locale, noYear, monthFormat, noDay);
}
export function formatDateTimeToString(datetime: Date | number, locale = 'en-US', noSeconds?: boolean) {
export function formatDateTimeToString(
datetime: Date | number, locale = 'en-US', noSeconds?: boolean,
timeFormat?: TimeFormat,
) {
const date = typeof datetime === 'number' ? new Date(datetime) : datetime;
return date.toLocaleString(
locale,
@ -301,6 +305,7 @@ export function formatDateTimeToString(datetime: Date | number, locale = 'en-US'
hour: 'numeric',
minute: 'numeric',
second: noSeconds ? undefined : 'numeric',
hourCycle: timeFormat === '12h' ? 'h12' : 'h23',
},
);
}