Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions apps/meteor/client/views/room/body/RoomBody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,19 @@ const RoomBody = (): ReactElement => {

const { messageListRef, messageListProps } = useMessageListNavigation();

const { handleNewMessageButtonClick, handleJumpToRecentButtonClick, handleComposerResize, hasNewMessages, newMessagesScrollRef } =
useHasNewMessages(room._id, user?._id, atBottomRef, {
sendToBottom,
sendToBottomIfNecessary,
isAtBottom,
});

const innerRef = useMergedRefs(
dateScrollInnerRef,
innerBoxRef,
restoreScrollPositionInnerRef,
isAtBottomInnerRef,
newMessagesScrollRef,
leaderBannerInnerRef,
unreadBarInnerRef,
getMoreInnerRef,
Expand All @@ -131,17 +139,6 @@ const RoomBody = (): ReactElement => {

const wrapperBoxRefs = useMergedRefs(unreadBarWrapperRef, leaderBannerWrapperRef);

const { handleNewMessageButtonClick, handleJumpToRecentButtonClick, handleComposerResize, hasNewMessages } = useHasNewMessages(
room._id,
user?._id,
atBottomRef,
{
sendToBottom,
sendToBottomIfNecessary,
isAtBottom,
},
);

const handleNavigateToPreviousMessage = useCallback((): void => {
chat.messageEditing.toPreviousMessage();
}, [chat.messageEditing]);
Expand Down
21 changes: 21 additions & 0 deletions apps/meteor/client/views/room/body/hooks/useHasNewMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useCallback, useEffect, useState } from 'react';

import { RoomHistoryManager } from '../../../../../app/ui-utils/client';
import { callbacks } from '../../../../../lib/callbacks';
import { withThrottling } from '../../../../../lib/utils/highOrderFunctions';
import { useChat } from '../../contexts/ChatContext';

export const useHasNewMessages = (
Expand Down Expand Up @@ -74,7 +75,27 @@ export const useHasNewMessages = (
};
}, [isAtBottom, rid, sendToBottom, uid]);

const ref = useCallback(
(node: HTMLElement | null) => {
if (!node) {
return;
}

node.addEventListener(
'scroll',
withThrottling({ wait: 100 })(() => {
atBottomRef.current && setHasNewMessages(false);
}),
{
passive: true,
},
);
},
[atBottomRef],
);

return {
newMessagesScrollRef: ref,
handleNewMessageButtonClick,
handleJumpToRecentButtonClick,
handleComposerResize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ export const useHandleUnread = (
room: IRoom,
subscription?: ISubscription,
): {
wrapperRef: (wrapper: HTMLDivElement | null) => void;
innerRef: React.MutableRefObject<HTMLDivElement | null>;
innerRef: (wrapper: HTMLDivElement | null) => void;
wrapperRef: React.MutableRefObject<HTMLDivElement | null>;
handleUnreadBarJumpToButtonClick: () => void;
handleMarkAsReadButtonClick: () => void;
counter: readonly [number, Date | undefined];
Expand Down Expand Up @@ -171,8 +171,8 @@ export const useHandleUnread = (
);

return {
wrapperRef: ref,
innerRef: messagesBoxRef,
innerRef: ref,
wrapperRef: messagesBoxRef,
handleUnreadBarJumpToButtonClick,
handleMarkAsReadButtonClick,
counter: [unread?.count ?? 0, unread?.since] as const,
Expand Down