Skip to content

Commit 098df49

Browse files
committed
fix: create new channel bug
1 parent f060df9 commit 098df49

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

examples/SampleApp/src/components/NewDirectMessagingSendButton.tsx

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import {
1717
import { NewDirectMessagingScreenNavigationProp } from '../screens/NewDirectMessagingScreen';
1818

1919
import { StreamChatGenerics as LocalStreamChatGenerics } from '../types';
20+
import { useUserSearchContext } from '../context/UserSearchContext';
21+
import { useAppContext } from '../context/AppContext';
2022

2123
type NewDirectMessagingSendButtonPropsWithContext<
2224
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
@@ -97,24 +99,36 @@ export type SendButtonProps<
9799
* UI Component for send button in MessageInput component.
98100
*/
99101
export const NewDirectMessagingSendButton = (props: SendButtonProps<LocalStreamChatGenerics>) => {
102+
const { chatClient } = useAppContext();
100103
const navigation = useNavigation<NewDirectMessagingScreenNavigationProp>();
101104
const { channel } = useChannelContext<LocalStreamChatGenerics>();
105+
const { selectedUserIds, reset } = useUserSearchContext();
102106

103107
const { giphyActive, text } = useMessageInputContext<LocalStreamChatGenerics>();
104108

105109
const sendMessage = async () => {
106110
if (!channel) {
107111
return;
108112
}
113+
if (!chatClient || !chatClient.user) {
114+
return;
115+
}
116+
const members = [chatClient.user.id, ...selectedUserIds];
109117
channel.initialized = false;
110-
await channel.query({});
118+
const newChannel = chatClient.channel('messaging', {
119+
members,
120+
});
111121
try {
112-
await channel.sendMessage({ text });
122+
await newChannel.watch();
123+
await newChannel.sendMessage({ text });
113124
navigation.replace('ChannelScreen', {
114-
channelId: channel.id,
125+
channelId: newChannel.id,
115126
});
127+
reset();
116128
} catch (e) {
117-
Alert.alert('Error sending a message');
129+
if (e instanceof Error) {
130+
Alert.alert('Error sending a message', e.message);
131+
}
118132
}
119133
};
120134

0 commit comments

Comments
 (0)