forked from FaridSafi/react-native-gifted-chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
269 lines (243 loc) · 6.81 KB
/
App.tsx
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import { MaterialIcons } from '@expo/vector-icons'
import { AppLoading, Asset, Linking } from 'expo'
import React, { Component } from 'react'
import { StyleSheet, View, Text, Platform } from 'react-native'
import { Bubble, GiftedChat, SystemMessage, IMessage, Send } from './src'
import AccessoryBar from './example-expo/AccessoryBar'
import CustomActions from './example-expo/CustomActions'
import CustomView from './example-expo/CustomView'
import NavBar from './example-expo/NavBar'
import messagesData from './example-expo/data/messages'
import earlierMessages from './example-expo/data/earlierMessages'
const styles = StyleSheet.create({
container: { flex: 1 },
})
const filterBotMessages = message =>
!message.system && message.user && message.user._id && message.user._id === 2
const findStep = step => message => message._id === step
const user = {
_id: 1,
name: 'Developer',
}
const otherUser = {
_id: 2,
name: 'React Native',
avatar: 'https://facebook.github.io/react/img/logo_og.png',
}
export default class App extends Component {
state = {
inverted: false,
step: 0,
messages: [],
loadEarlier: true,
typingText: null,
isLoadingEarlier: false,
appIsReady: false,
isTyping: false,
}
_isMounted = false
componentDidMount() {
this._isMounted = true
// init with only system messages
this.setState({
messages: messagesData, // messagesData.filter(message => message.system),
appIsReady: true,
isTyping: false,
})
}
componentWillUnmount() {
this._isMounted = false
}
onLoadEarlier = () => {
this.setState(() => {
return {
isLoadingEarlier: true,
}
})
setTimeout(() => {
if (this._isMounted === true) {
this.setState((previousState: any) => {
return {
messages: GiftedChat.prepend(
previousState.messages,
earlierMessages() as IMessage[],
Platform.OS !== 'web',
),
loadEarlier: true,
isLoadingEarlier: false,
}
})
}
}, 1500) // simulating network
}
onSend = (messages = []) => {
const step = this.state.step + 1
this.setState((previousState: any) => {
const sentMessages = [{ ...messages[0], sent: true, received: true }]
return {
messages: GiftedChat.append(
previousState.messages,
sentMessages,
Platform.OS !== 'web',
),
step,
}
})
// for demo purpose
// setTimeout(() => this.botSend(step), Math.round(Math.random() * 1000))
}
botSend = (step = 0) => {
const newMessage = (messagesData as IMessage[])
.reverse()
// .filter(filterBotMessages)
.find(findStep(step))
if (newMessage) {
this.setState((previousState: any) => ({
messages: GiftedChat.append(
previousState.messages,
[newMessage],
Platform.OS !== 'web',
),
}))
}
}
parsePatterns = (_linkStyle: any) => {
return [
{
pattern: /#(\w+)/,
style: { textDecorationLine: 'underline', color: 'darkorange' },
onPress: () => Linking.openURL('http://gifted.chat'),
},
]
}
renderCustomView(props) {
return <CustomView {...props} />
}
onReceive = (text: string) => {
this.setState((previousState: any) => {
return {
messages: GiftedChat.append(
previousState.messages as any,
[
{
_id: Math.round(Math.random() * 1000000),
text,
createdAt: new Date(),
user: otherUser,
},
],
Platform.OS !== 'web',
),
}
})
}
onSendFromUser = (messages: IMessage[] = []) => {
const createdAt = new Date()
const messagesToUpload = messages.map(message => ({
...message,
user,
createdAt,
_id: Math.round(Math.random() * 1000000),
}))
this.onSend(messagesToUpload)
}
setIsTyping = () => {
this.setState({
isTyping: !this.state.isTyping,
})
}
renderAccessory = () => (
<AccessoryBar onSend={this.onSendFromUser} isTyping={this.setIsTyping} />
)
renderCustomActions = props =>
Platform.OS === 'web' ? null : (
<CustomActions {...props} onSend={this.onSendFromUser} />
)
renderBubble = (props: any) => {
return <Bubble {...props} />
}
renderSystemMessage = props => {
return (
<SystemMessage
{...props}
containerStyle={{
marginBottom: 15,
}}
textStyle={{
fontSize: 14,
}}
/>
)
}
onQuickReply = replies => {
const createdAt = new Date()
if (replies.length === 1) {
this.onSend([
{
createdAt,
_id: Math.round(Math.random() * 1000000),
text: replies[0].title,
user,
},
])
} else if (replies.length > 1) {
this.onSend([
{
createdAt,
_id: Math.round(Math.random() * 1000000),
text: replies.map(reply => reply.title).join(', '),
user,
},
])
} else {
console.warn('replies param is not set correctly')
}
}
renderQuickReplySend = () => <Text>{' custom send =>'}</Text>
renderSend = (props: Send['props']) => (
<Send {...props} containerStyle={{ justifyContent: 'center' }}>
<MaterialIcons size={30} color={'tomato'} name={'send'} />
</Send>
)
render() {
if (!this.state.appIsReady) {
return <AppLoading />
}
return (
<View
style={styles.container}
accessible
accessibilityLabel='main'
testID='main'
>
<NavBar />
<GiftedChat
messages={this.state.messages}
onSend={this.onSend}
loadEarlier={this.state.loadEarlier}
onLoadEarlier={this.onLoadEarlier}
isLoadingEarlier={this.state.isLoadingEarlier}
parsePatterns={this.parsePatterns}
user={user}
scrollToBottom
onLongPressAvatar={user => alert(JSON.stringify(user))}
onPressAvatar={() => alert('short press')}
onQuickReply={this.onQuickReply}
keyboardShouldPersistTaps='never'
renderAccessory={Platform.OS === 'web' ? null : this.renderAccessory}
renderActions={this.renderCustomActions}
renderBubble={this.renderBubble}
renderSystemMessage={this.renderSystemMessage}
renderCustomView={this.renderCustomView}
renderSend={this.renderSend}
quickReplyStyle={{ borderRadius: 2 }}
renderQuickReplySend={this.renderQuickReplySend}
inverted={Platform.OS !== 'web'}
timeTextStyle={{ left: { color: 'red' }, right: { color: 'yellow' } }}
isTyping={this.state.isTyping}
infiniteScroll
/>
</View>
)
}
}