From d12c9d384dbc4a286a4c7df2694d497c3110773e Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Fri, 31 Dec 2021 18:17:24 +0100 Subject: [PATCH] Message: Display more info on time hover (#1608) --- src/api/gramjs/apiBuilders/messages.ts | 4 ++- src/api/types/messages.ts | 2 ++ src/components/middle/message/MessageMeta.tsx | 29 +++++++++++++++++-- src/util/dateFormat.ts | 18 +++++++++++- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/api/gramjs/apiBuilders/messages.ts b/src/api/gramjs/apiBuilders/messages.ts index 00e382ab..2a3698a0 100644 --- a/src/api/gramjs/apiBuilders/messages.ts +++ b/src/api/gramjs/apiBuilders/messages.ts @@ -168,7 +168,7 @@ export function buildApiMessageWithChatId(chatId: string, mtpMessage: UniversalM ...(replyToPeerId && { replyToChatId: getApiChatIdFromMtpPeer(replyToPeerId) }), ...(replyToTopId && { replyToTopMessageId: replyToTopId }), ...(forwardInfo && { forwardInfo }), - ...(isEdited && { isEdited }), + ...(isEdited && { isEdited, editDate: mtpMessage.editDate }), ...(isMediaUnread && { isMediaUnread }), ...(mtpMessage.mentioned && isMediaUnread && { hasUnreadMention: true }), ...(mtpMessage.mentioned && { isMentioned: true }), @@ -254,6 +254,7 @@ function buildApiMessageForwardInfo(fwdFrom: GramJs.MessageFwdHeader, isChatWith const fromId = fwdFrom.fromId && getApiChatIdFromMtpPeer(fwdFrom.fromId); return { + date: fwdFrom.date, isChannelPost: Boolean(fwdFrom.channelPost), channelPostId: fwdFrom.channelPost, isLinkedChannelPost: Boolean(fwdFrom.channelPost && savedFromPeerId && !isChatWithSelf), @@ -897,6 +898,7 @@ export function buildLocalForwardedMessage( // Forward info doesn't get added when users forwards his own messages, also when forwarding audio ...(senderId !== currentUserId && !isAudio && { forwardInfo: { + date: message.date, isChannelPost: false, fromChatId, fromMessageId, diff --git a/src/api/types/messages.ts b/src/api/types/messages.ts index 15b81cb0..e4de2e34 100644 --- a/src/api/types/messages.ts +++ b/src/api/types/messages.ts @@ -175,6 +175,7 @@ export interface ApiWebPage { } export interface ApiMessageForwardInfo { + date: number; isChannelPost: boolean; channelPostId?: number; isLinkedChannelPost?: boolean; @@ -247,6 +248,7 @@ export interface ApiMessage { previousLocalId?: number; views?: number; isEdited?: boolean; + editDate?: number; isMentioned?: boolean; isMediaUnread?: boolean; groupedId?: string; diff --git a/src/components/middle/message/MessageMeta.tsx b/src/components/middle/message/MessageMeta.tsx index 352ba842..d3ca0b5e 100644 --- a/src/components/middle/message/MessageMeta.tsx +++ b/src/components/middle/message/MessageMeta.tsx @@ -1,13 +1,16 @@ -import React, { FC, memo } from '../../../lib/teact/teact'; +import React, { + FC, memo, useCallback, useMemo, +} from '../../../lib/teact/teact'; import { ApiMessage, ApiMessageOutgoingStatus } from '../../../api/types'; -import { formatTime } from '../../../util/dateFormat'; +import { formatDateTimeToString, formatTime } from '../../../util/dateFormat'; import { formatIntegerCompact } from '../../../util/textFormat'; import MessageOutgoingStatus from '../../common/MessageOutgoingStatus'; import renderText from '../../common/helpers/renderText'; import useLang from '../../../hooks/useLang'; +import useFlag from '../../../hooks/useFlag'; import './MessageMeta.scss'; @@ -22,6 +25,26 @@ const MessageMeta: FC = ({ message, outgoingStatus, signature, onClick, }) => { const lang = useLang(); + const [isActivated, markActivated] = useFlag(); + + 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); + + let text = createDateTime; + if (editDateTime) { + text += '\n'; + text += lang('lng_edited_date').replace('{date}', editDateTime); + } + if (forwardedDateTime) { + text += '\n'; + text += lang('lng_forwarded_date').replace('{date}', forwardedDateTime); + } + + return text; + }, [isActivated, lang, message]); return ( @@ -36,7 +59,7 @@ const MessageMeta: FC = ({ {signature && ( {renderText(signature)} )} - + {message.isEdited && `${lang('EditedMessage')} `} {formatTime(message.date * 1000, lang)} diff --git a/src/util/dateFormat.ts b/src/util/dateFormat.ts index 24fff687..231310c9 100644 --- a/src/util/dateFormat.ts +++ b/src/util/dateFormat.ts @@ -197,7 +197,8 @@ export function formatVoiceRecordDuration(durationInMs: number) { return `${parts.join(':')},${String(milliseconds).padStart(2, '0')}`; } -export function formatDateToString(date: Date, locale = 'en-US') { +export function formatDateToString(datetime: Date | number, locale = 'en-US') { + const date = typeof datetime === 'number' ? new Date(datetime) : datetime; return date.toLocaleString( locale, { @@ -208,6 +209,21 @@ export function formatDateToString(date: Date, locale = 'en-US') { ); } +export function formatDateTimeToString(datetime: Date | number, locale = 'en-US') { + const date = typeof datetime === 'number' ? new Date(datetime) : datetime; + return date.toLocaleString( + locale, + { + year: 'numeric', + month: 'short', + day: 'numeric', + hour: 'numeric', + minute: 'numeric', + second: 'numeric', + }, + ); +} + function isValidDate(day: number, month: number, year = 2021): boolean { if (month > (MAX_MONTH_IN_YEAR - 1) || day > MAX_DAY_IN_MONTH) { return false;