diff --git a/src/components/common/Composer.tsx b/src/components/common/Composer.tsx index 8ead8705d..95d4cbb97 100644 --- a/src/components/common/Composer.tsx +++ b/src/components/common/Composer.tsx @@ -1012,7 +1012,7 @@ const Composer: FC = ({ lastMessageSendTimeSeconds.current = getServerTime(); - clearDraft({ chatId, isLocalOnly: true }); + clearDraft({ chatId, isLocalOnly: true, shouldKeepReply: isForwarding }); if (IS_IOS && messageInput && messageInput === document.activeElement) { applyIosAutoCapitalizationFix(messageInput); diff --git a/src/components/main/ForwardRecipientPicker.tsx b/src/components/main/ForwardRecipientPicker.tsx index 9de575d0a..367e9e171 100644 --- a/src/components/main/ForwardRecipientPicker.tsx +++ b/src/components/main/ForwardRecipientPicker.tsx @@ -5,13 +5,10 @@ import React, { import { getActions, getGlobal, withGlobal } from '../../global'; import type { ThreadId } from '../../types'; -import { MAIN_THREAD_ID } from '../../api/types'; import { getChatTitle, getUserFirstOrLastName, isUserId } from '../../global/helpers'; import { selectChat, - selectCurrentChat, - selectDraft, selectTabState, selectUser, } from '../../global/selectors'; @@ -30,7 +27,7 @@ interface StateProps { currentUserId?: string; isManyMessages?: boolean; isStory?: boolean; - isReplying?: boolean; + isForwarding?: boolean; } const ForwardRecipientPicker: FC = ({ @@ -38,7 +35,7 @@ const ForwardRecipientPicker: FC = ({ currentUserId, isManyMessages, isStory, - isReplying, + isForwarding, }) => { const { openChatOrTopicWithReplyInDraft, @@ -96,13 +93,13 @@ const ForwardRecipientPicker: FC = ({ } else { const chatId = recipientId; const topicId = threadId ? Number(threadId) : undefined; - if (isReplying) { - openChatOrTopicWithReplyInDraft({ chatId, topicId }); - } else { + if (isForwarding) { setForwardChatOrTopic({ chatId, topicId }); + } else { + openChatOrTopicWithReplyInDraft({ chatId, topicId }); } } - }, [currentUserId, isManyMessages, isStory, lang, isReplying]); + }, [currentUserId, isManyMessages, isStory, lang, isForwarding]); const handleClose = useCallback(() => { exitForwardMode(); @@ -126,12 +123,11 @@ const ForwardRecipientPicker: FC = ({ export default memo(withGlobal((global): StateProps => { const { messageIds, storyId } = selectTabState(global).forwardMessages; - const currentChatId = selectCurrentChat(global)?.id; - const isReplying = currentChatId && selectDraft(global, currentChatId, MAIN_THREAD_ID)?.replyInfo; + const isForwarding = (messageIds && messageIds.length > 0); return { currentUserId: global.currentUserId, isManyMessages: (messageIds?.length || 0) > 1, isStory: Boolean(storyId), - isReplying: Boolean(isReplying), + isForwarding, }; })(ForwardRecipientPicker)); diff --git a/src/components/middle/composer/ComposerEmbeddedMessage.tsx b/src/components/middle/composer/ComposerEmbeddedMessage.tsx index 3f0e2bdb4..15fcb8628 100644 --- a/src/components/middle/composer/ComposerEmbeddedMessage.tsx +++ b/src/components/middle/composer/ComposerEmbeddedMessage.tsx @@ -116,8 +116,8 @@ const ComposerEmbeddedMessage: FC = ({ const isShown = (() => { if (isInChangingRecipientMode) return false; - if (sender && isForwarding) return true; if (message && (replyInfo || editingId)) return true; + if (sender && isForwarding) return true; return false; })(); @@ -139,12 +139,12 @@ const ComposerEmbeddedMessage: FC = ({ }); const clearEmbedded = useLastCallback(() => { - if (replyInfo && !shouldForceShowEditing) { - resetDraftReplyInfo(); - } else if (editingId) { + if (editingId) { setEditingId({ messageId: undefined }); } else if (forwardedMessagesCount) { exitForwardMode(); + } else if (replyInfo && !shouldForceShowEditing) { + resetDraftReplyInfo(); } onClear?.(); }); @@ -210,15 +210,15 @@ const ComposerEmbeddedMessage: FC = ({ ); const leftIcon = useMemo(() => { - if (isShowingReply) { - return 'reply'; - } if (editingId) { return 'edit'; } if (isForwarding) { return 'forward'; } + if (isShowingReply) { + return 'reply'; + } return undefined; }, [editingId, isForwarding, isShowingReply]); @@ -398,25 +398,18 @@ export default memo(withGlobal( const senderChat = replyToPeerId ? selectChat(global, replyToPeerId) : undefined; let message: ApiMessage | undefined; - if (replyInfo && !shouldForceShowEditing) { - message = selectChatMessage(global, replyInfo.replyToPeerId || chatId, replyInfo.replyToMsgId); - } else if (editingId) { + if (editingId) { message = selectEditingMessage(global, chatId, threadId, messageListType); } else if (isForwarding && forwardMessageIds!.length === 1) { message = forwardedMessages?.[0]; + } else if (replyInfo && !shouldForceShowEditing) { + message = selectChatMessage(global, replyInfo.replyToPeerId || chatId, replyInfo.replyToMsgId); } let sender: ApiPeer | undefined; - if (replyInfo && message && !shouldForceShowEditing) { - const { forwardInfo } = message; - const isChatWithSelf = selectIsChatWithSelf(global, chatId); - if (forwardInfo && (forwardInfo.isChannelPost || isChatWithSelf)) { - sender = selectForwardedSender(global, message); - } - if (!sender && (!forwardInfo?.hiddenUserName || Boolean(replyInfo.quoteText))) { - sender = selectSender(global, message); - } + if (editingId && message) { + sender = selectSender(global, message); } else if (isForwarding) { if (message) { sender = selectForwardedSender(global, message); @@ -427,8 +420,16 @@ export default memo(withGlobal( if (!sender) { sender = selectPeer(global, fromChatId!); } - } else if (editingId && message) { - sender = selectSender(global, message); + } else if (replyInfo && message && !shouldForceShowEditing) { + const { forwardInfo } = message; + const isChatWithSelf = selectIsChatWithSelf(global, chatId); + if (forwardInfo && (forwardInfo.isChannelPost || isChatWithSelf)) { + sender = selectForwardedSender(global, message); + } + + if (!sender && (!forwardInfo?.hiddenUserName || Boolean(replyInfo.quoteText))) { + sender = selectSender(global, message); + } } const forwardsHaveCaptions = forwardedMessages?.some((forward) => ( diff --git a/src/global/actions/api/messages.ts b/src/global/actions/api/messages.ts index 1d0464634..bc97eb609 100644 --- a/src/global/actions/api/messages.ts +++ b/src/global/actions/api/messages.ts @@ -317,7 +317,9 @@ addActionHandler('sendMessage', (global, actions, payload): ActionReturnType => const chat = selectChat(global, chatId!)!; const draft = selectDraft(global, chatId!, threadId!); - const draftReplyInfo = !isStoryReply ? draft?.replyInfo : undefined; + const isForwarding = selectTabState(global, tabId).forwardMessages?.messageIds?.length; + + const draftReplyInfo = !isForwarding && !isStoryReply ? draft?.replyInfo : undefined; const storyReplyInfo = isStoryReply ? { type: 'story', @@ -1795,11 +1797,6 @@ addActionHandler('openChatOrTopicWithReplyInDraft', (global, actions, payload): global = getGlobal(); - if (!selectReplyCanBeSentToChat(global, toChatId, tabId)) { - actions.showNotification({ message: translate('Chat.SendNotAllowedText'), tabId }); - return; - } - global = updateTabState(global, { forwardMessages: { ...selectTabState(global, tabId).forwardMessages, @@ -1809,23 +1806,27 @@ addActionHandler('openChatOrTopicWithReplyInDraft', (global, actions, payload): setGlobal(global); const currentChat = selectCurrentChat(global, tabId); - if (!currentChat) return; + const currentThreadId = selectCurrentMessageList(global, tabId)?.threadId; + + if (!currentChat || !currentThreadId) return; const threadId = topicId || MAIN_THREAD_ID; const currentChatId = currentChat.id; - const currentReplyInfo = selectDraft(global, currentChatId, threadId)?.replyInfo; + const currentReplyInfo = selectDraft(global, currentChatId, currentThreadId)?.replyInfo; if (!currentReplyInfo) return; + + if (!selectReplyCanBeSentToChat(global, toChatId, currentChatId, currentReplyInfo)) { + actions.showNotification({ message: translate('Chat.SendNotAllowedText'), tabId }); + return; + } + if (!currentReplyInfo.replyToPeerId && toChatId === currentChat.id) return; const getPeerId = () => { if (!currentReplyInfo?.replyToPeerId) return currentChatId; return currentReplyInfo.replyToPeerId === toChatId ? undefined : currentReplyInfo.replyToPeerId; }; - const currentThreadId = selectCurrentMessageList(global, tabId)?.threadId; - if (!currentThreadId) { - return; - } const replyToPeerId = getPeerId(); const newReply: ApiInputMessageReplyInfo = { ...currentReplyInfo, @@ -1869,7 +1870,6 @@ addActionHandler('setForwardChatOrTopic', async (global, actions, payload): Prom }, }, tabId); setGlobal(global); - actions.openThread({ chatId, threadId: topicId || MAIN_THREAD_ID, tabId }); actions.closeMediaViewer({ tabId }); actions.exitMessageSelectMode({ tabId }); diff --git a/src/global/selectors/messages.ts b/src/global/selectors/messages.ts index fdd22e4ee..7719166f3 100644 --- a/src/global/selectors/messages.ts +++ b/src/global/selectors/messages.ts @@ -1363,15 +1363,13 @@ export function selectRequestedMessageTranslationLanguage export function selectReplyCanBeSentToChat( global: T, toChatId: string, - ...[tabId = getCurrentTabId()]: TabArgs + fromChatId: string, + replyInfo: ApiInputMessageReplyInfo, ) { - const currentChat = selectCurrentChat(global, tabId); - if (!currentChat) return false; - const replyInfo = selectDraft(global, currentChat.id, MAIN_THREAD_ID)?.replyInfo; - if (!replyInfo || !replyInfo.replyToMsgId) return false; - const fromChatId = replyInfo?.replyToPeerId ?? currentChat.id; - if (toChatId === fromChatId) return true; - const chatMessages = selectChatMessages(global, fromChatId!); + if (!replyInfo.replyToMsgId) return false; + const fromRealChatId = replyInfo?.replyToPeerId ?? fromChatId; + if (toChatId === fromRealChatId) return true; + const chatMessages = selectChatMessages(global, fromRealChatId!); const message = chatMessages[replyInfo.replyToMsgId]; return !isExpiredMessage(message);