-
Notifications
You must be signed in to change notification settings - Fork 408
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(messageSeen.jsx): move observer to it's own hook
Co-authored-by: Dunsin <78784850+Dun-sin@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
52 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import useChatUtils from 'src/lib/chatSocket'; | ||
import { socket } from 'src/lib/socketConnection'; | ||
import { useChat } from 'src/context/ChatContext'; | ||
import { useApp } from 'src/context/AppContext'; | ||
import { useCallback } from 'react'; | ||
|
||
export default (isSender) => { | ||
|
||
const { seenMessage } = useChatUtils(socket); | ||
const { receiveMessage } = useChat(); | ||
const { app } = useApp(); | ||
|
||
const observerCallback = useCallback( | ||
(entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting && !isSender) { | ||
const messageId = entry.target.getAttribute('id').split('-')[1]; | ||
try { | ||
seenMessage({ | ||
messageId, | ||
chatId: app.currentChatId, | ||
}); | ||
} catch (e) { | ||
return; | ||
} | ||
receiveMessage(messageId, app.currentChatId); | ||
} | ||
}); | ||
}, | ||
[isSender, seenMessage, receiveMessage, app.currentChatId] | ||
); | ||
|
||
const observer = new IntersectionObserver(observerCallback, { | ||
threshold: 0.5, // Trigger when 50% of the element is in the viewport | ||
}); | ||
|
||
return observer | ||
}; | ||
|