Skip to content

Commit 570b872

Browse files
authored
fix: use composer for show thread message in channel checkbox (#3122)
* fix: use composer for show thread message in channel checkbox * fix: how threadList prop is passed
1 parent df2e996 commit 570b872

File tree

4 files changed

+54
-95
lines changed

4 files changed

+54
-95
lines changed

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,13 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
222222
isOnline,
223223
members,
224224
Reply,
225+
threadList,
225226
SendButton,
226227
sendMessage,
227228
showPollCreationDialog,
228229
ShowThreadMessageInChannelButton,
229230
StartAudioRecordingButton,
230231
StopMessageStreamingButton,
231-
threadList,
232232
watchers,
233233
} = props;
234234

Lines changed: 46 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,35 @@
1-
import React from 'react';
2-
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
1+
import React, { useCallback } from 'react';
2+
import { Pressable, StyleSheet, Text, View } from 'react-native';
33

4-
import {
5-
MessageInputContextValue,
6-
useMessageInputContext,
7-
} from '../../contexts/messageInputContext/MessageInputContext';
4+
import { MessageComposerState } from 'stream-chat';
5+
6+
import { ChannelContextValue } from '../../contexts/channelContext/ChannelContext';
7+
import { useMessageComposer } from '../../contexts/messageInputContext/hooks/useMessageComposer';
88
import { useTheme } from '../../contexts/themeContext/ThemeContext';
99
import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext';
1010
import {
1111
TranslationContextValue,
1212
useTranslationContext,
1313
} from '../../contexts/translationContext/TranslationContext';
14+
import { useStateStore } from '../../hooks/useStateStore';
1415
import { Check } from '../../icons';
1516

16-
const styles = StyleSheet.create({
17-
checkBox: {
18-
alignItems: 'center',
19-
borderRadius: 3,
20-
borderWidth: 2,
21-
height: 16,
22-
justifyContent: 'center',
23-
width: 16,
24-
},
25-
container: {
26-
flexDirection: 'row',
27-
marginHorizontal: 2,
28-
marginTop: 8,
29-
},
30-
innerContainer: {
31-
flexDirection: 'row',
32-
},
33-
text: {
34-
fontSize: 13,
35-
marginLeft: 12,
36-
},
17+
const stateSelector = (state: MessageComposerState) => ({
18+
showReplyInChannel: state.showReplyInChannel,
3719
});
3820

3921
export type ShowThreadMessageInChannelButtonWithContextProps = Pick<
40-
MessageInputContextValue,
41-
'sendThreadMessageInChannel' | 'setSendThreadMessageInChannel'
22+
ThreadContextValue,
23+
'allowThreadMessagesInChannel'
4224
> &
43-
Pick<ThreadContextValue, 'allowThreadMessagesInChannel'> &
44-
Pick<TranslationContextValue, 't'> & {
45-
threadList?: boolean;
46-
};
25+
Pick<TranslationContextValue, 't'> & { threadList?: ChannelContextValue['threadList'] };
4726

4827
export const ShowThreadMessageInChannelButtonWithContext = (
4928
props: ShowThreadMessageInChannelButtonWithContextProps,
5029
) => {
51-
const {
52-
allowThreadMessagesInChannel,
53-
sendThreadMessageInChannel,
54-
setSendThreadMessageInChannel,
55-
t,
56-
threadList,
57-
} = props;
30+
const { allowThreadMessagesInChannel, t, threadList } = props;
31+
const messageComposer = useMessageComposer();
32+
const { showReplyInChannel } = useStateStore(messageComposer.state, stateSelector);
5833

5934
const {
6035
theme: {
@@ -72,20 +47,22 @@ export const ShowThreadMessageInChannelButtonWithContext = (
7247
},
7348
} = useTheme();
7449

50+
const onPressHandler = useCallback(() => {
51+
messageComposer.toggleShowReplyInChannel();
52+
}, [messageComposer]);
53+
7554
if (!threadList || !allowThreadMessagesInChannel) {
7655
return null;
7756
}
7857

7958
return (
8059
<View style={[styles.container, container]} testID='show-thread-message-in-channel-button'>
81-
<TouchableOpacity
82-
onPress={() => setSendThreadMessageInChannel((prevSendInChannel) => !prevSendInChannel)}
83-
>
60+
<Pressable onPress={onPressHandler} style={({ pressed }) => ({ opacity: pressed ? 0.8 : 1 })}>
8461
<View style={[styles.innerContainer, innerContainer]}>
8562
<View
8663
style={[
8764
styles.checkBox,
88-
sendThreadMessageInChannel
65+
showReplyInChannel
8966
? {
9067
backgroundColor: accent_blue,
9168
borderColor: accent_blue,
@@ -94,15 +71,13 @@ export const ShowThreadMessageInChannelButtonWithContext = (
9471
: { borderColor: grey, ...checkBoxInactive },
9572
]}
9673
>
97-
{sendThreadMessageInChannel && (
98-
<Check height={16} pathFill={white} width={16} {...check} />
99-
)}
74+
{showReplyInChannel && <Check height={16} pathFill={white} width={16} {...check} />}
10075
</View>
10176
<Text style={[styles.text, { color: grey }, text]}>
10277
{t<string>('Also send to channel')}
10378
</Text>
10479
</View>
105-
</TouchableOpacity>
80+
</Pressable>
10681
</View>
10782
);
10883
};
@@ -113,13 +88,11 @@ const areEqual = (
11388
) => {
11489
const {
11590
allowThreadMessagesInChannel: prevAllowThreadMessagesInChannel,
116-
sendThreadMessageInChannel: prevSendThreadMessageInChannel,
11791
t: prevT,
11892
threadList: prevThreadList,
11993
} = prevProps;
12094
const {
12195
allowThreadMessagesInChannel: nextAllowThreadMessagesInChannel,
122-
sendThreadMessageInChannel: nexSendThreadMessageInChannel,
12396
t: nextT,
12497
threadList: nextThreadList,
12598
} = nextProps;
@@ -129,12 +102,6 @@ const areEqual = (
129102
return false;
130103
}
131104

132-
const sendThreadMessageInChannelEqual =
133-
prevSendThreadMessageInChannel === nexSendThreadMessageInChannel;
134-
if (!sendThreadMessageInChannelEqual) {
135-
return false;
136-
}
137-
138105
const threadListEqual = prevThreadList === nextThreadList;
139106
if (!threadListEqual) {
140107
return false;
@@ -160,14 +127,11 @@ export type ShowThreadMessageInChannelButtonProps =
160127
export const ShowThreadMessageInChannelButton = (props: ShowThreadMessageInChannelButtonProps) => {
161128
const { t } = useTranslationContext();
162129
const { allowThreadMessagesInChannel } = useThreadContext();
163-
const { sendThreadMessageInChannel, setSendThreadMessageInChannel } = useMessageInputContext();
164130

165131
return (
166132
<MemoizedShowThreadMessageInChannelButton
167133
{...{
168134
allowThreadMessagesInChannel,
169-
sendThreadMessageInChannel,
170-
setSendThreadMessageInChannel,
171135
t,
172136
}}
173137
{...props}
@@ -177,3 +141,26 @@ export const ShowThreadMessageInChannelButton = (props: ShowThreadMessageInChann
177141

178142
ShowThreadMessageInChannelButton.displayName =
179143
'ShowThreadMessageInChannelButton{messageInput{showThreadMessageInChannelButton}}';
144+
145+
const styles = StyleSheet.create({
146+
checkBox: {
147+
alignItems: 'center',
148+
borderRadius: 3,
149+
borderWidth: 2,
150+
height: 16,
151+
justifyContent: 'center',
152+
width: 16,
153+
},
154+
container: {
155+
flexDirection: 'row',
156+
marginHorizontal: 2,
157+
marginTop: 8,
158+
},
159+
innerContainer: {
160+
flexDirection: 'row',
161+
},
162+
text: {
163+
fontSize: 13,
164+
marginLeft: 12,
165+
},
166+
});

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, {
2-
LegacyRef,
32
PropsWithChildren,
3+
Ref,
44
useCallback,
55
useContext,
66
useEffect,
@@ -12,7 +12,6 @@ import { Alert, Keyboard, Linking, TextInput, TextInputProps } from 'react-nativ
1212
import { BottomSheetHandleProps } from '@gorhom/bottom-sheet';
1313
import {
1414
LocalMessage,
15-
Message,
1615
MessageComposer,
1716
SendMessageOptions,
1817
StreamChat,
@@ -85,7 +84,7 @@ export type LocalMessageInputContext = {
8584
/** The time at which the active cooldown will end */
8685
cooldownEndsAt: Date;
8786

88-
inputBoxRef: React.MutableRefObject<TextInput | null>;
87+
inputBoxRef: React.RefObject<TextInput | null>;
8988
openAttachmentPicker: () => void;
9089
openFilePicker: () => void;
9190
/**
@@ -94,13 +93,11 @@ export type LocalMessageInputContext = {
9493
pickAndUploadImageFromNativePicker: () => Promise<void>;
9594
pickFile: () => Promise<void>;
9695
selectedPicker?: 'images';
97-
sendMessage: (params?: { customMessageData?: Partial<Message> }) => Promise<void>;
98-
sendThreadMessageInChannel: boolean;
96+
sendMessage: () => Promise<void>;
9997
/**
10098
* Ref callback to set reference on input box
10199
*/
102-
setInputBoxRef: LegacyRef<TextInput> | undefined;
103-
setSendThreadMessageInChannel: React.Dispatch<React.SetStateAction<boolean>>;
100+
setInputBoxRef: Ref<TextInput> | undefined;
104101
/**
105102
* Function for taking a photo and uploading it
106103
*/
@@ -435,7 +432,6 @@ export const MessageInputProvider = ({
435432
const { t } = useTranslationContext();
436433
const inputBoxRef = useRef<TextInput | null>(null);
437434

438-
const [sendThreadMessageInChannel, setSendThreadMessageInChannel] = useState(false);
439435
const [showPollCreationDialog, setShowPollCreationDialog] = useState(false);
440436

441437
const defaultOpenPollCreationDialog = useCallback(() => setShowPollCreationDialog(true), []);
@@ -449,11 +445,6 @@ export const MessageInputProvider = ({
449445
const { attachmentManager, editedMessage } = messageComposer;
450446
const { availableUploadSlots } = useAttachmentManagerState();
451447

452-
const threadId = thread?.id;
453-
useEffect(() => {
454-
setSendThreadMessageInChannel(false);
455-
}, [threadId]);
456-
457448
/**
458449
* These are the RN SDK specific middlewares that are added to the message composer to provide the default behaviour.
459450
* TODO: Discuss and decide if we provide them by default in the SDK or leave it to the user to add them if they want the feature.
@@ -634,14 +625,8 @@ export const MessageInputProvider = ({
634625
messageComposer.clear();
635626
}
636627
await value.sendMessage({
637-
localMessage: {
638-
...localMessage,
639-
show_in_channel: sendThreadMessageInChannel || undefined,
640-
},
641-
message: {
642-
...message,
643-
show_in_channel: sendThreadMessageInChannel || undefined,
644-
},
628+
localMessage,
629+
message,
645630
options: sendOptions,
646631
});
647632
} catch (error) {
@@ -693,9 +678,7 @@ export const MessageInputProvider = ({
693678
openFilePicker: pickFile,
694679
pickAndUploadImageFromNativePicker,
695680
pickFile,
696-
sendThreadMessageInChannel,
697681
setInputBoxRef,
698-
setSendThreadMessageInChannel,
699682
takeAndUploadImage,
700683
thread,
701684
toggleAttachmentPicker,

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,8 @@ export const useCreateMessageInputContext = ({
6464
SendButton,
6565
sendMessage,
6666
SendMessageDisallowedIndicator,
67-
sendThreadMessageInChannel,
6867
setInputBoxRef,
6968
setInputRef,
70-
setSendThreadMessageInChannel,
7169
showPollCreationDialog,
7270
ShowThreadMessageInChannelButton,
7371
StartAudioRecordingButton,
@@ -143,10 +141,8 @@ export const useCreateMessageInputContext = ({
143141
SendButton,
144142
sendMessage,
145143
SendMessageDisallowedIndicator,
146-
sendThreadMessageInChannel,
147144
setInputBoxRef,
148145
setInputRef,
149-
setSendThreadMessageInChannel,
150146
showPollCreationDialog,
151147
ShowThreadMessageInChannelButton,
152148
StartAudioRecordingButton,
@@ -158,14 +154,7 @@ export const useCreateMessageInputContext = ({
158154
VideoRecorderSelectorIcon,
159155
}),
160156
// eslint-disable-next-line react-hooks/exhaustive-deps
161-
[
162-
cooldownEndsAt,
163-
isCommandUIEnabled,
164-
sendThreadMessageInChannel,
165-
threadId,
166-
showPollCreationDialog,
167-
selectedPicker,
168-
],
157+
[cooldownEndsAt, isCommandUIEnabled, threadId, showPollCreationDialog, selectedPicker],
169158
);
170159

171160
return messageInputContext;

0 commit comments

Comments
 (0)