-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatInterface.js
More file actions
58 lines (52 loc) · 1.84 KB
/
ChatInterface.js
File metadata and controls
58 lines (52 loc) · 1.84 KB
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
import React, { useState, useEffect } from 'react';
const ChatInterface = ({ currentUser, otherUser }) => {
const [messages, setMessages] = useState([]);
const [newMessage, setNewMessage] = useState('');
useEffect(() => {
// Fetch chat history from the server or local storage
fetchChatHistory();
}, []);
const fetchChatHistory = () => {
// Simulate fetching chat history
const chatHistory = [
{ sender: 'User1', text: 'Hello!' },
{ sender: 'User2', text: 'Hi there!' },
];
setMessages(chatHistory);
};
const handleSendMessage = () => {
if (newMessage.trim()) {
const message = {
sender: currentUser.name,
text: newMessage,
};
setMessages([...messages, message]);
setNewMessage('');
// Here you would also send the message to the server
}
};
return (
<div className="chat-interface">
<div className="chat-header">
<h2>Chat with {otherUser.name}</h2>
</div>
<div className="chat-messages">
{messages.map((msg, index) => (
<div key={index} className={msg.sender === currentUser.name ? 'message sent' : 'message received'}>
<strong>{msg.sender}: </strong>{msg.text}
</div>
))}
</div>
<div className="chat-input">
<input
type="text"
value={newMessage}
onChange={(e) => setNewMessage(e.target.value)}
placeholder="Type your message..."
/>
<button onClick={handleSendMessage}>Send</button>
</div>
</div>
);
};
export default ChatInterface;