From 195a38b88d202f8f7bd761606de703170fb3fdbe Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Wed, 25 Aug 2021 16:29:24 +0300 Subject: [PATCH] Composer: Fix recording not stopped when closing chat on mobile --- src/components/middle/composer/Composer.tsx | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/components/middle/composer/Composer.tsx b/src/components/middle/composer/Composer.tsx index f1a16dbe..e22952f7 100644 --- a/src/components/middle/composer/Composer.tsx +++ b/src/components/middle/composer/Composer.tsx @@ -424,16 +424,15 @@ const Composer: FC = ({ } }, [closeStickerTooltip, closeCalendar, closeMentionTooltip, closeEmojiTooltip, closeSymbolMenu]); - // Handle chat change - const prevChatId = usePrevious(chatId); + // Handle chat change (ref is used to avoid redundant effect calls) + const stopRecordingVoiceRef = useRef(); + stopRecordingVoiceRef.current = stopRecordingVoice; useEffect(() => { - if (!prevChatId || chatId === prevChatId) { - return; - } - - stopRecordingVoice(); - resetComposer(); - }, [chatId, prevChatId, resetComposer, stopRecordingVoice]); + return () => { + stopRecordingVoiceRef.current!(); + resetComposer(); + }; + }, [chatId, resetComposer, stopRecordingVoiceRef]); const handleEditComplete = useEditing(htmlRef, setHtml, editingMessage, resetComposer, openDeleteModal, editMessage); useDraft(draft, chatId, threadId, html, htmlRef, setHtml, editingMessage, saveDraft, clearDraft);