Skip to content

Commit b6e91a2

Browse files
authored
fix: clear composer state optimistically with snapshot restoration upon sending a message (#2710)
Make the clearing of the MessageInput UI immediate without any delay when sending a message.
1 parent 990e15c commit b6e91a2

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/components/MessageInput/hooks/useSubmitHandler.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ import { useChannelActionContext } from '../../../context/ChannelActionContext';
44
import { useTranslationContext } from '../../../context/TranslationContext';
55

66
import type { MessageInputProps } from '../MessageInput';
7+
import type { MessageComposer } from 'stream-chat';
8+
9+
const takeStateSnapshot = (messageComposer: MessageComposer) => {
10+
const textComposerState = messageComposer.textComposer.state.getLatestValue();
11+
const attachmentManagerState = messageComposer.attachmentManager.state.getLatestValue();
12+
const linkPreviewsManagerState =
13+
messageComposer.linkPreviewsManager.state.getLatestValue();
14+
const pollComposerState = messageComposer.pollComposer.state.getLatestValue();
15+
const customDataManagerState = messageComposer.customDataManager.state.getLatestValue();
16+
const state = messageComposer.state.getLatestValue();
17+
18+
return () => {
19+
messageComposer.state.next(state);
20+
messageComposer.textComposer.state.next(textComposerState);
21+
messageComposer.attachmentManager.state.next(attachmentManagerState);
22+
messageComposer.linkPreviewsManager.state.next(linkPreviewsManagerState);
23+
messageComposer.pollComposer.state.next(pollComposerState);
24+
messageComposer.customDataManager.state.next(customDataManagerState);
25+
};
26+
};
727

828
export const useSubmitHandler = (props: MessageInputProps) => {
929
const { clearEditingState, overrideSubmitHandler } = props;
@@ -29,7 +49,9 @@ export const useSubmitHandler = (props: MessageInputProps) => {
2949
addNotification(t('Edit message request failed'), 'error');
3050
}
3151
} else {
52+
const restoreComposerStateSnapshot = takeStateSnapshot(messageComposer);
3253
try {
54+
messageComposer.clear();
3355
// todo: get rid of overrideSubmitHandler once MessageComposer supports submission flow
3456
if (overrideSubmitHandler) {
3557
await overrideSubmitHandler({
@@ -41,10 +63,10 @@ export const useSubmitHandler = (props: MessageInputProps) => {
4163
} else {
4264
await sendMessage({ localMessage, message, options: sendOptions });
4365
}
44-
messageComposer.clear();
4566
if (messageComposer.config.text.publishTypingEvents)
4667
await messageComposer.channel.stopTyping();
4768
} catch (err) {
69+
restoreComposerStateSnapshot();
4870
addNotification(t('Send message request failed'), 'error');
4971
}
5072
}

0 commit comments

Comments
 (0)