From 2b3cc19c50f4b0d7910de6777fc04d15fc5ce4b8 Mon Sep 17 00:00:00 2001 From: Nico de Haen Date: Sun, 8 Dec 2024 16:05:16 +0100 Subject: [PATCH] Avoid drafts in chats where canSend is false (#4394) * Avoid drafts in chats where canSend is false resolves #4389 * Disable Ctrl+Up shortcut in readonly chats * Clear draft if canSend == false --- CHANGELOG.md | 1 + .../frontend/src/components/composer/Composer.tsx | 13 +++++++++---- .../components/message/MessageListAndComposer.tsx | 1 + 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d4906e54eb..34d93f5c1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ## Fixed - handle double escape on Dialog #4365 - fix random crashes on quote reply #4337 +- avoid drafts in readonly chats #4349 diff --git a/packages/frontend/src/components/composer/Composer.tsx b/packages/frontend/src/components/composer/Composer.tsx index 4597b78b4d..459b359a81 100644 --- a/packages/frontend/src/components/composer/Composer.tsx +++ b/packages/frontend/src/components/composer/Composer.tsx @@ -424,6 +424,7 @@ export function useDraft( chatId: number | null, isContactRequest: boolean, isProtectionBroken: boolean, + canSend: boolean, // no draft needed in chats we can't send messages inputRef: React.MutableRefObject ): { draftState: DraftObject @@ -443,6 +444,10 @@ export function useDraft( const loadDraft = useCallback( (chatId: number) => { + if (chatId === null || !canSend) { + clearDraft() + return + } BackendRemote.rpc.getDraft(selectedAccountId(), chatId).then(newDraft => { if (!newDraft) { log.debug('no draft') @@ -468,7 +473,7 @@ export function useDraft( }) }) }, - [clearDraft, inputRef] + [clearDraft, inputRef, canSend] ) useEffect(() => { @@ -482,7 +487,7 @@ export function useDraft( }, [chatId, loadDraft, isContactRequest, isProtectionBroken]) const saveDraft = useCallback(async () => { - if (chatId === null) { + if (chatId === null || !canSend) { return } if (inputRef.current?.textareaRef.current?.disabled) { @@ -533,7 +538,7 @@ export function useDraft( } else { clearDraft() } - }, [chatId, clearDraft, inputRef]) + }, [chatId, clearDraft, canSend, inputRef]) const updateDraftText = (text: string, InputChatId: number) => { if (chatId !== InputChatId) { @@ -586,7 +591,7 @@ export function useDraft( | KeybindAction.Composer_SelectReplyToUp | KeybindAction.Composer_SelectReplyToDown ) => { - if (chatId == undefined) { + if (chatId == undefined || !canSend) { return } const quoteMessage = (messageId: number) => { diff --git a/packages/frontend/src/components/message/MessageListAndComposer.tsx b/packages/frontend/src/components/message/MessageListAndComposer.tsx index 8314dc67c9..d4652c98f0 100644 --- a/packages/frontend/src/components/message/MessageListAndComposer.tsx +++ b/packages/frontend/src/components/message/MessageListAndComposer.tsx @@ -91,6 +91,7 @@ export default function MessageListAndComposer({ accountId, chat }: Props) { chat.id, chat.isContactRequest, chat.isProtectionBroken, + chat.canSend, messageInputRef )