Skip to content

Commit

Permalink
[FIX] "Jump to first unread" banner does not always appear when the f…
Browse files Browse the repository at this point in the history
…irst unread message is outside of the visible screen (#27941)
  • Loading branch information
Educg550 committed Feb 14, 2023
1 parent ee4fa5d commit a0f4950
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 31 deletions.
38 changes: 16 additions & 22 deletions apps/meteor/client/views/room/components/body/RoomBody.tsx
Expand Up @@ -55,7 +55,7 @@ const RoomBody = (): ReactElement => {
const admin = useRole('admin');
const subscription = useRoomSubscription();

const [lastMessage, setLastMessage] = useState<Date | undefined>();
const [lastMessageDate, setLastMessageDate] = useState<Date | undefined>();
const [hideLeaderHeader, setHideLeaderHeader] = useState(false);
const [hasNewMessages, setHasNewMessages] = useState(false);

Expand Down Expand Up @@ -179,17 +179,19 @@ const RoomBody = (): ReactElement => {

const handleUnreadBarJumpToButtonClick = useCallback(() => {
const rid = room._id;
const { firstUnread } = RoomHistoryManager.getRoom(rid) ?? {};
const { firstUnread } = RoomHistoryManager.getRoom(rid);
let message = firstUnread?.get();
if (!message) {
message = ChatMessage.findOne({ rid, ts: { $gt: subscription?.ls } }, { sort: { ts: 1 }, limit: 1 });
message = ChatMessage.findOne({ rid, ts: { $gt: unread?.since } }, { sort: { ts: 1 }, limit: 1 });
}
RoomHistoryManager.getSurroundingMessages(message, atBottomRef);
}, [room._id, subscription?.ls]);
setUnreadCount(0);
}, [room._id, unread?.since, setUnreadCount]);

const handleMarkAsReadButtonClick = useCallback(() => {
readMessage.readNow(room._id);
}, [room._id]);
setUnreadCount(0);
}, [room._id, setUnreadCount]);

const handleUploadProgressClose = useCallback(
(id: Upload['id']) => {
Expand Down Expand Up @@ -291,11 +293,11 @@ const RoomBody = (): ReactElement => {

const count = ChatMessage.find({
rid: room._id,
ts: { $lte: lastMessage, $gt: subscription?.ls },
ts: { $lte: lastMessageDate, $gt: subscription?.ls },
}).count();

setUnreadCount(count);
}, [lastMessage, room._id, setUnreadCount, subscribed, subscription?.ls]);
count && setUnreadCount(count);
}, [lastMessageDate, room._id, setUnreadCount, subscribed, subscription?.ls]);

useEffect(() => {
if (!unread?.count) {
Expand All @@ -304,15 +306,6 @@ const RoomBody = (): ReactElement => {
readMessage.refreshUnreadMark(room._id);
}, [debouncedReadMessageRead, room._id, unread?.count]);

useEffect(() => {
const handleReadMessage = (): void => setUnreadCount(0);
readMessage.on(room._id, handleReadMessage);

return () => {
readMessage.off(room._id, handleReadMessage);
};
}, [room._id, setUnreadCount]);

useEffect(() => {
const wrapper = wrapperRef.current;

Expand All @@ -338,7 +331,7 @@ const RoomBody = (): ReactElement => {
element = document.elementFromPoint(messagesBoxLeft + 1, messagesBoxTop + topOffset + 1);
}

if (element?.classList.contains('message')) {
if (element?.classList.contains('rcx-message') || element?.classList.contains('rcx-message--sequential')) {
return element;
}
};
Expand All @@ -347,7 +340,7 @@ const RoomBody = (): ReactElement => {
Tracker.afterFlush(() => {
const lastInvisibleMessageOnScreen = getElementFromPoint(0) || getElementFromPoint(20) || getElementFromPoint(40);

if (!lastInvisibleMessageOnScreen || !lastInvisibleMessageOnScreen.id) {
if (!lastInvisibleMessageOnScreen) {
setUnreadCount(0);
return;
}
Expand All @@ -358,7 +351,7 @@ const RoomBody = (): ReactElement => {
return;
}

setLastMessage(lastMessage.ts);
setLastMessageDate(lastMessage.ts);
});
});

Expand Down Expand Up @@ -536,14 +529,15 @@ const RoomBody = (): ReactElement => {
<div className='messages-container-main' {...fileUploadTriggerProps}>
<DropTargetOverlay {...fileUploadOverlayProps} />
<div className={['container-bars', (unread || uploads.length) && 'show'].filter(isTruthy).join(' ')}>
{unread ? (
{unread && (
<UnreadMessagesIndicator
count={unread.count}
since={unread.since}
onJumpButtonClick={handleUnreadBarJumpToButtonClick}
onMarkAsReadButtonClick={handleMarkAsReadButtonClick}
/>
) : null}
)}

{uploads.map((upload) => (
<UploadProgressIndicator
key={upload.id}
Expand Down
Expand Up @@ -5,17 +5,14 @@ import { useCallback, useMemo, useState } from 'react';
import { RoomManager, RoomHistoryManager } from '../../../../../app/ui-utils/client';
import { useReactiveValue } from '../../../../hooks/useReactiveValue';

interface IUnreadMessages {
count: number;
since: Date;
}

export const useUnreadMessages = (
room: IRoom,
): readonly [
data:
| {
count: number;
since: Date;
}
| undefined,
setUnreadCount: Dispatch<SetStateAction<number>>,
] => {
): readonly [data: IUnreadMessages | undefined, setUnreadCount: Dispatch<SetStateAction<number>>] => {
const notLoadedCount = useReactiveValue(useCallback(() => RoomHistoryManager.getRoom(room._id).unreadNotLoaded.get(), [room._id]));
const [loadedCount, setLoadedCount] = useState(0);

Expand Down

0 comments on commit a0f4950

Please sign in to comment.