2025-11-12 17:50:44 +01:00
|
|
|
import { useState, useRef, useEffect } from 'react';
|
|
|
|
|
import { useParams, useNavigate } from 'react-router-dom';
|
|
|
|
|
import Layout from '../components/layout/Layout';
|
|
|
|
|
import { useAuth } from '../contexts/AuthContext';
|
|
|
|
|
import { mockEvents } from '../mocks/events';
|
|
|
|
|
import { Send, UserPlus } from 'lucide-react';
|
2025-11-12 22:42:15 +01:00
|
|
|
import { connectSocket, disconnectSocket, getSocket } from '../services/socket';
|
2025-11-12 17:50:44 +01:00
|
|
|
|
|
|
|
|
const EventChatPage = () => {
|
|
|
|
|
const { eventId } = useParams();
|
|
|
|
|
const { user } = useAuth();
|
|
|
|
|
const navigate = useNavigate();
|
2025-11-12 22:42:15 +01:00
|
|
|
const [messages, setMessages] = useState([]);
|
2025-11-12 17:50:44 +01:00
|
|
|
const [newMessage, setNewMessage] = useState('');
|
2025-11-12 22:42:15 +01:00
|
|
|
const [activeUsers, setActiveUsers] = useState([]);
|
|
|
|
|
const [isConnected, setIsConnected] = useState(false);
|
2025-11-12 17:50:44 +01:00
|
|
|
const messagesEndRef = useRef(null);
|
|
|
|
|
|
|
|
|
|
const event = mockEvents.find(e => e.id === parseInt(eventId));
|
|
|
|
|
|
|
|
|
|
const scrollToBottom = () => {
|
|
|
|
|
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
scrollToBottom();
|
|
|
|
|
}, [messages]);
|
|
|
|
|
|
2025-11-12 22:42:15 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
// Connect to Socket.IO
|
|
|
|
|
const socket = connectSocket();
|
|
|
|
|
|
|
|
|
|
if (!socket) {
|
|
|
|
|
console.error('Failed to connect to socket');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Socket event listeners
|
|
|
|
|
socket.on('connect', () => {
|
|
|
|
|
setIsConnected(true);
|
|
|
|
|
// Join event room
|
|
|
|
|
socket.emit('join_event_room', { eventId: parseInt(eventId) });
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
socket.on('disconnect', () => {
|
|
|
|
|
setIsConnected(false);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Receive messages
|
|
|
|
|
socket.on('event_message', (message) => {
|
|
|
|
|
setMessages((prev) => [...prev, message]);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Receive active users list
|
|
|
|
|
socket.on('active_users', (users) => {
|
|
|
|
|
// Filter out duplicates and current user
|
|
|
|
|
const uniqueUsers = users
|
|
|
|
|
.filter((u, index, self) =>
|
|
|
|
|
index === self.findIndex((t) => t.userId === u.userId)
|
|
|
|
|
)
|
|
|
|
|
.filter((u) => u.userId !== user.id);
|
|
|
|
|
setActiveUsers(uniqueUsers);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// User joined notification
|
|
|
|
|
socket.on('user_joined', (userData) => {
|
|
|
|
|
console.log(`${userData.username} joined the room`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// User left notification
|
|
|
|
|
socket.on('user_left', (userData) => {
|
|
|
|
|
console.log(`${userData.username} left the room`);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
|
return () => {
|
|
|
|
|
socket.emit('leave_event_room', { eventId: parseInt(eventId) });
|
|
|
|
|
socket.off('connect');
|
|
|
|
|
socket.off('disconnect');
|
|
|
|
|
socket.off('event_message');
|
|
|
|
|
socket.off('active_users');
|
|
|
|
|
socket.off('user_joined');
|
|
|
|
|
socket.off('user_left');
|
|
|
|
|
};
|
|
|
|
|
}, [eventId, user.id]);
|
|
|
|
|
|
2025-11-12 17:50:44 +01:00
|
|
|
const handleSendMessage = (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
if (!newMessage.trim()) return;
|
|
|
|
|
|
2025-11-12 22:42:15 +01:00
|
|
|
const socket = getSocket();
|
|
|
|
|
if (!socket || !socket.connected) {
|
|
|
|
|
alert('Not connected to chat server');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Send message via Socket.IO
|
|
|
|
|
socket.emit('send_event_message', {
|
|
|
|
|
eventId: parseInt(eventId),
|
2025-11-12 17:50:44 +01:00
|
|
|
content: newMessage,
|
2025-11-12 22:42:15 +01:00
|
|
|
});
|
2025-11-12 17:50:44 +01:00
|
|
|
|
|
|
|
|
setNewMessage('');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleMatchWith = (userId) => {
|
2025-11-12 22:42:15 +01:00
|
|
|
// TODO: Implement match request
|
2025-11-12 17:50:44 +01:00
|
|
|
alert(`Match request sent to user!`);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
navigate(`/matches/1/chat`);
|
|
|
|
|
}, 1000);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!event) {
|
|
|
|
|
return (
|
|
|
|
|
<Layout>
|
|
|
|
|
<div className="text-center">Event not found</div>
|
|
|
|
|
</Layout>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Layout>
|
|
|
|
|
<div className="max-w-6xl mx-auto">
|
|
|
|
|
<div className="bg-white rounded-lg shadow-md overflow-hidden">
|
|
|
|
|
{/* Header */}
|
|
|
|
|
<div className="bg-primary-600 text-white p-4">
|
|
|
|
|
<h2 className="text-2xl font-bold">{event.name}</h2>
|
|
|
|
|
<p className="text-primary-100 text-sm">{event.location}</p>
|
2025-11-12 22:42:15 +01:00
|
|
|
<div className="mt-2">
|
|
|
|
|
<span className={`text-xs px-2 py-1 rounded ${isConnected ? 'bg-green-500' : 'bg-red-500'}`}>
|
|
|
|
|
{isConnected ? '● Connected' : '● Disconnected'}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2025-11-12 17:50:44 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div className="flex h-[calc(100vh-280px)]">
|
|
|
|
|
{/* Active Users Sidebar */}
|
|
|
|
|
<div className="w-64 border-r bg-gray-50 p-4 overflow-y-auto">
|
|
|
|
|
<h3 className="font-semibold text-gray-900 mb-4">
|
|
|
|
|
Active users ({activeUsers.length})
|
|
|
|
|
</h3>
|
2025-11-12 22:42:15 +01:00
|
|
|
{activeUsers.length === 0 && (
|
|
|
|
|
<p className="text-sm text-gray-500">No other users online</p>
|
|
|
|
|
)}
|
2025-11-12 17:50:44 +01:00
|
|
|
<div className="space-y-2">
|
|
|
|
|
{activeUsers.map((activeUser) => (
|
|
|
|
|
<div
|
2025-11-12 22:42:15 +01:00
|
|
|
key={activeUser.userId}
|
2025-11-12 17:50:44 +01:00
|
|
|
className="flex items-center justify-between p-2 hover:bg-gray-100 rounded-lg"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-center space-x-2">
|
|
|
|
|
<img
|
|
|
|
|
src={activeUser.avatar}
|
|
|
|
|
alt={activeUser.username}
|
|
|
|
|
className="w-8 h-8 rounded-full"
|
|
|
|
|
/>
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-sm font-medium text-gray-900">
|
|
|
|
|
{activeUser.username}
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<button
|
2025-11-12 22:42:15 +01:00
|
|
|
onClick={() => handleMatchWith(activeUser.userId)}
|
2025-11-12 17:50:44 +01:00
|
|
|
className="p-1 text-primary-600 hover:bg-primary-50 rounded"
|
|
|
|
|
title="Connect"
|
|
|
|
|
>
|
|
|
|
|
<UserPlus className="w-4 h-4" />
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Chat Area */}
|
|
|
|
|
<div className="flex-1 flex flex-col">
|
|
|
|
|
{/* Messages */}
|
|
|
|
|
<div className="flex-1 overflow-y-auto p-4 space-y-4">
|
2025-11-12 22:42:15 +01:00
|
|
|
{messages.length === 0 && (
|
|
|
|
|
<div className="text-center text-gray-500 py-8">
|
|
|
|
|
No messages yet. Start the conversation!
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
2025-11-12 17:50:44 +01:00
|
|
|
{messages.map((message) => {
|
2025-11-12 22:42:15 +01:00
|
|
|
const isOwnMessage = message.userId === user.id;
|
2025-11-12 17:50:44 +01:00
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
key={message.id}
|
|
|
|
|
className={`flex ${isOwnMessage ? 'justify-end' : 'justify-start'}`}
|
|
|
|
|
>
|
|
|
|
|
<div className={`flex items-start space-x-2 max-w-md ${isOwnMessage ? 'flex-row-reverse space-x-reverse' : ''}`}>
|
|
|
|
|
<img
|
|
|
|
|
src={message.avatar}
|
|
|
|
|
alt={message.username}
|
|
|
|
|
className="w-8 h-8 rounded-full"
|
|
|
|
|
/>
|
|
|
|
|
<div>
|
|
|
|
|
<div className="flex items-baseline space-x-2 mb-1">
|
|
|
|
|
<span className="text-sm font-medium text-gray-900">
|
|
|
|
|
{message.username}
|
|
|
|
|
</span>
|
|
|
|
|
<span className="text-xs text-gray-500">
|
2025-11-12 22:42:15 +01:00
|
|
|
{new Date(message.createdAt).toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' })}
|
2025-11-12 17:50:44 +01:00
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
<div
|
|
|
|
|
className={`rounded-lg px-4 py-2 ${
|
|
|
|
|
isOwnMessage
|
|
|
|
|
? 'bg-primary-600 text-white'
|
|
|
|
|
: 'bg-gray-100 text-gray-900'
|
|
|
|
|
}`}
|
|
|
|
|
>
|
|
|
|
|
{message.content}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
<div ref={messagesEndRef} />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Message Input */}
|
|
|
|
|
<div className="border-t p-4">
|
|
|
|
|
<form onSubmit={handleSendMessage} className="flex space-x-2">
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
value={newMessage}
|
|
|
|
|
onChange={(e) => setNewMessage(e.target.value)}
|
|
|
|
|
placeholder="Write a message..."
|
2025-11-12 22:42:15 +01:00
|
|
|
disabled={!isConnected}
|
|
|
|
|
className="flex-1 px-4 py-2 border border-gray-300 rounded-lg focus:ring-primary-500 focus:border-primary-500 disabled:opacity-50"
|
2025-11-12 17:50:44 +01:00
|
|
|
/>
|
|
|
|
|
<button
|
|
|
|
|
type="submit"
|
2025-11-12 22:42:15 +01:00
|
|
|
disabled={!isConnected}
|
|
|
|
|
className="px-4 py-2 bg-primary-600 text-white rounded-lg hover:bg-primary-700 transition-colors disabled:opacity-50"
|
2025-11-12 17:50:44 +01:00
|
|
|
>
|
|
|
|
|
<Send className="w-5 h-5" />
|
|
|
|
|
</button>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</Layout>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default EventChatPage;
|