Skip to content

Commit 3dcc0ce

Browse files
committed
fix: use composer for show thread message in channel checkbox
1 parent 3c2aa69 commit 3dcc0ce

File tree

4 files changed

+59
-95
lines changed

4 files changed

+59
-95
lines changed

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
228228
ShowThreadMessageInChannelButton,
229229
StartAudioRecordingButton,
230230
StopMessageStreamingButton,
231-
threadList,
232231
watchers,
233232
} = props;
234233

@@ -589,7 +588,7 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
589588
</>
590589
)}
591590
</View>
592-
<ShowThreadMessageInChannelButton threadList={threadList} />
591+
<ShowThreadMessageInChannelButton />
593592
</View>
594593

595594
<View style={[styles.suggestionsListContainer, { bottom: height }, suggestionListContainer]}>
Lines changed: 51 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,39 @@
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';
3+
4+
import { MessageComposerState } from 'stream-chat';
35

46
import {
5-
MessageInputContextValue,
6-
useMessageInputContext,
7-
} from '../../contexts/messageInputContext/MessageInputContext';
7+
ChannelContextValue,
8+
useChannelContext,
9+
} from '../../contexts/channelContext/ChannelContext';
10+
import { useMessageComposer } from '../../contexts/messageInputContext/hooks/useMessageComposer';
811
import { useTheme } from '../../contexts/themeContext/ThemeContext';
912
import { ThreadContextValue, useThreadContext } from '../../contexts/threadContext/ThreadContext';
1013
import {
1114
TranslationContextValue,
1215
useTranslationContext,
1316
} from '../../contexts/translationContext/TranslationContext';
17+
import { useStateStore } from '../../hooks/useStateStore';
1418
import { Check } from '../../icons';
1519

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-
},
20+
const stateSelector = (state: MessageComposerState) => ({
21+
showReplyInChannel: state.showReplyInChannel,
3722
});
3823

3924
export type ShowThreadMessageInChannelButtonWithContextProps = Pick<
40-
MessageInputContextValue,
41-
'sendThreadMessageInChannel' | 'setSendThreadMessageInChannel'
25+
ThreadContextValue,
26+
'allowThreadMessagesInChannel'
4227
> &
43-
Pick<ThreadContextValue, 'allowThreadMessagesInChannel'> &
44-
Pick<TranslationContextValue, 't'> & {
45-
threadList?: boolean;
46-
};
28+
Pick<TranslationContextValue, 't'> &
29+
Pick<ChannelContextValue, 'threadList'>;
4730

4831
export const ShowThreadMessageInChannelButtonWithContext = (
4932
props: ShowThreadMessageInChannelButtonWithContextProps,
5033
) => {
51-
const {
52-
allowThreadMessagesInChannel,
53-
sendThreadMessageInChannel,
54-
setSendThreadMessageInChannel,
55-
t,
56-
threadList,
57-
} = props;
34+
const { allowThreadMessagesInChannel, t, threadList } = props;
35+
const messageComposer = useMessageComposer();
36+
const { showReplyInChannel } = useStateStore(messageComposer.state, stateSelector);
5837

5938
const {
6039
theme: {
@@ -72,20 +51,22 @@ export const ShowThreadMessageInChannelButtonWithContext = (
7251
},
7352
} = useTheme();
7453

54+
const onPressHandler = useCallback(() => {
55+
messageComposer.toggleShowReplyInChannel();
56+
}, [messageComposer]);
57+
7558
if (!threadList || !allowThreadMessagesInChannel) {
7659
return null;
7760
}
7861

7962
return (
8063
<View style={[styles.container, container]} testID='show-thread-message-in-channel-button'>
81-
<TouchableOpacity
82-
onPress={() => setSendThreadMessageInChannel((prevSendInChannel) => !prevSendInChannel)}
83-
>
64+
<Pressable onPress={onPressHandler} style={({ pressed }) => ({ opacity: pressed ? 0.8 : 1 })}>
8465
<View style={[styles.innerContainer, innerContainer]}>
8566
<View
8667
style={[
8768
styles.checkBox,
88-
sendThreadMessageInChannel
69+
showReplyInChannel
8970
? {
9071
backgroundColor: accent_blue,
9172
borderColor: accent_blue,
@@ -94,15 +75,13 @@ export const ShowThreadMessageInChannelButtonWithContext = (
9475
: { borderColor: grey, ...checkBoxInactive },
9576
]}
9677
>
97-
{sendThreadMessageInChannel && (
98-
<Check height={16} pathFill={white} width={16} {...check} />
99-
)}
78+
{showReplyInChannel && <Check height={16} pathFill={white} width={16} {...check} />}
10079
</View>
10180
<Text style={[styles.text, { color: grey }, text]}>
10281
{t<string>('Also send to channel')}
10382
</Text>
10483
</View>
105-
</TouchableOpacity>
84+
</Pressable>
10685
</View>
10786
);
10887
};
@@ -113,13 +92,11 @@ const areEqual = (
11392
) => {
11493
const {
11594
allowThreadMessagesInChannel: prevAllowThreadMessagesInChannel,
116-
sendThreadMessageInChannel: prevSendThreadMessageInChannel,
11795
t: prevT,
11896
threadList: prevThreadList,
11997
} = prevProps;
12098
const {
12199
allowThreadMessagesInChannel: nextAllowThreadMessagesInChannel,
122-
sendThreadMessageInChannel: nexSendThreadMessageInChannel,
123100
t: nextT,
124101
threadList: nextThreadList,
125102
} = nextProps;
@@ -129,12 +106,6 @@ const areEqual = (
129106
return false;
130107
}
131108

132-
const sendThreadMessageInChannelEqual =
133-
prevSendThreadMessageInChannel === nexSendThreadMessageInChannel;
134-
if (!sendThreadMessageInChannelEqual) {
135-
return false;
136-
}
137-
138109
const threadListEqual = prevThreadList === nextThreadList;
139110
if (!threadListEqual) {
140111
return false;
@@ -158,17 +129,16 @@ export type ShowThreadMessageInChannelButtonProps =
158129
Partial<ShowThreadMessageInChannelButtonWithContextProps>;
159130

160131
export const ShowThreadMessageInChannelButton = (props: ShowThreadMessageInChannelButtonProps) => {
132+
const { threadList } = useChannelContext();
161133
const { t } = useTranslationContext();
162134
const { allowThreadMessagesInChannel } = useThreadContext();
163-
const { sendThreadMessageInChannel, setSendThreadMessageInChannel } = useMessageInputContext();
164135

165136
return (
166137
<MemoizedShowThreadMessageInChannelButton
167138
{...{
168139
allowThreadMessagesInChannel,
169-
sendThreadMessageInChannel,
170-
setSendThreadMessageInChannel,
171140
t,
141+
threadList,
172142
}}
173143
{...props}
174144
/>
@@ -177,3 +147,26 @@ export const ShowThreadMessageInChannelButton = (props: ShowThreadMessageInChann
177147

178148
ShowThreadMessageInChannelButton.displayName =
179149
'ShowThreadMessageInChannelButton{messageInput{showThreadMessageInChannelButton}}';
150+
151+
const styles = StyleSheet.create({
152+
checkBox: {
153+
alignItems: 'center',
154+
borderRadius: 3,
155+
borderWidth: 2,
156+
height: 16,
157+
justifyContent: 'center',
158+
width: 16,
159+
},
160+
container: {
161+
flexDirection: 'row',
162+
marginHorizontal: 2,
163+
marginTop: 8,
164+
},
165+
innerContainer: {
166+
flexDirection: 'row',
167+
},
168+
text: {
169+
fontSize: 13,
170+
marginLeft: 12,
171+
},
172+
});

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,
@@ -15,7 +15,6 @@ import {
1515
// createCommandStringExtractionMiddleware,
1616
// createDraftCommandInjectionMiddleware,
1717
LocalMessage,
18-
Message,
1918
MessageComposer,
2019
SendMessageOptions,
2120
StreamChat,
@@ -89,7 +88,7 @@ export type LocalMessageInputContext = {
8988
/** The time at which the active cooldown will end */
9089
cooldownEndsAt: Date;
9190

92-
inputBoxRef: React.MutableRefObject<TextInput | null>;
91+
inputBoxRef: React.RefObject<TextInput | null>;
9392
openAttachmentPicker: () => void;
9493
openFilePicker: () => void;
9594
/**
@@ -98,13 +97,11 @@ export type LocalMessageInputContext = {
9897
pickAndUploadImageFromNativePicker: () => Promise<void>;
9998
pickFile: () => Promise<void>;
10099
selectedPicker?: 'images';
101-
sendMessage: (params?: { customMessageData?: Partial<Message> }) => Promise<void>;
102-
sendThreadMessageInChannel: boolean;
100+
sendMessage: () => Promise<void>;
103101
/**
104102
* Ref callback to set reference on input box
105103
*/
106-
setInputBoxRef: LegacyRef<TextInput> | undefined;
107-
setSendThreadMessageInChannel: React.Dispatch<React.SetStateAction<boolean>>;
104+
setInputBoxRef: Ref<TextInput> | undefined;
108105
/**
109106
* Function for taking a photo and uploading it
110107
*/
@@ -439,7 +436,6 @@ export const MessageInputProvider = ({
439436
const { t } = useTranslationContext();
440437
const inputBoxRef = useRef<TextInput | null>(null);
441438

442-
const [sendThreadMessageInChannel, setSendThreadMessageInChannel] = useState(false);
443439
const [showPollCreationDialog, setShowPollCreationDialog] = useState(false);
444440

445441
const defaultOpenPollCreationDialog = useCallback(() => setShowPollCreationDialog(true), []);
@@ -453,11 +449,6 @@ export const MessageInputProvider = ({
453449
const { attachmentManager, editedMessage } = messageComposer;
454450
const { availableUploadSlots } = useAttachmentManagerState();
455451

456-
const threadId = thread?.id;
457-
useEffect(() => {
458-
setSendThreadMessageInChannel(false);
459-
}, [threadId]);
460-
461452
/**
462453
* These are the RN SDK specific middlewares that are added to the message composer to provide the default behaviour.
463454
* 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.
@@ -638,14 +629,8 @@ export const MessageInputProvider = ({
638629
messageComposer.clear();
639630
}
640631
await value.sendMessage({
641-
localMessage: {
642-
...localMessage,
643-
show_in_channel: sendThreadMessageInChannel || undefined,
644-
},
645-
message: {
646-
...message,
647-
show_in_channel: sendThreadMessageInChannel || undefined,
648-
},
632+
localMessage,
633+
message,
649634
options: sendOptions,
650635
});
651636
} catch (error) {
@@ -697,9 +682,7 @@ export const MessageInputProvider = ({
697682
openFilePicker: pickFile,
698683
pickAndUploadImageFromNativePicker,
699684
pickFile,
700-
sendThreadMessageInChannel,
701685
setInputBoxRef,
702-
setSendThreadMessageInChannel,
703686
takeAndUploadImage,
704687
thread,
705688
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)