-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added message sent status (#146)
* chore: updated eslintrc.json to disable globals error * refactor: moved sendMessage logic to a separate file * feat: implemented message feedback from server * feat: added message sent status
- Loading branch information
1 parent
f869900
commit b9cdf8d
Showing
12 changed files
with
550 additions
and
294 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useState, useContext, useEffect } from 'react'; | ||
import { ThreeDots } from 'react-loading-icons'; | ||
import { SocketContext } from 'context/Context'; | ||
|
||
import Anonymous from 'components/Anonymous'; | ||
import { useAuth } from 'src/context/AuthContext'; | ||
import { useChat } from 'src/context/ChatContext'; | ||
|
||
const BuddyMatcher = () => { | ||
const { auth } = useAuth(); | ||
const { createChat } = useChat(); | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
const [isFound, setIsFound] = useState(false); | ||
const socket = useContext(SocketContext); | ||
|
||
const userID = auth.loginId; | ||
const defaultLoadingText = 'Looking for a random buddy'; | ||
const [loadingText, setLoadingText] = useState(defaultLoadingText); | ||
let timeout = null; | ||
|
||
useEffect(() => { | ||
if (loadingText === defaultLoadingText) { | ||
timeout = setTimeout(() => { | ||
setLoadingText( | ||
'Taking too long? No chat buddy is currently available :(\nTry refreshing!' | ||
); | ||
}, 15000); | ||
} | ||
|
||
return () => { | ||
clearTimeout(timeout); | ||
}; | ||
}, [loadingText]); | ||
|
||
useEffect(() => { | ||
if (isFound) { | ||
return; | ||
} | ||
|
||
if (!socket.connected) { | ||
socket.connect(); | ||
} | ||
|
||
// This is necessary else chat won't be restored after re-connections | ||
socket.on('connect', () => { | ||
// Here server will be informed that user is searching for | ||
// another user | ||
socket.emit('join', { loginId: auth.loginId, email: auth.email }); | ||
}); | ||
socket.connected && socket.emit('adding', { userID }); | ||
socket.emit('createRoom', `${userID}-in-search`); | ||
// From here will get the info from server that user has joined the room | ||
|
||
socket.on('joined', ({ roomId, userIds }) => { | ||
localStorage.setItem('currentChatId', roomId); | ||
|
||
createChat(roomId, userIds); | ||
setIsFound(true); | ||
}); | ||
|
||
socket.on('chat_restore', ({ chats, currentChatId }) => { | ||
localStorage.setItem('currentChatId', currentChatId); | ||
Object.values(chats).forEach((chat) => { | ||
createChat( | ||
chat.id, | ||
chat.userIds, | ||
chat.messages, | ||
chat.createdAt | ||
); | ||
}); | ||
|
||
setIsFound(true); | ||
console.log('chat restored!', chats, currentChatId); | ||
}); | ||
|
||
return () => { | ||
socket.off('connect').off('joined').off('chat_restore'); | ||
socket.disconnect(); | ||
}; | ||
}, []); | ||
|
||
return isFound ? ( | ||
<Anonymous /> | ||
) : ( | ||
<div className="flex w-full justify-center items-center h-screen flex-col bg-primary"> | ||
<ThreeDots fill="rgb(255 159 28)" /> | ||
<div className="text-lg text-center"> | ||
{loadingText.split('\n').map((text) => ( | ||
<p key={text}>{text}</p> | ||
))} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default BuddyMatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
b9cdf8d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
whisper – ./
anon-chat-app.vercel.app
whischat.vercel.app
whisper-git-main-dun-sin.vercel.app
whisper-dun-sin.vercel.app