Skip to content

Commit 2181a6c

Browse files
committed
fix: make sending flow faster and bug free and fix bug with cooldown
1 parent 8b4268c commit 2181a6c

File tree

4 files changed

+17
-44
lines changed

4 files changed

+17
-44
lines changed

package/src/components/MessageInput/InputButtons.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { StyleSheet, View } from 'react-native';
33

44
import { CustomDataManagerState, TextComposerState } from 'stream-chat';
55

6+
import { AttachmentPickerContextValue, useAttachmentPickerContext } from '../../contexts';
67
import { useAttachmentManagerState } from '../../contexts/messageInputContext/hooks/useAttachmentManagerState';
78
import { useMessageComposer } from '../../contexts/messageInputContext/hooks/useMessageComposer';
89
import {
@@ -24,9 +25,9 @@ export type InputButtonsWithContextProps = Pick<
2425
| 'hasFilePicker'
2526
| 'hasImagePicker'
2627
| 'MoreOptionsButton'
27-
| 'selectedPicker'
2828
| 'toggleAttachmentPicker'
29-
>;
29+
> &
30+
Pick<AttachmentPickerContextValue, 'selectedPicker'>;
3031

3132
const textComposerStateSelector = (state: TextComposerState) => ({
3233
text: state.text,
@@ -148,9 +149,9 @@ export const InputButtons = (props: InputButtonsProps) => {
148149
hasFilePicker,
149150
hasImagePicker,
150151
MoreOptionsButton,
151-
selectedPicker,
152152
toggleAttachmentPicker,
153153
} = useMessageInputContext();
154+
const { selectedPicker } = useAttachmentPickerContext();
154155

155156
return (
156157
<MemoizedInputButtonsWithContext

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ type MessageInputPropsWithContext = Pick<
141141
| 'maxNumberOfFiles'
142142
| 'resetInput'
143143
| 'SendButton'
144-
| 'sending'
145144
| 'ShowThreadMessageInChannelButton'
146145
| 'StartAudioRecordingButton'
147146
| 'uploadNewFile'
@@ -206,7 +205,6 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
206205
Reply,
207206
resetInput,
208207
SendButton,
209-
sending,
210208
sendMessage,
211209
showPollCreationDialog,
212210
ShowThreadMessageInChannelButton,
@@ -748,9 +746,7 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
748746
<CooldownTimer seconds={cooldownRemainingSeconds} />
749747
) : (
750748
<View style={[styles.sendButtonContainer, sendButtonContainer]}>
751-
<SendButton
752-
disabled={sending.current || !hasSendableData || (!!command && !isOnline)}
753-
/>
749+
<SendButton disabled={!hasSendableData || (!!command && !isOnline)} />
754750
</View>
755751
)
756752
) : null}
@@ -830,10 +826,10 @@ const areEqual = (
830826
audioRecordingEnabled: prevAsyncMessagesEnabled,
831827
channel: prevChannel,
832828
closePollCreationDialog: prevClosePollCreationDialog,
829+
cooldownEndsAt: prevCooldownEndsAt,
833830
editing: prevEditing,
834831
isOnline: prevIsOnline,
835832
openPollCreationDialog: prevOpenPollCreationDialog,
836-
sending: prevSending,
837833
showPollCreationDialog: prevShowPollCreationDialog,
838834
t: prevT,
839835
thread: prevThread,
@@ -847,10 +843,10 @@ const areEqual = (
847843
audioRecordingEnabled: nextAsyncMessagesEnabled,
848844
channel: nextChannel,
849845
closePollCreationDialog: nextClosePollCreationDialog,
846+
cooldownEndsAt: nextCooldownEndsAt,
850847
editing: nextEditing,
851848
isOnline: nextIsOnline,
852849
openPollCreationDialog: nextOpenPollCreationDialog,
853-
sending: nextSending,
854850
showPollCreationDialog: nextShowPollCreationDialog,
855851
t: nextT,
856852
thread: nextThread,
@@ -909,13 +905,13 @@ const areEqual = (
909905
return false;
910906
}
911907

912-
const sendingEqual = prevSending.current === nextSending.current;
913-
if (!sendingEqual) {
908+
const isOnlineEqual = prevIsOnline === nextIsOnline;
909+
if (!isOnlineEqual) {
914910
return false;
915911
}
916912

917-
const isOnlineEqual = prevIsOnline === nextIsOnline;
918-
if (!isOnlineEqual) {
913+
const cooldownEndsAtEqual = prevCooldownEndsAt === nextCooldownEndsAt;
914+
if (!cooldownEndsAtEqual) {
919915
return false;
920916
}
921917

@@ -992,7 +988,6 @@ export const MessageInput = (props: MessageInputProps) => {
992988
openPollCreationDialog,
993989
resetInput,
994990
SendButton,
995-
sending,
996991
sendMessage,
997992
SendMessageDisallowedIndicator,
998993
showPollCreationDialog,
@@ -1057,7 +1052,6 @@ export const MessageInput = (props: MessageInputProps) => {
10571052
Reply,
10581053
resetInput,
10591054
SendButton,
1060-
sending,
10611055
sendMessage,
10621056
SendMessageDisallowedIndicator,
10631057
showPollCreationDialog,

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ export type LocalMessageInputContext = {
9393
pickAndUploadImageFromNativePicker: () => Promise<void>;
9494
pickFile: () => Promise<void>;
9595
resetInput: (pendingAttachments?: Attachment[]) => void;
96-
selectedPicker: string | undefined;
97-
sending: React.MutableRefObject<boolean>;
9896
sendMessage: (params?: { customMessageData?: Partial<Message> }) => Promise<void>;
9997
sendThreadMessageInChannel: boolean;
10098
/**
@@ -368,7 +366,6 @@ export const MessageInputProvider = ({
368366
const { thread } = useThreadContext();
369367
const { t } = useTranslationContext();
370368
const inputBoxRef = useRef<TextInput | null>(null);
371-
const sending = useRef(false);
372369

373370
const [sendThreadMessageInChannel, setSendThreadMessageInChannel] = useState(false);
374371
const [showPollCreationDialog, setShowPollCreationDialog] = useState(false);
@@ -528,8 +525,8 @@ export const MessageInputProvider = ({
528525
}
529526
}, [closeAttachmentPicker, openAttachmentPicker, selectedPicker]);
530527

531-
const resetInput = useStableCallback(async () => {
532-
await messageComposer.clear();
528+
const resetInput = useStableCallback(() => {
529+
messageComposer.clear();
533530
/**
534531
* If the MediaLibrary is available, reset the selected files and images
535532
*/
@@ -544,12 +541,6 @@ export const MessageInputProvider = ({
544541
});
545542

546543
const sendMessage = useStableCallback(async () => {
547-
if (sending.current) {
548-
return;
549-
}
550-
551-
sending.current = true;
552-
553544
startCooldown();
554545

555546
if (inputBoxRef.current) {
@@ -562,16 +553,14 @@ export const MessageInputProvider = ({
562553

563554
if (editedMessage && editedMessage.type !== 'error') {
564555
try {
565-
value.clearEditingState();
556+
resetInput();
566557
await value.editMessage({ localMessage, options: sendOptions });
567-
await resetInput();
568558
} catch (error) {
569-
console.log('Failed to edit message', error);
559+
console.log('Failed to edit message:', error);
570560
}
571-
572-
sending.current = false;
573561
} else {
574562
try {
563+
resetInput();
575564
await value.sendMessage({
576565
localMessage: {
577566
...localMessage,
@@ -583,12 +572,8 @@ export const MessageInputProvider = ({
583572
},
584573
options: sendOptions,
585574
});
586-
587-
sending.current = false;
588-
await resetInput();
589575
} catch (error) {
590-
sending.current = false;
591-
console.log('Failed to send message', error);
576+
console.log('Failed to send message:', error);
592577
}
593578
}
594579
});
@@ -637,8 +622,6 @@ export const MessageInputProvider = ({
637622
pickAndUploadImageFromNativePicker,
638623
pickFile,
639624
resetInput,
640-
selectedPicker,
641-
sending,
642625
sendThreadMessageInChannel,
643626
setInputBoxRef,
644627
setSendThreadMessageInChannel,

package/src/contexts/messageInputContext/hooks/useCreateMessageInputContext.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ export const useCreateMessageInputContext = ({
5555
pickAndUploadImageFromNativePicker,
5656
pickFile,
5757
resetInput,
58-
selectedPicker,
5958
SendButton,
60-
sending,
6159
sendMessage,
6260
SendMessageDisallowedIndicator,
6361
sendThreadMessageInChannel,
@@ -130,9 +128,7 @@ export const useCreateMessageInputContext = ({
130128
pickAndUploadImageFromNativePicker,
131129
pickFile,
132130
resetInput,
133-
selectedPicker,
134131
SendButton,
135-
sending,
136132
sendMessage,
137133
SendMessageDisallowedIndicator,
138134
sendThreadMessageInChannel,
@@ -153,7 +149,6 @@ export const useCreateMessageInputContext = ({
153149
cooldownEndsAt,
154150
editingdep,
155151
isCommandUIEnabled,
156-
selectedPicker,
157152
sendThreadMessageInChannel,
158153
threadId,
159154
showPollCreationDialog,

0 commit comments

Comments
 (0)