Draft: Fix overwriting draft on chat change (#4485)

This commit is contained in:
Alexander Zinchuk 2024-04-19 13:38:47 +04:00
parent 956a59b457
commit 2688cd768a

View File

@ -1,4 +1,4 @@
import { useEffect, useRef } from '../../../../lib/teact/teact'; import { useEffect, useLayoutEffect, useRef } from '../../../../lib/teact/teact';
import { getActions } from '../../../../global'; import { getActions } from '../../../../global';
import type { ApiMessage } from '../../../../api/types'; import type { ApiMessage } from '../../../../api/types';
@ -73,11 +73,13 @@ const useDraft = ({
const html = getHtml(); const html = getHtml();
if (html) { if (html) {
requestMeasure(() => {
saveDraft({ saveDraft({
chatId: prevState.chatId ?? chatId, chatId: prevState.chatId ?? chatId,
threadId: prevState.threadId ?? threadId, threadId: prevState.threadId ?? threadId,
text: parseHtmlAsFormattedText(html), text: parseHtmlAsFormattedText(html),
}); });
});
} else { } else {
clearDraft({ clearDraft({
chatId: prevState.chatId ?? chatId, chatId: prevState.chatId ?? chatId,
@ -117,8 +119,8 @@ const useDraft = ({
if (customEmojiIds.length) loadCustomEmojis({ ids: customEmojiIds }); if (customEmojiIds.length) loadCustomEmojis({ ids: customEmojiIds });
}, [chatId, threadId, draft, getHtml, setHtml, editedMessage, isDisabled]); }, [chatId, threadId, draft, getHtml, setHtml, editedMessage, isDisabled]);
// Save draft on chat change // Save draft on chat change. Should be layout effect to read correct html on cleanup
useEffect(() => { useLayoutEffect(() => {
if (isDisabled) { if (isDisabled) {
return undefined; return undefined;
} }