Skip to content

Commit 5e61cf4

Browse files
authored
fix: poll creation and optimistic updates (#3117)
* fix: poll creation and optimistic updates * fix: multi poll sending edge case
1 parent 2402084 commit 5e61cf4

File tree

5 files changed

+22
-12
lines changed

5 files changed

+22
-12
lines changed

package/src/components/MessageInput/SendButton.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type SendButtonProps = Partial<Pick<MessageInputContextValue, 'sendMessag
2222

2323
// TODO: Comment out once the commands PR has been merged on the LLC
2424
// @ts-ignore
25+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2526
const textComposerStateSelector = (state: TextComposerState) => ({
2627
// TODO: Comment out once the commands PR has been merged on the LLC
2728
// command: state.command,

package/src/components/MessageInput/__tests__/AudioAttachmentUploadPreview.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@ import { act, fireEvent, render, screen, waitFor } from '@testing-library/react-
44

55
import { OverlayProvider } from '../../../contexts';
66
import { initiateClientWithChannels } from '../../../mock-builders/api/initiateClientWithChannels';
7-
import {
8-
generateAudioAttachment,
9-
generateFileAttachment,
10-
generateVideoAttachment,
11-
} from '../../../mock-builders/attachments';
7+
import { generateAudioAttachment } from '../../../mock-builders/attachments';
128

139
import { FileState } from '../../../utils/utils';
1410
import { Channel } from '../../Channel/Channel';

package/src/components/Poll/CreatePollContent.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,13 @@ export const CreatePoll = ({
158158

159159
const createAndSendPoll = useCallback(async () => {
160160
try {
161-
/**
162-
* The poll is emptied inside the createPoll method(using initState) which is why we close the dialog
163-
* first so that it doesn't look weird.
164-
*/
165-
closePollCreationDialog?.();
166161
await messageComposer.createPoll();
167162
await sendMessage();
163+
closePollCreationDialog?.();
164+
// it's important that the reset of the pollComposer state happens
165+
// after we've already closed the modal; as otherwise we'd get weird
166+
// UI behaviour.
167+
messageComposer.pollComposer.initState();
168168
} catch (error) {
169169
console.log('Error creating a poll and sending a message:', error);
170170
}

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ import {
1616
// createDraftCommandInjectionMiddleware,
1717
LocalMessage,
1818
Message,
19+
MessageComposer,
1920
SendMessageOptions,
2021
StreamChat,
21-
Message as StreamMessage,
2222
// TextComposerMiddleware,
23+
Message as StreamMessage,
2324
UpdateMessageOptions,
2425
UploadRequestFn,
2526
UserResponse,
@@ -624,7 +625,18 @@ export const MessageInputProvider = ({
624625
}
625626
} else {
626627
try {
627-
messageComposer.clear();
628+
// Since the message id does not get cleared, we have to handle this manually
629+
// and let the poll creation dialog handle clearing the rest of the state. Once
630+
// sending a message has been moved to the composer as an API, this will be
631+
// redundant and can be removed.
632+
if (localMessage.poll_id) {
633+
messageComposer.state.partialNext({
634+
id: MessageComposer.generateId(),
635+
pollId: null,
636+
});
637+
} else {
638+
messageComposer.clear();
639+
}
628640
await value.sendMessage({
629641
localMessage: {
630642
...localMessage,

package/src/utils/setupCommandUIMiddleware.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88

99
// TODO: Comment out once the commands PR has been merged on the LLC
1010
// @ts-ignore
11+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1112
export const setupCommandUIMiddleware = (messageComposer: MessageComposer) => {
1213
// TODO: Comment out once the commands PR has been merged on the LLC
1314
// messageComposer.compositionMiddlewareExecutor.insert({

0 commit comments

Comments
 (0)