Skip to content

Commit

Permalink
fix: properly restore chats (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasayivor authored and Dun-sin committed Jan 24, 2024
1 parent e187008 commit 230aaeb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
23 changes: 21 additions & 2 deletions src/components/PageWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,29 @@ import { useRouter } from 'next/navigation';
// Store
import { useAuth } from '@/context/AuthContext';
import { useApp } from '@/context/AppContext';
import { ChatProvider } from '@/context/ChatContext';
import { ChatProvider, useChat } from '@/context/ChatContext';
import events from '@/shared/constants/constants';

// Components
import NavBar from './NavBar';

import useIsTabActive from '@/hooks/useIsTabActive';
import { ProviderType } from '@/types/propstypes';
import { useSocket } from '@/context/SocketContext';

const PageWrapper = ({ children }: ProviderType) => {
const { isLoggedIn } = useAuth();
const { updateOnlineStatus, app } = useApp();
const { updateOnlineStatus, endSearch, app } = useApp();
const { createChat } = useChat()
const router = useRouter();
const { settings } = app;

const [onlineStatus, setOnlineStatus] = useState<Date | string | null>(null);

const isTabActive = useIsTabActive();

const { socket } = useSocket();

useEffect(() => {
if (!isLoggedIn) {
return;
Expand All @@ -42,6 +47,20 @@ const PageWrapper = ({ children }: ProviderType) => {
!isLoggedIn && router.push('/login');
}, [isLoggedIn, router]);

useEffect(() => {
const onRestoreChat = ({ chats, currentChatId }: any) => {
Object.values(chats).forEach((chat: any) => {
createChat(chat.id, chat.userIds, chat.messages, chat.createdAt);
});
endSearch(currentChatId);
};

socket?.on(events.NEW_EVENT_CHAT_RESTORE, onRestoreChat);
return () => {
socket?.off(events.NEW_EVENT_CHAT_RESTORE, onRestoreChat);
};
}, [socket]);

return (
<section
className={`flex flex-col-reverse md:flex-row h-screen ${
Expand Down
10 changes: 1 addition & 9 deletions src/pages/anonymous.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,6 @@ const Anonymous = () => {
navigate.push('/searching');
});

socket?.on(events.NEW_EVENT_CHAT_RESTORE, ({ chats, currentChatId }) => {
Object.values(chats).forEach((chat: any) => {
chat = chat as ChatIdType;
createChat(chat.id, chat.userIds, chat.messages, chat.createdAt);
});
endSearch(currentChatId);
});

const connectionEvents = {
connect: () => {
updateConnection(false);
Expand Down Expand Up @@ -297,14 +289,14 @@ const Anonymous = () => {
return () => {
socket?.off(events.NEW_EVENT_ONLINE_STATUS, onlineStatushandler);
socket?.off('connect');
socket?.off(events.NEW_EVENT_CHAT_RESTORE);
socket?.off(events.NEW_EVENT_CLOSE);
socket?.off(events.NEW_EVENT_INACTIVE);
socket?.off('disconnect', onDisconnect);

socket?.io.off('reconnect_attempt', onReconnectAttempt);
socket?.io.off('reconnect_error', onReconnectError);

socket?.disconnect();

newMessageEvents.forEach(event => {
socket?.off(event, onNewMessage);
Expand Down
8 changes: 8 additions & 0 deletions src/pages/searching.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ const Searching = () => {
};
}, [app.currentChatId, authState.email, authState.loginId, router, socket]);

useEffect(() => {
if (!app.currentChatId) {
return;
}

router.push('/anonymous')
}, [app.currentChatId])

useEffect(() => {
if (loadingText === defaultLoadingText) {
timeout = setTimeout(() => {
Expand Down

0 comments on commit 230aaeb

Please sign in to comment.