From dab1585014cc279f344ff05488f720dcfaa838db Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 3 May 2022 14:17:29 +0100 Subject: [PATCH] Composer: Fix duplicated pasting from clipboard (#1854) --- src/components/middle/composer/Composer.tsx | 10 +++++++++- .../middle/composer/hooks/useClipboardPaste.ts | 7 ++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/components/middle/composer/Composer.tsx b/src/components/middle/composer/Composer.tsx index 4f5b80fe3..a38f9abf8 100644 --- a/src/components/middle/composer/Composer.tsx +++ b/src/components/middle/composer/Composer.tsx @@ -48,6 +48,7 @@ import { selectEditingDraft, selectRequestedText, selectTheme, + selectCurrentMessageList, } from '../../../global/selectors'; import { getAllowedAttachmentOptions, @@ -131,6 +132,7 @@ type StateProps = isChatWithBot?: boolean; isChatWithSelf?: boolean; isChannel?: boolean; + isForCurrentMessageList: boolean; isRightColumnShown?: boolean; isSelectModeActive?: boolean; isForwarding?: boolean; @@ -201,6 +203,7 @@ const Composer: FC = ({ messageListType, draft, chat, + isForCurrentMessageList, connectionState, isChatWithBot, isChatWithSelf, @@ -499,7 +502,7 @@ const Composer: FC = ({ editingDraft, ); useDraft(draft, chatId, threadId, htmlRef, setHtml, editingMessage); - useClipboardPaste(insertTextAndUpdateCursor, setAttachments, editingMessage); + useClipboardPaste(isForCurrentMessageList, insertTextAndUpdateCursor, setAttachments, editingMessage); const handleEmbeddedClear = useCallback(() => { if (editingMessage) { @@ -1270,6 +1273,10 @@ export default memo(withGlobal( const sendAsUser = sendAsId ? selectUser(global, sendAsId) : undefined; const sendAsChat = !sendAsUser && sendAsId ? selectChat(global, sendAsId) : undefined; const requestedText = selectRequestedText(global, chatId); + const currentMessageList = selectCurrentMessageList(global); + const isForCurrentMessageList = chatId === currentMessageList?.chatId + && threadId === currentMessageList?.threadId + && messageListType === currentMessageList?.type; const editingDraft = messageListType === 'scheduled' ? selectEditingScheduledDraft(global, chatId) @@ -1283,6 +1290,7 @@ export default memo(withGlobal( isChatWithBot, isChatWithSelf, isPrivateChat, + isForCurrentMessageList, canScheduleUntilOnline: selectCanScheduleUntilOnline(global, chatId), isChannel: chat ? isChatChannel(chat) : undefined, isRightColumnShown: selectIsRightColumnShown(global), diff --git a/src/components/middle/composer/hooks/useClipboardPaste.ts b/src/components/middle/composer/hooks/useClipboardPaste.ts index 4257c747f..f67189155 100644 --- a/src/components/middle/composer/hooks/useClipboardPaste.ts +++ b/src/components/middle/composer/hooks/useClipboardPaste.ts @@ -9,11 +9,16 @@ const CLIPBOARD_ACCEPTED_TYPES = ['image/png', 'image/jpeg', 'image/gif']; const MAX_MESSAGE_LENGTH = 4096; const useClipboardPaste = ( + isActive: boolean, insertTextAndUpdateCursor: (text: string, inputId?: string) => void, setAttachments: StateHookSetter, editedMessage: ApiMessage | undefined, ) => { useEffect(() => { + if (!isActive) { + return undefined; + } + async function handlePaste(e: ClipboardEvent) { if (!e.clipboardData) { return; @@ -54,7 +59,7 @@ const useClipboardPaste = ( return () => { document.removeEventListener('paste', handlePaste, false); }; - }, [insertTextAndUpdateCursor, editedMessage, setAttachments]); + }, [insertTextAndUpdateCursor, editedMessage, setAttachments, isActive]); }; export default useClipboardPaste;