Skip to content

Commit

Permalink
refactor(messageSeen.jsx): move observer to it's own hook
Browse files Browse the repository at this point in the history
Co-authored-by: Dunsin <78784850+Dun-sin@users.noreply.github.com>
  • Loading branch information
damirgros and Dun-sin authored Aug 20, 2024
1 parent 44f15a2 commit 49f6d5e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
42 changes: 13 additions & 29 deletions client/src/components/Chat/MessageSeen.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import PropTypes from 'prop-types';

import useIsTabActive from 'src/hooks/useIsTabActive';

import useChatUtils from 'src/lib/chatSocket';
import useObserver from 'src/hooks/useObserver';

import { useApp } from 'src/context/AppContext';
import { useChat } from 'src/context/ChatContext';
import { socket } from 'src/lib/socketConnection';

const MessageSeen = ({ isRead, isSender }) => {
const { app } = useApp();

const isTabVisible = useIsTabActive();

const { messages: state, receiveMessage } = useChat();
const { seenMessage } = useChatUtils(socket);
const observer = useObserver(isSender);

const { messages: state } = useChat();

const sortedMessages = useMemo(
() =>
Expand All @@ -27,28 +28,10 @@ const MessageSeen = ({ isRead, isSender }) => {
);

useEffect(() => {
// Initialize Intersection Observer
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting && !isSender) {
// Mark the message as read
const messageId = entry.target.getAttribute('id').split('-')[1];
try {
seenMessage({
messageId,
chatId: app.currentChatId,
});
} catch (e) {
return;
}
receiveMessage(messageId, app.currentChatId);
}
});
},
{ threshold: 0.5 }
// Trigger when 50% of the element is in the viewport
);
// Only proceed if the observer and sortedMessages are available
if (!observer || !sortedMessages.length) {
return;
}

sortedMessages.forEach((message) => {
if (message.isRead) {
Expand All @@ -62,10 +45,11 @@ const MessageSeen = ({ isRead, isSender }) => {
});

return () => {
// Clean up the observer
observer.disconnect();
if (observer) {
observer.disconnect();
}
};
}, [sortedMessages, isTabVisible]);
}, [sortedMessages, isTabVisible, observer]);

return isSender && <p className="text-sm">{isRead ? 'Seen' : 'Not Seen'}</p>;
};
Expand Down
39 changes: 39 additions & 0 deletions client/src/hooks/useObserver.js
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
};

0 comments on commit 49f6d5e

Please sign in to comment.