-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathSharedGroupsScreen.tsx
More file actions
202 lines (180 loc) · 4.94 KB
/
SharedGroupsScreen.tsx
File metadata and controls
202 lines (180 loc) · 4.94 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import React, { useMemo } from 'react';
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { NavigationProp, RouteProp, useNavigation } from '@react-navigation/native';
import {
ChannelList,
ChannelListView,
ChannelListViewProps,
ChannelPreviewViewProps,
getChannelPreviewDisplayAvatar,
GroupAvatar,
useChannelPreviewDisplayName,
useChannelsContext,
useTheme,
Avatar,
getInitialsFromName,
} from 'stream-chat-react-native';
import { ScreenHeader } from '../components/ScreenHeader';
import { useAppContext } from '../context/AppContext';
import { Contacts } from '../icons/Contacts';
import type { StackNavigatorParamList } from '../types';
const styles = StyleSheet.create({
container: {
flex: 1,
},
emptyListContainer: {
alignItems: 'center',
flex: 1,
justifyContent: 'center',
},
emptyListSubtitle: {
marginTop: 8,
textAlign: 'center',
},
emptyListTitle: {
fontSize: 16,
marginTop: 10,
},
groupContainer: {
alignItems: 'center',
flexDirection: 'row',
},
nameText: {
fontWeight: '700',
marginLeft: 8,
},
previewContainer: {
alignItems: 'center',
borderBottomWidth: 1,
flexDirection: 'row',
justifyContent: 'space-between',
padding: 12,
},
});
type CustomPreviewProps = ChannelPreviewViewProps;
const CustomPreview: React.FC<CustomPreviewProps> = ({ channel }) => {
const { chatClient } = useAppContext();
const name = useChannelPreviewDisplayName(channel, 30);
const navigation = useNavigation<NavigationProp<StackNavigatorParamList, 'SharedGroupsScreen'>>();
const {
theme: {
colors: { black, grey, grey_whisper, white_snow },
},
} = useTheme();
const displayAvatar = getChannelPreviewDisplayAvatar(channel, chatClient);
const placeholder = useMemo(() => {
if (displayAvatar?.name) {
return <Text style={{ color: '#003179' }}>{getInitialsFromName(displayAvatar?.name)}</Text>;
} else {
return <Text style={{ color: '#003179' }}>?</Text>;
}
}, [displayAvatar.name]);
if (!chatClient) {
return null;
}
if (Object.keys(channel.state.members).length === 2) {
return null;
}
const switchToChannel = () => {
navigation.reset({
index: 1,
routes: [
{
name: 'MessagingScreen',
},
{
name: 'ChannelScreen',
params: {
channelId: channel.id,
},
},
],
});
};
return (
<TouchableOpacity
onPress={switchToChannel}
style={[
styles.previewContainer,
{
backgroundColor: white_snow,
borderBottomColor: grey_whisper,
},
]}
>
<View style={styles.groupContainer}>
{displayAvatar.images ? (
<GroupAvatar images={displayAvatar.images} names={displayAvatar.names} size={40} />
) : (
<Avatar imageUrl={displayAvatar.image} placeholder={placeholder} size={'lg'} />
)}
<Text style={[styles.nameText, { color: black }]}>{name}</Text>
</View>
<Text
style={{
color: grey,
}}
>
{Object.keys(channel.state.members).length} Members
</Text>
</TouchableOpacity>
);
};
const EmptyListComponent = () => {
const {
theme: {
colors: { black, grey, grey_gainsboro },
},
} = useTheme();
return (
<View style={styles.emptyListContainer}>
<Contacts fill={grey_gainsboro} scale={6} />
<Text style={[styles.emptyListTitle, { color: black }]}>No shared groups</Text>
<Text style={[styles.emptyListSubtitle, { color: grey }]}>
Groups shared with user will appear here
</Text>
</View>
);
};
type ListComponentProps = ChannelListViewProps;
// If the length of channels is 1, which means we only got 1:1-distinct channel,
// And we don't want to show 1:1-distinct channel in this list.
const ListComponent: React.FC<ListComponentProps> = (props) => {
const { channels, loadingChannels, refreshing } = useChannelsContext();
if (channels && channels.length <= 1 && !loadingChannels && !refreshing) {
return <EmptyListComponent />;
}
return <ChannelListView {...props} />;
};
type SharedGroupsScreenRouteProp = RouteProp<StackNavigatorParamList, 'SharedGroupsScreen'>;
type SharedGroupsScreenProps = {
route: SharedGroupsScreenRouteProp;
};
export const SharedGroupsScreen: React.FC<SharedGroupsScreenProps> = ({
route: {
params: { user },
},
}) => {
const { chatClient } = useAppContext();
if (!chatClient?.user) {
return null;
}
return (
<View style={styles.container}>
<ScreenHeader titleText='Shared Groups' />
<ChannelList
filters={{
$and: [{ members: { $in: [chatClient?.user?.id] } }, { members: { $in: [user.id] } }],
}}
List={ListComponent}
options={{
watch: false,
}}
Preview={CustomPreview}
sort={{
last_updated: -1,
}}
/>
</View>
);
};