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

fix: message not sending #615

Merged
merged 8 commits into from
Jun 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
146 changes: 77 additions & 69 deletions client/src/components/Anonymous.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useContext, useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import PropTypes from 'prop-types';
import {
Expand All @@ -17,7 +17,7 @@ import { Icon } from '@rsuite/icons';
import { BiArrowBack, BiDotsVerticalRounded } from 'react-icons/bi';

// Store
import { SocketContext } from 'src/context/Context';
import { socket } from 'src/lib/socketConnection';
import { useChat } from 'src/context/ChatContext';
import { useApp } from 'src/context/AppContext';
import { useDialog } from 'src/context/DialogContext';
Expand All @@ -31,7 +31,10 @@ import useCheckTimePassed from 'src/hooks/useCheckTimePassed';

const centerItems = `flex items-center justify-center`;

const Anonymous = ({ onChatClosed }) => {
const Anonymous = ({
onChatClosed,

}) => {
const { app, endSearch } = useApp();
const { currentChatId, onlineStatus } = app;
const { clearTimer } = useCheckTimePassed();
Expand All @@ -51,11 +54,10 @@ const Anonymous = ({ onChatClosed }) => {
const typingStatusTimeoutRef = useRef(null);

const navigate = useNavigate();
const socket = useContext(SocketContext);
const { closeChat } = useChat();
const { setDialog } = useDialog();

socket.on(NEW_EVENT_DISPLAY, ({ isTyping, chatId }) => {
const onDisplay = useCallback(({ isTyping, chatId }) => {
// eslint-disable-next-line curly
if (chatId !== currentChatId) return;
if (!isTyping) {
Expand All @@ -72,43 +74,80 @@ const Anonymous = ({ onChatClosed }) => {
typingStatusTimeoutRef.current = setTimeout(() => {
setIsTyping(false);
}, 3000);
});

const closeChatHandler = (autoSearch = false) => {
const currentChatId = currentChatIdRef.current;
}, [])

const onOnlineStatushandler = useCallback((onlineStatusState) => {
if (!onlineStatusState.includes('online')) {
const date = new Date(onlineStatusState);
const today = new Date();
// Set today's date to the beginning of the day
today.setHours(0, 0, 0, 0);
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1); // Set it to yesterday

let formattedDate;
if (date >= today) {
// Today
const options = { hour: '2-digit', minute: '2-digit' };
formattedDate = `Last seen today at ${date.toLocaleString('en-US', options)}`;
} else if (date >= yesterday) {
// Yesterday
const options = { hour: '2-digit', minute: '2-digit' };
formattedDate = `Last seen yesterday at ${date.toLocaleString('en-US', options)}`;
} else {
// More than yesterday
const options = { year: 'numeric', month: 'short', day: 'numeric' };
formattedDate = date.toLocaleString('en-US', options);
}
setBuddyOnlineStatus(formattedDate);

if (!currentChatId) {
navigate('/');
return;
}
setBuddyOnlineStatus(onlineStatusState);
}, []);

setAutoSearchAfterClose(autoSearch);
const onNewMessage = useCallback(() => {
setIsTyping(false);
}, [])

socket.timeout(30000).emit(NEW_EVENT_CLOSE, currentChatId, (err, chatClosed) => {
if (err) {
alert('An error occured whiles closing chat.');
setAutoSearchAfterClose(false);
return err;
}
const emitClose = useCallback((err, chatClosed) => {
if (err) {
alert('An error occured whiles closing chat.');
setAutoSearchAfterClose(false);
return err;
}

if (chatClosed) {
closeChat(currentChatId);
}

endSearch();

if (chatClosed) {
closeChat(currentChatId);
if (chatClosed && autoSearchRef.current) {
if (onChatClosed) {
onChatClosed();
}

endSearch();
setAutoSearchAfterClose(false);
} else {
navigate('/');
}

if (chatClosed && autoSearchRef.current) {
if (onChatClosed) {
onChatClosed();
}
clearTimer();
}, [])

setAutoSearchAfterClose(false);
} else {
navigate('/');
}

clearTimer();
});
const closeChatHandler = (autoSearch = false) => {
const currentChatId = currentChatIdRef.current;

if (!currentChatId) {
navigate('/');
return;
}

setAutoSearchAfterClose(autoSearch);

socket.timeout(30000).emit(NEW_EVENT_CLOSE, currentChatId, emitClose)
};

const MenuToggle = (props, ref) => {
Expand Down Expand Up @@ -151,40 +190,6 @@ const Anonymous = ({ onChatClosed }) => {
},
};

function onNewMessage() {
setIsTyping(false);
}

const onlineStatushandler = (onlineStatusState) => {
if (!onlineStatusState.includes('online')) {
const date = new Date(onlineStatusState);
const today = new Date();
// Set today's date to the beginning of the day
today.setHours(0, 0, 0, 0);
const yesterday = new Date(today);
yesterday.setDate(yesterday.getDate() - 1); // Set it to yesterday

let formattedDate;
if (date >= today) {
// Today
const options = { hour: '2-digit', minute: '2-digit' };
formattedDate = `Last seen today at ${date.toLocaleString('en-US', options)}`;
} else if (date >= yesterday) {
// Yesterday
const options = { hour: '2-digit', minute: '2-digit' };
formattedDate = `Last seen yesterday at ${date.toLocaleString('en-US', options)}`;
} else {
// More than yesterday
const options = { year: 'numeric', month: 'short', day: 'numeric' };
formattedDate = date.toLocaleString('en-US', options);
}
setBuddyOnlineStatus(formattedDate);

return;
}
setBuddyOnlineStatus(onlineStatusState);
};

for (const event in connectionEvents) {
socket.on(event, connectionEvents[event]);
}
Expand All @@ -193,10 +198,11 @@ const Anonymous = ({ onChatClosed }) => {
socket.on(event, onNewMessage);
});

socket.on(NEW_EVENT_ONLINE_STATUS, onlineStatushandler);
socket.on(NEW_EVENT_DISPLAY, onDisplay);
socket.on(NEW_EVENT_ONLINE_STATUS, onOnlineStatushandler);

return () => {
socket.off(NEW_EVENT_ONLINE_STATUS, onlineStatushandler);
socket.off(NEW_EVENT_ONLINE_STATUS, onOnlineStatushandler);

newMessageEvents.forEach((event) => {
socket.off(event, onNewMessage);
Expand Down Expand Up @@ -284,7 +290,9 @@ const Anonymous = ({ onChatClosed }) => {
'flex-auto',
])}
>
<Chat />
<Chat

/>
</div>
</div>
);
Expand All @@ -294,4 +302,4 @@ export default Anonymous;

Anonymous.propTypes = {
onChatClosed: PropTypes.func,
};
};
Loading
Loading