-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAblyChatComponent.jsx
181 lines (169 loc) · 5.19 KB
/
AblyChatComponent.jsx
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
import React, { useEffect, useState } from 'react';
import { useChannel } from './AblyReactEffect';
import { useAuthContext } from '../hooks/useAuthContext';
import {
collection,
addDoc,
getDocs,
query,
where,
orderBy,
} from 'firebase/firestore';
import { db } from '../firebase/config';
import { useRouter } from 'next/router';
import styles from '../css/chat.module.css';
import { motion } from 'framer-motion';
import Link from 'next/link';
const buttonVariants = {
hover: {
scale: 1.06,
},
tap: {
scale: 0.99,
},
};
const AblyChatComponent = (props) => {
const { user } = useAuthContext();
let inputBox = null;
let messageEnd = null;
const [messageText, setMessageText] = useState('');
const [receivedMessages, setMessages] = useState([]);
const [messageHistory, setMessageHistory] = useState([]);
const messageTextIsEmpty = messageText.trim().length === 0;
const [channel, ably] = useChannel(props.channelNum.channel, (message) => {
// Here we're computing the state that'll be drawn into the message history
// We do that by slicing the last 199 messages from the receivedMessages buffer
const history = receivedMessages.slice(-199);
setMessages([...history, message]);
// Then finally, we take the message history, and combine it with the new message
// This means we'll always have up to 199 message + 1 new message, stored using the
// setMessages react useState hook
});
const router = useRouter();
useEffect(() => {
async function getChatHistory() {
const q = query(
collection(db, 'messages'),
where('channel', '==', `${props.channelNum.channel}`),
orderBy('time')
);
const querySnapshot = await getDocs(q);
const returnedMessages = querySnapshot.docs.map((doc) => {
return doc._document.data.value.mapValue.fields;
});
return returnedMessages;
}
getChatHistory().then((res) => {
setMessageHistory(res);
});
messageEnd.scrollIntoView({ behaviour: 'smooth' });
}, []);
const sendChatMessage = (messageText) => {
const docRef = addDoc(collection(db, 'messages'), {
user: user.displayName,
message: messageText,
channel: props.channelNum.channel,
recipient: router.query.secondUser,
time: Date.now(),
});
channel.publish({ name: user.displayName, data: messageText });
setMessageText('');
inputBox.focus();
};
const handleFormSubmission = (event) => {
event.preventDefault();
sendChatMessage(messageText);
};
const handleKeyPress = (event) => {
if (event.charCode !== 13 || messageTextIsEmpty) {
return;
}
sendChatMessage(messageText);
event.preventDefault();
};
const previousMessages = messageHistory.map((msg, index) => {
return (
<section key={index}>
<span className={styles.prevMessage}>
{msg.user.stringValue}: {msg.message.stringValue}
</span>
<br />
</section>
);
});
const messages = receivedMessages.map((message, index) => {
const author = message.connectionId === ably.connection.id ? 'me' : 'other';
return (
<section key={index}>
<div>
<span data-author={author} className="">
{message.name}: {message.data}
</span>
</div>
<br />
</section>
);
});
<div
ref={(element) => {
messageEnd = element;
}}
></div>;
return (
<main className="gap-10 flex flex-col md:justify-end md:items-start pl-1 p-5">
<div className="md:hidden text-2xl font-bold">
Chatting with {router.query.secondUser}
</div>
<div>
<div className="flex flex-col h-full w-full">
<div>{previousMessages}</div>
<div>{messages}</div>
</div>
<div
ref={(element) => {
messageEnd = element;
}}
></div>
</div>
<form
onSubmit={handleFormSubmission}
className="flex flex-col md:flex-row md:items-end gap-[20px]"
>
<textarea
className="h-full w-[400px] px-4 py-4 resize-none text-lg"
value={messageText}
ref={(element) => {
inputBox = element;
}}
placeholder="Type a message..."
onChange={(e) => setMessageText(e.target.value)}
onKeyPress={handleKeyPress}
></textarea>
<div className="flex gap-2">
<motion.button
variants={buttonVariants}
whileHover="hover"
whileTap="tap"
type="submit"
disabled={messageTextIsEmpty}
className="bg-[#4f9cf9] text-[#ffffff] border-none cursor-pointer text-lg py-5 px-10 rounded self-end"
>
Send
</motion.button>
<Link href="/video" target="_blank">
<motion.button
variants={buttonVariants}
whileHover="hover"
whileTap="tap"
type="button"
className="bg-[#4f9cf9] text-[#ffffff] border-none cursor-pointer text-lg py-5 px-10 rounded self-end"
>
Video
</motion.button>
</Link>
</div>
</form>
</main>
);
};
export default AblyChatComponent;