-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathChannelScreen.tsx
More file actions
179 lines (160 loc) · 5.21 KB
/
ChannelScreen.tsx
File metadata and controls
179 lines (160 loc) · 5.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { useCallback, useEffect, useState } from 'react';
import type { Channel as StreamChatChannel } from 'stream-chat';
import { RouteProp, useFocusEffect, useNavigation } from '@react-navigation/native';
import {
Channel,
ChannelAvatar,
MessageInput,
MessageList,
ThreadContextValue,
useAttachmentPickerContext,
useChannelPreviewDisplayName,
useChatContext,
useTheme,
useTypingString,
AITypingIndicatorView,
} from 'stream-chat-react-native';
import { Platform, StyleSheet, View } from 'react-native';
import type { StackNavigationProp } from '@react-navigation/stack';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { useAppContext } from '../context/AppContext';
import { ScreenHeader } from '../components/ScreenHeader';
import { TouchableOpacity } from 'react-native-gesture-handler';
import { useChannelMembersStatus } from '../hooks/useChannelMembersStatus';
import type { StackNavigatorParamList, StreamChatGenerics } from '../types';
import { NetworkDownIndicator } from '../components/NetworkDownIndicator';
const styles = StyleSheet.create({
flex: { flex: 1 },
});
export type ChannelScreenNavigationProp = StackNavigationProp<
StackNavigatorParamList,
'ChannelScreen'
>;
export type ChannelScreenRouteProp = RouteProp<StackNavigatorParamList, 'ChannelScreen'>;
export type ChannelScreenProps = {
navigation: ChannelScreenNavigationProp;
route: ChannelScreenRouteProp;
};
export type ChannelHeaderProps = {
channel: StreamChatChannel<StreamChatGenerics>;
};
const ChannelHeader: React.FC<ChannelHeaderProps> = ({ channel }) => {
const { closePicker } = useAttachmentPickerContext();
const membersStatus = useChannelMembersStatus(channel);
const displayName = useChannelPreviewDisplayName(channel, 30);
const { isOnline } = useChatContext();
const { chatClient } = useAppContext();
const navigation = useNavigation<ChannelScreenNavigationProp>();
const typing = useTypingString();
if (!channel || !chatClient) {
return null;
}
const isOneOnOneConversation =
channel &&
Object.values(channel.state.members).length === 2 &&
channel.id?.indexOf('!members-') === 0;
return (
<ScreenHeader
onBack={() => {
if (!navigation.canGoBack()) {
// if no previous screen was present in history, go to the list screen
// this can happen when opened through push notification
navigation.reset({ index: 0, routes: [{ name: 'MessagingScreen' }] });
} else {
navigation.goBack();
}
}}
// eslint-disable-next-line react/no-unstable-nested-components
RightContent={() => (
<TouchableOpacity
onPress={() => {
closePicker();
if (isOneOnOneConversation) {
navigation.navigate('OneOnOneChannelDetailScreen', {
channel,
});
} else {
navigation.navigate('GroupChannelDetailsScreen', {
channel,
});
}
}}
>
<ChannelAvatar channel={channel} />
</TouchableOpacity>
)}
showUnreadCountBadge
Subtitle={isOnline ? undefined : NetworkDownIndicator}
subtitleText={typing ? typing : membersStatus}
titleText={displayName}
/>
);
};
// Either provide channel or channelId.
export const ChannelScreen: React.FC<ChannelScreenProps> = ({
route: {
params: { channel: channelFromProp, channelId, messageId },
},
}) => {
const { chatClient } = useAppContext();
const navigation = useNavigation();
const { bottom } = useSafeAreaInsets();
const {
theme: {
colors: { white },
},
} = useTheme();
const [channel, setChannel] = useState<StreamChatChannel<StreamChatGenerics> | undefined>(
channelFromProp,
);
const [selectedThread, setSelectedThread] =
useState<ThreadContextValue<StreamChatGenerics>['thread']>();
useEffect(() => {
const initChannel = async () => {
if (!chatClient || !channelId) {
return;
}
const newChannel = chatClient?.channel('messaging', channelId);
if (!newChannel?.initialized) {
await newChannel?.watch();
}
setChannel(newChannel);
};
initChannel();
}, [channelId, chatClient]);
useFocusEffect(() => {
setSelectedThread(undefined);
});
const onThreadSelect = useCallback((thread) => {
setSelectedThread(thread);
navigation.navigate('ThreadScreen', {
channel,
thread,
});
}, [channel, navigation]);
if (!channel || !chatClient) {
return null;
}
return (
<View style={[styles.flex, { backgroundColor: white, paddingBottom: bottom }]}>
<Channel
audioRecordingEnabled={true}
channel={channel}
disableTypingIndicator
enforceUniqueReaction
initialScrollToFirstUnreadMessage
keyboardVerticalOffset={Platform.OS === 'ios' ? 0 : -300}
messageId={messageId}
NetworkDownIndicator={() => null}
thread={selectedThread}
>
<ChannelHeader channel={channel} />
<MessageList<StreamChatGenerics>
onThreadSelect={onThreadSelect}
/>
<AITypingIndicatorView channel={channel} />
<MessageInput />
</Channel>
</View>
);
};