fixed some socket not working on production

This commit is contained in:
slawk0
2024-12-17 22:36:24 +01:00
parent 894c6d8f3a
commit 9c98a88027
2 changed files with 51 additions and 60 deletions

View File

@@ -44,20 +44,16 @@ function ContactsList({
useEffect(() => { useEffect(() => {
if (!socket) return; if (!socket) return;
socket.on( const handleAddedToGroup = () => {
'added to group', fetchContacts();
(msg: { };
username: string;
user_id: string; socket.on('added to group', handleAddedToGroup);
group_id: string;
isadmin: false; return () => {
}) => { socket?.off('added to group', handleAddedToGroup);
console.log('added to group, fetching contacts'); };
console.error(msg); }, [socket]);
fetchContacts();
},
);
}, []);
const fetchContacts = async () => { const fetchContacts = async () => {
const contacts: ContactsProps[] = await getContactsList(); const contacts: ContactsProps[] = await getContactsList();

View File

@@ -71,50 +71,42 @@ function ParticipantsBar({
useEffect(() => { useEffect(() => {
if (!socket || !contact) return; if (!socket || !contact) return;
socket.on( const handleAddedToGroup = (msg: {
'added to group', username: string;
(msg: { user_id: string;
username: string; group_id: string;
user_id: string; isadmin: false;
group_id: string; }) => {
isadmin: false; const { group_id } = msg;
}) => { if (
const { group_id } = msg; msg.group_id == currentContact?.conversation_id &&
console.log('Added to group: ', msg); msg.user_id == user?.user_id
console.log('Current participants: ', participants); ) {
console.log('CURRENTCONTACT: ', currentContact); initializeContact({
if ( read: true,
msg.group_id == currentContact?.conversation_id && username: msg.username,
msg.user_id == user?.user_id user_id: msg.user_id,
) { id: currentContact?.id,
initializeContact({ type: 'group',
read: true, conversation_id: msg.group_id,
username: msg.username, last_active: new Date().toString(),
user_id: msg.user_id, });
id: currentContact?.id, }
type: 'group',
conversation_id: msg.group_id,
last_active: new Date().toString(),
});
}
if (group_id === contact.conversation_id) { if (group_id === contact.conversation_id) {
setParticipants((prevMembers) => { setParticipants((prevMembers) => {
const existingMember = prevMembers.some( const existingMember = prevMembers.some(
(m) => m.user_id === msg.user_id, (m) => m.user_id === msg.user_id,
); );
if (existingMember) { if (existingMember) {
return prevMembers; return prevMembers;
} }
return [...prevMembers, msg]; return [...prevMembers, msg];
}); });
} }
}, };
);
socket.on('left group', (msg: { user_id: string; group_id: string }) => { const handleLeftGroup = (msg: { user_id: string; group_id: string }) => {
console.log(`(socket on "left group")`, msg);
// Initialize contact again to clear messages and participants list
if ( if (
msg.group_id == currentContact?.conversation_id && msg.group_id == currentContact?.conversation_id &&
msg.user_id == user?.user_id msg.user_id == user?.user_id
@@ -124,13 +116,16 @@ function ParticipantsBar({
setParticipants((prevMembers) => setParticipants((prevMembers) =>
prevMembers.filter((member) => member.user_id !== msg.user_id), prevMembers.filter((member) => member.user_id !== msg.user_id),
); );
}); };
socket.on('added to group', handleAddedToGroup);
socket.on('left group', handleLeftGroup);
return () => { return () => {
socket?.off('left group'); socket?.off('added to group', handleAddedToGroup);
socket?.off('added to group'); socket?.off('left group', handleLeftGroup);
}; };
}, [contact, participants]); }, [socket, contact, currentContact, user?.user_id]);
const handleRemoveUser = async (userId: string) => { const handleRemoveUser = async (userId: string) => {
try { try {