Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create ChatSupport.jsx #503

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/components/ChatSupport.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// src/components/ChatSupport.jsx
import React, { useEffect, useState } from 'react';
import io from 'socket.io-client';
// Socket server URL
const SOCKET_SERVER_URL = 'http://localhost:3000';
const ChatSupport = () => {
const [socket, setSocket] = useState(null);
const [messages, setMessages] = useState([]);
const [newMessage, setNewMessage] = useState('');
const [username, setUsername] = useState('');
useEffect(() => {
// Initialize socket connection
const socketIo = io(SOCKET_SERVER_URL);
setSocket(socketIo);
// Listen for incoming messages
socketIo.on('message', (message) => {
setMessages((prevMessages) => [...prevMessages, message]);
});
// Clean up on unmount
return () => {
socketIo.disconnect();
};
}, []);
const handleSendMessage = () => {
if (newMessage.trim() && socket) {
socket.emit('message', { username, text: newMessage });
setNewMessage('');
}
};
return (
<div className="chat-support">
<div className="chat-window">
<div className="messages">
{messages.map((msg, index) => (
<div key={index} className={`message ${msg.username === username ? 'self' : ''}`}>
<strong>{msg.username}:</strong> {msg.text}
</div>
))}
</div>
<div className="message-input">
<input
type="text"
value={newMessage}
onChange={(e) => setNewMessage(e.target.value)}
placeholder="Type your message..."
/>
<button onClick={handleSendMessage}>Send</button>
</div>
</div>
</div>
);
};
export default ChatSupport;
Loading