Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

fix(chat): dismiss new-messages pill on scroll

Jaya Allamsetty39b441545928
diff --git a/react/features/chat/components/web/MessageContainer.tsx b/react/features/chat/components/web/MessageContainer.tsx
index c2bf85b1db..40f08d9eda 100644
--- a/react/features/chat/components/web/MessageContainer.tsx
+++ b/react/features/chat/components/web/MessageContainer.tsx
@@ -1,4 +1,4 @@
-import { throttle } from 'lodash-es';
+import { DebouncedFunc, throttle } from 'lodash-es';
import React, { Component, RefObject } from 'react';
import { scrollIntoView } from 'seamless-scroll-polyfill';
@@ -28,6 +28,11 @@ interface IProps {
interface IState {
+ /**
+ * Id of the first message that arrived while scrolled up; the new-messages pill targets it.
+ */
+ firstUnreadMessageId: string | null;
+
/**
* Whether or not message container has received new messages.
*/
@@ -37,11 +42,6 @@ interface IState {
* Whether or not scroll position is at the bottom of container.
*/
isScrolledToBottom: boolean;
-
- /**
- * The id of the last read message.
- */
- lastReadMessageId: string | null;
}
/**
@@ -55,9 +55,9 @@ export default class MessageContainer extends Component<IProps, IState> {
* and where to scroll when click on hasNewMessages button.
*/
override state: IState = {
+ firstUnreadMessageId: null,
hasNewMessages: false,
- isScrolledToBottom: true,
- lastReadMessageId: ''
+ isScrolledToBottom: true
};
/**
@@ -83,6 +83,11 @@ export default class MessageContainer extends Component<IProps, IState> {
*/
_hasInitiallyScrolled: boolean;
+ /**
+ * Throttled scroll handler; typed as {@code DebouncedFunc} so unmount can cancel a pending trailing call.
+ */
+ _onChatScroll: DebouncedFunc<() => void>;
+
static defaultProps = {
isVisible: true,
messages: [] as IMessage[]
@@ -104,8 +109,7 @@ export default class MessageContainer extends Component<IProps, IState> {
// Bind event handlers so they are only bound once for every instance.
this._handleIntersectBottomList = this._handleIntersectBottomList.bind(this);
this._findFirstUnreadMessage = this._findFirstUnreadMessage.bind(this);
- this._isMessageVisible = this._isMessageVisible.bind(this);
- this._onChatScroll = throttle(this._onChatScroll.bind(this), 300, { leading: true });
+ this._onChatScroll = throttle(this._handleChatScroll.bind(this), 300, { leading: true });
this._onGoToFirstUnreadMessage = this._onGoToFirstUnreadMessage.bind(this);
}
@@ -185,15 +189,20 @@ export default class MessageContainer extends Component<IProps, IState> {
* @returns {void}
*/
override componentDidUpdate(prevProps: IProps) {
- const newMessages = this.props.messages.filter(message => !prevProps.messages.includes(message));
- const hasLocalMessage = newMessages.map(message => message.messageType).includes(MESSAGE_TYPE_LOCAL);
+ // Diff by id: edits, reactions and moderation replace the message object without adding a message.
+ const prevIds = new Set(prevProps.messages.map(message => message.messageId));
+ const newMessages = this.props.messages.filter(message => !prevIds.has(message.messageId));
+ const hasLocalMessage = newMessages.some(message => message.messageType === MESSAGE_TYPE_LOCAL);
if (newMessages.length > 0) {
if (this.state.isScrolledToBottom || hasLocalMessage) {
this.scrollToElement(false, null);
} else {
// eslint-disable-next-line react/no-did-update-set-state
- this.setState({ hasNewMessages: true });
+ this.setState(prev => ({
+ firstUnreadMessageId: prev.hasNewMessages ? prev.firstUnreadMessageId : newMessages[0].messageId,
+ hasNewMessages: true
+ }));
}
}
@@ -217,6 +226,9 @@ export default class MessageContainer extends Component<IProps, IState> {
const target = document.querySelector('#messagesListEnd');
this._bottomListObserver.unobserve(target as Element);
+
+ // Drop any trailing throttled call so it does not setState or query the DOM post-unmount.
+ this._onChatScroll.cancel();
}
/**
@@ -239,32 +251,40 @@ export default class MessageContainer extends Component<IProps, IState> {
/**
- * Callback invoked to listen to current scroll position and update next unread message.
- * The callback is invoked inside a throttle with 300 ms to decrease the number of function calls.
+ * Dismisses the new-messages pill once the first unread message has scrolled into view.
*
* @private
* @returns {void}
*/
- _onChatScroll() {
+ _handleChatScroll() {
+ const container = this._messageListRef.current;
+
+ if (!this.state.hasNewMessages || !container) {
+ return;
+ }
+
const firstUnreadMessage = this._findFirstUnreadMessage();
- if (firstUnreadMessage && firstUnreadMessage.id !== this.state.lastReadMessageId) {
- this.setState({ lastReadMessageId: firstUnreadMessage?.id });
+ // Keep the pill until the first unread message is rendered and its top edge is inside the viewport.
+ if (!firstUnreadMessage
+ || firstUnreadMessage.getBoundingClientRect().top >= container.getBoundingClientRect().bottom) {
+ return;
}
+
+ this.setState({
+ firstUnreadMessageId: null,
+ hasNewMessages: false
+ });
}
/**
- * Find the first unread message.
- * Update component state and scroll to element.
+ * Scrolls to the first unread message.
*
* @private
* @returns {void}
*/
_onGoToFirstUnreadMessage() {
- const firstUnreadMessage = this._findFirstUnreadMessage();
-
- this.setState({ lastReadMessageId: firstUnreadMessage?.id || null });
- this.scrollToElement(true, firstUnreadMessage as Element);
+ this.scrollToElement(true, this._findFirstUnreadMessage());
}
/**
@@ -288,82 +308,39 @@ export default class MessageContainer extends Component<IProps, IState> {
}
}
- /** .
- * _HandleIntersectBottomList.
- * When entry is intersecting with bottom of container set last message as last read message.
- * When entry is not intersecting update only isScrolledToBottom with false value.
- *
- * @param {Array} entries - List of entries.
- * @private
- * @returns {void}
- */
+ /**
+ * Tracks whether the bottom of the list is in view; reaching it dismisses the new-messages pill.
+ *
+ * @param {Array} entries - List of entries.
+ * @private
+ * @returns {void}
+ */
_handleIntersectBottomList(entries: IntersectionObserverEntry[]) {
entries.forEach((entry: IntersectionObserverEntry) => {
- if (entry.isIntersecting && this.props.messages.length) {
- const lastMessageIndex = this.props.messages.length - 1;
- const lastMessage = this.props.messages[lastMessageIndex];
- const lastReadMessageId = lastMessage.messageId;
-
- this.setState(
- {
- isScrolledToBottom: true,
- hasNewMessages: false,
- lastReadMessageId
- });
- }
-
- if (!entry.isIntersecting) {
- this.setState(
- {
- isScrolledToBottom: false
- });
+ if (entry.isIntersecting) {
+ this.setState({
+ firstUnreadMessageId: null,
+ hasNewMessages: false,
+ isScrolledToBottom: true
+ });
+ } else {
+ this.setState({ isScrolledToBottom: false });
}
});
}
/**
- * Find first unread message.
- * MessageIsAfterLastSeenMessage filter elements which are not visible but are before the last read message.
- *
- * @private
- * @returns {Element}
- */
- _findFirstUnreadMessage() {
- const messagesNodeList = document.querySelectorAll('.chatmessage-wrapper');
-
- // @ts-ignore
- const messagesToArray = [ ...messagesNodeList ];
-
- const previousIndex = messagesToArray.findIndex((message: Element) =>
- message.id === this.state.lastReadMessageId);
-
- if (previousIndex !== -1) {
- for (let i = previousIndex; i < messagesToArray.length; i++) {
- if (!this._isMessageVisible(messagesToArray[i])) {
- return messagesToArray[i];
- }
- }
- }
- }
-
- /**
- * Check if a message is visible in view.
+ * Returns the rendered element of the first unread message, or null if there is none.
*
- * @param {Element} message - The message.
- *
- * @returns {boolean}
+ * @private
+ * @returns {Element|null}
*/
- _isMessageVisible(message: Element): boolean {
- const { bottom, height, top } = message.getBoundingClientRect();
-
- if (this._messageListRef.current) {
- const containerRect = this._messageListRef.current.getBoundingClientRect();
-
- return top <= containerRect.top
- ? containerRect.top - top <= height : bottom - containerRect.bottom <= height;
- }
+ _findFirstUnreadMessage(): Element | null {
+ const { firstUnreadMessageId } = this.state;
- return false;
+ return firstUnreadMessageId
+ ? this._messageListRef.current?.querySelector(`#${CSS.escape(firstUnreadMessageId)}`) ?? null
+ : null;
}
/**
diff --git a/react/features/chat/reducer.ts b/react/features/chat/reducer.ts
index 62796cbeaf..bdf14f7334 100644
--- a/react/features/chat/reducer.ts
+++ b/react/features/chat/reducer.ts
@@ -1,3 +1,5 @@
+import { v4 as uuidv4 } from 'uuid';
+
import { UPDATE_CONFERENCE_METADATA } from '../base/conference/actionTypes';
import { ILocalParticipant, IParticipant } from '../base/participants/types';
import ReducerRegistry from '../base/redux/ReducerRegistry';
@@ -97,7 +99,8 @@ ReducerRegistry.register<IChatState>('features/chat', (state = DEFAULT_STATE, ac
participantId: action.participantId,
isEdited: Boolean(action.isEdited),
isReaction: action.isReaction,
- messageId: action.messageId,
+ // Reaction, system and error messages carry no id; give them one so every message is addressable.
+ messageId: action.messageId ?? uuidv4(),
messageType: action.messageType,
message: action.message,
reactions: action.reactions,