fix: phần code build android
This commit is contained in:
@@ -1,4 +1,10 @@
|
||||
import React, { useEffect, useState, useRef } from 'react';
|
||||
<<<<<<< Updated upstream
|
||||
=======
|
||||
import { io, Socket } from 'socket.io-client';
|
||||
import { Capacitor } from '@capacitor/core';
|
||||
import { BACKEND_URL } from '@/utils/backendEndpoint';
|
||||
>>>>>>> Stashed changes
|
||||
import {
|
||||
Compass,
|
||||
Users,
|
||||
@@ -97,6 +103,7 @@ 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(() => {
|
||||
@@ -530,14 +537,32 @@ export const MemberDashboard: React.FC<MemberDashboardProps> = ({
|
||||
muteNotificationsRef.current = muteNotifications;
|
||||
});
|
||||
|
||||
// Socket connection for realtime messaging - changed to listen to global socket events
|
||||
// Socket connection for realtime messaging
|
||||
useEffect(() => {
|
||||
if (!user?.id) return;
|
||||
|
||||
<<<<<<< Updated upstream
|
||||
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)) {
|
||||
=======
|
||||
// Connect to WebSocket: absolute URL on native, relative (Vite proxy) on web
|
||||
const socket = Capacitor.isNativePlatform()
|
||||
? io(BACKEND_URL)
|
||||
: io();
|
||||
socketRef.current = socket;
|
||||
|
||||
socket.on('connect', () => {
|
||||
console.log('[WS] MemberDashboard connected:', socket.id);
|
||||
socket.emit('joinUser', user.id);
|
||||
});
|
||||
|
||||
const handleMessageReceived = (message: any) => {
|
||||
// Check against window-global activeChatUserId so stale closure doesn't block updates
|
||||
const activeChatUserId = (window as any).activeChatUserId;
|
||||
if (activeChatUserId && (message.senderId === activeChatUserId || message.receiverId === activeChatUserId)) {
|
||||
>>>>>>> Stashed changes
|
||||
setChatMessages(prev => [...prev, message]);
|
||||
} else {
|
||||
// Add to unread states
|
||||
@@ -548,31 +573,24 @@ export const MemberDashboard: React.FC<MemberDashboardProps> = ({
|
||||
}
|
||||
};
|
||||
|
||||
const handleConnectionAccepted = () => {
|
||||
socket.on('messageReceived', handleMessageReceived);
|
||||
|
||||
socket.on('connectionAccepted', () => {
|
||||
fetchConnectionsRef.current();
|
||||
};
|
||||
});
|
||||
|
||||
const handleTourMessageNotification = (e: Event) => {
|
||||
const data = (e as CustomEvent).detail;
|
||||
socket.on('tourMessageNotification', (data: any) => {
|
||||
setUnreadTourChats(prev => prev.includes(data.tourId) ? prev : [...prev, data.tourId]);
|
||||
};
|
||||
});
|
||||
|
||||
const handleJoinRequestAccepted = () => {
|
||||
socket.on('joinRequestAccepted', () => {
|
||||
fetchPublicToursRef.current();
|
||||
};
|
||||
|
||||
window.addEventListener('app:messageReceived', handleMessageReceived);
|
||||
window.addEventListener('app:connectionAccepted', handleConnectionAccepted);
|
||||
window.addEventListener('app:tourMessageNotification', handleTourMessageNotification);
|
||||
window.addEventListener('app:joinRequestAccepted', handleJoinRequestAccepted);
|
||||
});
|
||||
|
||||
return () => {
|
||||
window.removeEventListener('app:messageReceived', handleMessageReceived);
|
||||
window.removeEventListener('app:connectionAccepted', handleConnectionAccepted);
|
||||
window.removeEventListener('app:tourMessageNotification', handleTourMessageNotification);
|
||||
window.removeEventListener('app:joinRequestAccepted', handleJoinRequestAccepted);
|
||||
socket.disconnect();
|
||||
};
|
||||
}, [user?.id, activeChatUser]);
|
||||
}, [user?.id]);
|
||||
|
||||
// Autoscroll chat to bottom
|
||||
useEffect(() => {
|
||||
|
||||
Reference in New Issue
Block a user