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
|
import {useBottomTabBarHeight} from '@react-navigation/bottom-tabs';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {useContext} from 'react';
import {Image, StyleSheet, Text, View} from 'react-native';
import {SafeAreaView} from 'react-native-safe-area-context';
import {useStore} from 'react-redux';
import {
Channel,
Chat,
MessageInput,
MessageList,
OverlayProvider,
useMessageContext,
} from 'stream-chat-react-native';
import {ChatContext} from '../../App';
import ChatHeader from '../../components/messages/ChatHeader';
import {TAGG_LIGHT_BLUE} from '../../constants';
import {MainStackParams} from '../../routes';
import {RootState} from '../../store/rootReducer';
import {ScreenType} from '../../types';
import {isIPhoneX, SCREEN_WIDTH} from '../../utils';
type ChatScreenNavigationProp = StackNavigationProp<MainStackParams, 'Chat'>;
interface ChatScreenProps {
navigation: ChatScreenNavigationProp;
}
/*
* Screen that displays all of the user's active conversations.
*/
const ChatScreen: React.FC<ChatScreenProps> = () => {
const {channel, chatClient} = useContext(ChatContext);
const state: RootState = useStore().getState();
const loggedInUserId = state.user.user.userId;
const tabbarHeight = useBottomTabBarHeight();
const chatTheme = {
messageList: {
container: {
backgroundColor: 'white',
},
},
};
const isOwnMessage = (message) => {
if (message.user.id === loggedInUserId) {
return true;
} else {
return false;
}
};
const OwnMessageBubble = ({message}) => (
<View style={[styles.mainBubbleContainer, styles.mainOwnBubbleContainer]}>
<View style={styles.ownBubbleContainer}>
<View style={styles.ownBubble}>
<Text style={styles.messageText}>{message.text}</Text>
</View>
</View>
{/* TODO: Timestamp */}
</View>
);
const UserXMessageBubble = ({message}) => (
<View style={[styles.mainBubbleContainer, styles.mainUserXBubbleContainer]}>
<Image
style={styles.avatar}
source={
message.user.thumbnail_url
? {uri: message.user.thumbnail_url}
: require('../../assets/images/avatar-placeholder.png')
}
/>
<View style={styles.userXBubble}>
<Text style={styles.messageText}>{message.text}</Text>
</View>
{/* TODO: Timestamp */}
</View>
);
const CustomMessageUIComponent = () => {
const {message} = useMessageContext();
if (isOwnMessage(message)) {
return <OwnMessageBubble message={message} />;
} else {
return <UserXMessageBubble message={message} />;
}
};
return (
<SafeAreaView
style={[
styles.container,
// unable to figure out the padding issue, a hacky solution
{paddingBottom: isIPhoneX() ? tabbarHeight + 20 : tabbarHeight + 50},
]}>
<ChatHeader screenType={ScreenType.Chat} />
<Chat client={chatClient} style={chatTheme}>
<OverlayProvider topInset={0} bottomInset={0}>
<Channel
channel={channel}
keyboardVerticalOffset={0}
OverlayReactionList={() => null}
MessageSimple={CustomMessageUIComponent}
messageActions={({copyMessage, deleteMessage}) => [
copyMessage,
deleteMessage,
]}>
<MessageList onThreadSelect={() => {}} />
<MessageInput />
</Channel>
</OverlayProvider>
</Chat>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
container: {
backgroundColor: 'white',
flex: 1,
},
submitButton: {
height: 35,
width: 35,
backgroundColor: TAGG_LIGHT_BLUE,
borderRadius: 999,
justifyContent: 'center',
alignItems: 'center',
bottom: -5,
alignSelf: 'flex-end',
},
messageText: {
width: 196,
paddingHorizontal: 23,
paddingTop: 7.35,
paddingBottom: 12,
},
mainBubbleContainer: {
marginVertical: 1,
width: SCREEN_WIDTH,
flexDirection: 'row',
},
mainOwnBubbleContainer: {
justifyContent: 'flex-end', // Different
marginTop: 22,
},
mainUserXBubbleContainer: {
justifyContent: 'flex-start',
},
avatar: {
width: 40,
height: 40,
borderRadius: 20,
right: -19,
zIndex: 1,
position: 'absolute',
top: 0,
left: 0,
},
ownBubbleContainer: {width: 241, marginBottom: 9},
ownBubble: {
maxWidth: 270,
backgroundColor: '#DEE6F4',
borderColor: '#DEE6F4',
borderWidth: 1,
borderRadius: 10,
alignSelf: 'center',
},
userXBubble: {
maxWidth: 235,
backgroundColor: '#E4F0F2',
borderColor: '#E4F0F2',
borderWidth: 1,
borderRadius: 10,
zIndex: 0,
alignSelf: 'flex-end',
marginLeft: 25,
marginTop: 14,
},
});
export default ChatScreen;
|