-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat.js
178 lines (165 loc) · 4.44 KB
/
chat.js
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
import React, { useEffect, useState } from "react";
import {
StyleSheet,
Text,
View,
Button,
SafeAreaView,
TextInput,
KeyboardAvoidingView,
Platform
} from "react-native";
import { usePubNub } from "pubnub-react";
const Chat = ({ route }) => {
// The `route` prop will be passed to us thanks to React Navigation.
// It will contain our emoji in `route.params.emoji`.
const userEmoji = route.params.emoji;
// Here we obtain our PubNub instance thanks to using the provider
const pubnub = usePubNub();
// In next two statements we define the state needed for our chat
const [input, setInput] = useState("");
const [messages, setMessages] = useState([]);
// First we need to set our PubNub UUID and subscribe to chat channel.
// We will use `useEffect` hook for that.
useEffect(() => {
// We need to make sure that PubNub is defined
if (pubnub) {
// Set the UUID of our user to their chosen emoji
pubnub.setUUID(userEmoji);
// Create a listener that will push new messages to our `messages` variable
// using the `setMessages` function.
const listener = {
message: envelope => {
setMessages(msgs => [
...msgs,
{
id: envelope.message.id,
author: envelope.publisher,
content: envelope.message.content,
timetoken: envelope.timetoken
}
]);
}
};
// Add the listener to pubnub instance and subscribe to `chat` channel.
pubnub.addListener(listener);
pubnub.subscribe({ channels: ["chat"] });
// We need to return a function that will handle unsubscription on unmount
return () => {
pubnub.removeListener(listener);
pubnub.unsubscribeAll();
};
}
}, [pubnub]);
// This function handles sending messages.
const handleSubmit = () => {
// Clear the input field.
setInput("");
// Create the message with random `id`.
const message = {
content: input,
id: Math.random()
.toString(16)
.substr(2)
};
// Publish our message to the channel `chat`
pubnub.publish({ channel: "chat", message });
};
return (
<SafeAreaView style={styles.outerContainer}>
<KeyboardAvoidingView
style={styles.innerContainer}
behavior="height"
keyboardVerticalOffset={Platform.select({
ios: 78,
android: 0
})}
>
<View style={styles.topContainer}>
{messages.map(message => (
<View key={message.timetoken} style={styles.messageContainer}>
<View style={styles.avatar}>
<Text style={styles.avatarContent}>{message.author}</Text>
</View>
<View style={styles.messageContent}>
<Text>{message.content}</Text>
</View>
</View>
))}
</View>
<View style={styles.bottomContainer}>
<TextInput
style={styles.textInput}
value={input}
onChangeText={setInput}
onSubmitEditing={handleSubmit}
returnKeyType="send"
enablesReturnKeyAutomatically={true}
placeholder="Type your message here..."
/>
<View style={styles.submitButton}>
{input !== "" && <Button title="Send" onPress={handleSubmit} />}
</View>
</View>
</KeyboardAvoidingView>
</SafeAreaView>
);
};
const styles = StyleSheet.create({
outerContainer: {
width: "100%",
height: "100%"
},
innerContainer: {
width: "100%",
height: "100%"
},
topContainer: {
flex: 1,
width: "100%",
flexDirection: "column",
justifyContent: "flex-end",
paddingHorizontal: 16
},
messageContainer: {
flexDirection: "row",
marginTop: 16,
alignItems: "center",
backgroundColor: "#fff",
padding: 8,
borderRadius: 4
},
avatar: {
width: 38,
height: 38,
borderRadius: 50,
overflow: "hidden",
marginRight: 16
},
avatarContent: {
fontSize: 30,
textAlign: "center",
textAlignVertical: "center"
},
messageContent: {
flex: 1
},
bottomContainer: {
width: "100%",
flexDirection: "row",
alignItems: "center",
padding: 16
},
textInput: {
flex: 1,
backgroundColor: "#fff",
borderRadius: 4,
padding: 16,
elevation: 2
},
submitButton: {
position: "absolute",
right: 32
}
});
export default Chat;