Skip to content

Commit 03732df

Browse files
committed
fix: change how message composer config is set
1 parent 2892a3b commit 03732df

File tree

6 files changed

+78
-78
lines changed

6 files changed

+78
-78
lines changed

examples/TypeScriptMessaging/App.tsx

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,41 @@ type AppContextType = {
189189

190190
const AppContext = React.createContext({} as AppContextType);
191191

192+
const StackNavigator = () => {
193+
const { channel } = useContext(AppContext);
194+
195+
return (
196+
<Stack.Navigator
197+
initialRouteName='ChannelList'
198+
screenOptions={{
199+
headerTitleStyle: { alignSelf: 'center', fontWeight: 'bold' },
200+
}}
201+
>
202+
<Stack.Screen
203+
component={ChannelScreen}
204+
name='Channel'
205+
options={() => ({
206+
headerBackTitle: 'Back',
207+
headerRight: EmptyHeader,
208+
headerTitle: channel?.data?.name,
209+
})}
210+
/>
211+
<Stack.Screen
212+
component={ChannelListScreen}
213+
name='ChannelList'
214+
options={{ headerTitle: 'Channel List' }}
215+
/>
216+
<Stack.Screen
217+
component={ThreadScreen}
218+
name='Thread'
219+
options={() => ({ headerLeft: EmptyHeader })}
220+
/>
221+
</Stack.Navigator>
222+
);
223+
};
224+
192225
const App = () => {
193226
const theme = useStreamChatTheme();
194-
const { channel } = useContext(AppContext);
195227

196228
const chatClient = useCreateChatClient({
197229
apiKey,
@@ -206,32 +238,7 @@ const App = () => {
206238
return (
207239
<OverlayProvider i18nInstance={streami18n} value={{ style: theme }}>
208240
<Chat client={chatClient} i18nInstance={streami18n} enableOfflineSupport>
209-
<Stack.Navigator
210-
initialRouteName='ChannelList'
211-
screenOptions={{
212-
headerTitleStyle: { alignSelf: 'center', fontWeight: 'bold' },
213-
}}
214-
>
215-
<Stack.Screen
216-
component={ChannelScreen}
217-
name='Channel'
218-
options={() => ({
219-
headerBackTitle: 'Back',
220-
headerRight: EmptyHeader,
221-
headerTitle: channel?.data?.name,
222-
})}
223-
/>
224-
<Stack.Screen
225-
component={ChannelListScreen}
226-
name='ChannelList'
227-
options={{ headerTitle: 'Channel List' }}
228-
/>
229-
<Stack.Screen
230-
component={ThreadScreen}
231-
name='Thread'
232-
options={() => ({ headerLeft: EmptyHeader })}
233-
/>
234-
</Stack.Navigator>
241+
<StackNavigator />
235242
</Chat>
236243
</OverlayProvider>
237244
);

package/src/components/Channel/Channel.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
622622
loadingMoreRecent: loadingMoreRecentProp,
623623
markdownRules,
624624
markReadOnMount = true,
625-
maxNumberOfFiles = 10,
626625
maxTimeBetweenGroupedMessages,
627626
Message = MessageDefault,
628627
MessageActionList = MessageActionListDefault,
@@ -1781,7 +1780,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
17811780
InputEditingStateHeader,
17821781
InputReplyStateHeader,
17831782
isCommandUIEnabled,
1784-
maxNumberOfFiles,
17851783
MoreOptionsButton,
17861784
openPollCreationDialog,
17871785
SendButton,

package/src/components/Channel/hooks/useCreateInputMessageInputContext.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const useCreateInputMessageInputContext = ({
5151
InputEditingStateHeader,
5252
InputReplyStateHeader,
5353
isCommandUIEnabled,
54-
maxNumberOfFiles,
5554
MoreOptionsButton,
5655
openPollCreationDialog,
5756
SendButton,
@@ -119,7 +118,6 @@ export const useCreateInputMessageInputContext = ({
119118
InputEditingStateHeader,
120119
InputReplyStateHeader,
121120
isCommandUIEnabled,
122-
maxNumberOfFiles,
123121
MoreOptionsButton,
124122
openPollCreationDialog,
125123
SendButton,

package/src/components/MessageInput/MessageInput.tsx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ type MessageInputPropsWithContext = Partial<
144144
| 'CooldownTimer'
145145
| 'closeAttachmentPicker'
146146
| 'compressImageQuality'
147-
| 'doFileUploadRequest'
148147
| 'FileUploadPreview'
149148
| 'ImageUploadPreview'
150149
| 'Input'
@@ -158,7 +157,6 @@ type MessageInputPropsWithContext = Partial<
158157
| 'VideoRecorderSelectorIcon'
159158
| 'CommandInput'
160159
| 'InputReplyStateHeader'
161-
| 'maxNumberOfFiles'
162160
| 'SendButton'
163161
| 'ShowThreadMessageInChannelButton'
164162
| 'StartAudioRecordingButton'
@@ -210,7 +208,6 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
210208
cooldownEndsAt,
211209
CooldownTimer,
212210
CreatePollContent,
213-
doFileUploadRequest,
214211
editing,
215212
FileUploadPreview,
216213
ImageUploadPreview,
@@ -221,7 +218,6 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
221218
CommandInput,
222219
InputReplyStateHeader,
223220
isOnline,
224-
maxNumberOfFiles,
225221
members,
226222
Reply,
227223
SendButton,
@@ -235,7 +231,7 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
235231
} = props;
236232

237233
const messageComposer = useMessageComposer();
238-
const { attachmentManager, textComposer } = messageComposer;
234+
const { textComposer } = messageComposer;
239235
const { command, mentionedUsers, hasText } = useStateStore(
240236
textComposer.state,
241237
textComposerStateSelector,
@@ -271,15 +267,13 @@ const MessageInputWithContext = (props: MessageInputPropsWithContext) => {
271267

272268
const { seconds: cooldownRemainingSeconds } = useCountdown(cooldownEndsAt);
273269

274-
useEffect(() => {
275-
attachmentManager.maxNumberOfFilesPerMessage = maxNumberOfFiles;
276-
if (doFileUploadRequest) {
277-
attachmentManager.setCustomUploadFn(doFileUploadRequest);
278-
}
279-
280-
return closeAttachmentPicker;
281-
// eslint-disable-next-line react-hooks/exhaustive-deps
282-
}, []);
270+
// Close the attachment picker state when the component unmounts
271+
useEffect(
272+
() => () => {
273+
closeAttachmentPicker();
274+
},
275+
[closeAttachmentPicker],
276+
);
283277

284278
const editingExists = !!editing;
285279

@@ -799,7 +793,6 @@ export const MessageInput = (props: MessageInputProps) => {
799793
CooldownTimer,
800794
CreatePollContent,
801795
CreatePollIcon,
802-
doFileUploadRequest,
803796
FileSelectorIcon,
804797
FileUploadPreview,
805798
ImageSelectorIcon,
@@ -810,7 +803,6 @@ export const MessageInput = (props: MessageInputProps) => {
810803
InputEditingStateHeader,
811804
CommandInput,
812805
InputReplyStateHeader,
813-
maxNumberOfFiles,
814806
openPollCreationDialog,
815807
SendButton,
816808
sendMessage,
@@ -872,7 +864,6 @@ export const MessageInput = (props: MessageInputProps) => {
872864
CooldownTimer,
873865
CreatePollContent,
874866
CreatePollIcon,
875-
doFileUploadRequest,
876867
editing,
877868
FileSelectorIcon,
878869
FileUploadPreview,
@@ -884,7 +875,6 @@ export const MessageInput = (props: MessageInputProps) => {
884875
InputEditingStateHeader,
885876
InputReplyStateHeader,
886877
isOnline,
887-
maxNumberOfFiles,
888878
members,
889879
openPollCreationDialog,
890880
Reply,

package/src/contexts/messageInputContext/MessageInputContext.tsx

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { Alert, Keyboard, Linking, TextInput, TextInputProps } from 'react-nativ
1111

1212
import { BottomSheetHandleProps } from '@gorhom/bottom-sheet';
1313
import {
14-
createApplyCommandSettingsMiddleware,
1514
createCommandInjectionMiddleware,
15+
createCommandStringExtractionMiddleware,
1616
createDraftCommandInjectionMiddleware,
1717
LocalMessage,
1818
Message,
@@ -292,8 +292,6 @@ export type InputMessageInputContextValue = {
292292
isCommandUIEnabled?: boolean;
293293
CommandInput: React.ComponentType<CommandInputProps>;
294294
InputReplyStateHeader: React.ComponentType;
295-
/** Limit on allowed number of files to attach at a time. */
296-
maxNumberOfFiles: number;
297295
/**
298296
* Custom UI component for more options button.
299297
*
@@ -452,33 +450,44 @@ export const MessageInputProvider = ({
452450
setSendThreadMessageInChannel(false);
453451
}, [threadId]);
454452

453+
/**
454+
* These are the RN SDK specific middlewares that are added to the message composer to provide the default behaviour.
455+
* 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.
456+
*/
455457
useEffect(() => {
456-
if (!client) {
457-
return;
458+
if (value.doFileUploadRequest) {
459+
attachmentManager.setCustomUploadFn(value.doFileUploadRequest);
458460
}
459461

460-
client.setMessageComposerSetupFunction(({ composer }) => {
461-
isCommandUIEnabled &&
462-
composer.compositionMiddlewareExecutor.insert({
463-
middleware: [createCommandInjectionMiddleware(composer)],
464-
position: { after: 'stream-io/message-composer-middleware/attachments' },
465-
});
466-
enableOfflineSupport &&
467-
composer.compositionMiddlewareExecutor.replace([
468-
createAttachmentsCompositionMiddleware(composer),
469-
]);
470-
isCommandUIEnabled &&
471-
composer.draftCompositionMiddlewareExecutor.insert({
472-
middleware: [createDraftCommandInjectionMiddleware(composer)],
473-
position: { after: 'stream-io/message-composer-middleware/draft-attachments' },
474-
});
475-
isCommandUIEnabled &&
476-
composer.textComposer.middlewareExecutor.insert({
477-
middleware: [createApplyCommandSettingsMiddleware() as TextComposerMiddleware],
478-
position: { after: 'stream-io/text-composer/commands-middleware' },
479-
});
480-
});
481-
}, [client, isCommandUIEnabled, enableOfflineSupport]);
462+
if (isCommandUIEnabled) {
463+
messageComposer.compositionMiddlewareExecutor.insert({
464+
middleware: [createCommandInjectionMiddleware(messageComposer)],
465+
position: { after: 'stream-io/message-composer-middleware/attachments' },
466+
});
467+
468+
messageComposer.draftCompositionMiddlewareExecutor.insert({
469+
middleware: [createDraftCommandInjectionMiddleware(messageComposer)],
470+
position: { after: 'stream-io/message-composer-middleware/draft-attachments' },
471+
});
472+
473+
messageComposer.textComposer.middlewareExecutor.insert({
474+
middleware: [createCommandStringExtractionMiddleware() as TextComposerMiddleware],
475+
position: { after: 'stream-io/text-composer/commands-middleware' },
476+
});
477+
}
478+
479+
if (enableOfflineSupport) {
480+
messageComposer.compositionMiddlewareExecutor.replace([
481+
createAttachmentsCompositionMiddleware(messageComposer),
482+
]);
483+
}
484+
}, [
485+
value.doFileUploadRequest,
486+
isCommandUIEnabled,
487+
enableOfflineSupport,
488+
messageComposer,
489+
attachmentManager,
490+
]);
482491

483492
/**
484493
* Function for capturing a photo and uploading it

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export const useCreateMessageInputContext = ({
5454
InputButtons,
5555
InputEditingStateHeader,
5656
InputReplyStateHeader,
57-
maxNumberOfFiles,
5857
MoreOptionsButton,
5958
openAttachmentPicker,
6059
openFilePicker,
@@ -134,7 +133,6 @@ export const useCreateMessageInputContext = ({
134133
InputEditingStateHeader,
135134
InputReplyStateHeader,
136135
isCommandUIEnabled,
137-
maxNumberOfFiles,
138136
MoreOptionsButton,
139137
openAttachmentPicker,
140138
openFilePicker,

0 commit comments

Comments
 (0)