fix: cập nhật giao diện và các trang dashboard từ máy B

This commit is contained in:
2026-06-27 11:44:28 +07:00
parent b5b6082d26
commit accda67b22
4 changed files with 166 additions and 87 deletions
+29 -80
View File
@@ -1,5 +1,5 @@
import React, { useEffect, useState, useRef } from 'react';
import { io, Socket } from 'socket.io-client';
import {
Compass,
Users,
@@ -90,7 +90,14 @@ export const MemberDashboard: React.FC<MemberDashboardProps> = ({
const [chatMessages, setChatMessages] = useState<any[]>([]);
const [newMessage, setNewMessage] = useState('');
const messagesEndRef = useRef<HTMLDivElement>(null);
const socketRef = useRef<Socket | null>(null);
useEffect(() => {
(window as any).activeChatUserId = activeChatUser?.id;
return () => {
(window as any).activeChatUserId = undefined;
};
}, [activeChatUser]);
const [unreadChatSenders, setUnreadChatSenders] = useState<string[]>([]);
const [unreadTourSenders, setUnreadTourSenders] = useState<string[]>([]);
@@ -333,26 +340,7 @@ export const MemberDashboard: React.FC<MemberDashboardProps> = ({
return () => window.removeEventListener('resize', handleResize);
}, []);
// Request browser Notification permission
useEffect(() => {
if ('Notification' in window && Notification.permission === 'default') {
Notification.requestPermission();
}
}, []);
const showSystemNotification = (title: string, body: string) => {
if (muteNotificationsRef.current) return;
if ('Notification' in window && Notification.permission === 'granted') {
try {
new Notification(title, {
body,
icon: '/favicon.ico'
});
} catch (e) {
console.error('System Notification error:', e);
}
}
};
const isMemberOfMyTours = (senderId: string) => {
if (!publicTours) return false;
@@ -535,86 +523,47 @@ export const MemberDashboard: React.FC<MemberDashboardProps> = ({
muteNotificationsRef.current = muteNotifications;
});
// Socket connection for realtime messaging
// Socket connection for realtime messaging - changed to listen to global socket events
useEffect(() => {
if (!user?.id) return;
// Connect to WebSocket using same origin/proxy
const socket = io();
socketRef.current = socket;
socket.on('connect', () => {
console.log('[WS] MemberDashboard connected:', socket.id);
socket.emit('joinUser', user.id);
});
socket.on('messageReceived', (message: any) => {
const handleMessageReceived = (e: Event) => {
const message = (e as CustomEvent).detail;
// If we are actively chatting with the sender of this message
if (activeChatUser && (message.senderId === activeChatUser.id || message.receiverId === activeChatUser.id)) {
setChatMessages(prev => [...prev, message]);
} else {
if (!muteNotificationsRef.current) {
// Show notification toast for new message
notify({
title: 'Tin nhắn mới',
message: `${message.sender?.name || 'Ai đó'} gửi: "${message.content.substring(0, 30)}${message.content.length > 30 ? '...' : ''}"`,
type: 'success'
});
// Show push notification
showSystemNotification(
`Tin nhắn mới từ ${message.sender?.name || 'Thành viên'}`,
message.content
);
}
// Add to unread states
setUnreadChatSenders(prev => prev.includes(message.senderId) ? prev : [...prev, message.senderId]);
if (isMemberOfMyToursRef.current(message.senderId)) {
setUnreadTourSenders(prev => prev.includes(message.senderId) ? prev : [...prev, message.senderId]);
}
}
});
};
socket.on('connectionAccepted', (data: any) => {
if (!muteNotificationsRef.current) {
notify({
title: 'Kết nối mới',
message: `${data.acceptedByName} đã chấp nhận yêu cầu kết nối của bạn.`,
type: 'success'
});
}
const handleConnectionAccepted = () => {
fetchConnectionsRef.current();
});
socket.on('tourMessageNotification', (data: any) => {
if (!muteNotificationsRef.current) {
notify({
title: `Tin nhắn mới trong tour "${data.tourTitle}"`,
message: `${data.senderName}: "${data.content.substring(0, 30)}${data.content.length > 30 ? '...' : ''}"`,
type: 'success'
});
showSystemNotification(
`Tin nhắn mới trong tour "${data.tourTitle}"`,
`${data.senderName}: ${data.content}`
);
}
};
const handleTourMessageNotification = (e: Event) => {
const data = (e as CustomEvent).detail;
setUnreadTourChats(prev => prev.includes(data.tourId) ? prev : [...prev, data.tourId]);
});
};
socket.on('joinRequestAccepted', (data: any) => {
if (!muteNotificationsRef.current) {
notify({
title: 'Yêu cầu tham gia được duyệt',
message: `Yêu cầu tham gia hành trình "${data.tourTitle}" của bạn đã được chấp nhận!`,
type: 'success'
});
}
const handleJoinRequestAccepted = () => {
fetchPublicToursRef.current();
});
};
window.addEventListener('app:messageReceived', handleMessageReceived);
window.addEventListener('app:connectionAccepted', handleConnectionAccepted);
window.addEventListener('app:tourMessageNotification', handleTourMessageNotification);
window.addEventListener('app:joinRequestAccepted', handleJoinRequestAccepted);
return () => {
socket.disconnect();
window.removeEventListener('app:messageReceived', handleMessageReceived);
window.removeEventListener('app:connectionAccepted', handleConnectionAccepted);
window.removeEventListener('app:tourMessageNotification', handleTourMessageNotification);
window.removeEventListener('app:joinRequestAccepted', handleJoinRequestAccepted);
};
}, [user?.id, activeChatUser]);