-
Notifications
You must be signed in to change notification settings - Fork 29
/
Chat.js
94 lines (86 loc) · 2.65 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
import { useCallback, useState } from 'react';
import { useAppMessage, useLocalSessionId, useParticipantProperty } from '@daily-co/daily-react';
import { Arrow } from '../Tray/Icons/index';
import './Chat.css';
export default function Chat({ showChat, toggleChat }) {
const [messages, setMessages] = useState([]);
const [inputValue, setInputValue] = useState('');
const localSessionId = useLocalSessionId();
const username = useParticipantProperty(localSessionId, 'user_name');
const sendAppMessage = useAppMessage({
onAppMessage: useCallback(
(ev) =>
setMessages((existingMessages) => [
...existingMessages,
{
msg: ev.data.msg,
name: ev.data.name,
},
]),
[],
),
});
const sendMessage = useCallback(
(message) => {
/* Send the message to all participants in the chat - this does not include ourselves!
* See https://docs.daily.co/reference/daily-js/events/participant-events#app-message
*/
sendAppMessage(
{
msg: message,
name: username || 'Guest',
},
'*',
);
/* Since we don't receive our own messages, we will set our message in the messages array.
* This way _we_ can also see what we wrote.
*/
setMessages([
...messages,
{
msg: message,
name: username || 'Guest',
},
]);
},
[messages, sendAppMessage, username],
);
const handleChange = (e) => {
setInputValue(e.target.value);
};
const handleSubmit = (e) => {
e.preventDefault();
if (!inputValue.trim()) return; // don't allow people to submit empty strings
sendMessage(inputValue);
setInputValue('');
};
return showChat ? (
<aside className="chat">
<button onClick={toggleChat} className="close-chat" type="button">
Close chat
</button>
<ul className="chat-messages">
{messages.map((message, index) => (
<li key={`message-${index}`} className="chat-message">
<span className="chat-message-author">{message?.name}</span>:{' '}
<p className="chat-message-body">{message?.msg}</p>
</li>
))}
</ul>
<div className="add-message">
<form className="chat-form" onSubmit={handleSubmit}>
<input
className="chat-input"
type="text"
placeholder="Type your message here.."
value={inputValue}
onChange={handleChange}
/>
<button type="submit" className="chat-submit-button">
<Arrow />
</button>
</form>
</div>
</aside>
) : null;
}