From 61b78b8f1bf2c5f4318debf28292e0b695b02711 Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Mon, 20 Feb 2023 12:00:49 -0800 Subject: [PATCH] Limit the number of chat messages that can collapse in a row --- .../chat/ChatContainer/ChatContainer.tsx | 56 ++++++++++++++----- 1 file changed, 41 insertions(+), 15 deletions(-) diff --git a/web/components/chat/ChatContainer/ChatContainer.tsx b/web/components/chat/ChatContainer/ChatContainer.tsx index 11557ea33..4a7e8d039 100644 --- a/web/components/chat/ChatContainer/ChatContainer.tsx +++ b/web/components/chat/ChatContainer/ChatContainer.tsx @@ -27,7 +27,11 @@ export type ChatContainerProps = { height?: string; }; -function shouldCollapseMessages(messages: ChatMessage[], index: number): boolean { +function shouldCollapseMessages( + messages: ChatMessage[], + index: number, + collapsedMessageIds: Set, +): boolean { if (messages.length < 2) { return false; } @@ -56,7 +60,17 @@ function shouldCollapseMessages(messages: ChatMessage[], index: number): boolean return false; } - return id === lastMessage?.user.id; + if (id !== lastMessage?.user.id) { + return false; + } + + // Limit the number of messages that can be collapsed in a row. + const maxCollapsedMessageCount = 5; + if (collapsedMessageIds.size >= maxCollapsedMessageCount) { + return false; + } + + return true; } function checkIsModerator(message: ChatMessage | ConnectedClientInfoEvent) { @@ -86,6 +100,8 @@ export const ChatContainer: FC = ({ const showScrollToBottomButtonDelay = useRef(null); const scrollToBottomDelay = useRef(null); + const collapsedMessageIds = new Set(); + const setShowScrolltoBottomButtonWithDelay = (show: boolean) => { showScrollToBottomButtonDelay.current = setTimeout(() => { setShowScrollToBottomButton(show); @@ -136,25 +152,35 @@ export const ChatContainer: FC = ({ return ; }; + const getUserChatMessageView = (index: number, message: ChatMessage) => { + const collapsed = shouldCollapseMessages(messages, index, collapsedMessageIds); + if (!collapsed) { + collapsedMessageIds.clear(); + } else { + collapsedMessageIds.add(message.id); + } + + return ( + + ); + }; const getViewForMessage = ( index: number, message: ChatMessage | NameChangeEvent | ConnectedClientInfoEvent | FediverseEvent, ) => { switch (message.type) { case MessageType.CHAT: - return ( - - ); + return getUserChatMessageView(index, message as ChatMessage); case MessageType.NAME_CHANGE: return ; case MessageType.CONNECTED_USER_INFO: