From 80bd0b608da668124a36fe2b62bd5168fcee72d8 Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Thu, 28 Oct 2021 10:45:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BD=BF=E7=94=A8=E8=99=9A=E6=8B=9F?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=9B=BF=E6=8D=A2=E6=B8=B2=E6=9F=93=E7=9A=84?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E8=81=8A=E5=A4=A9=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ChatMessageList/VirtualizedList.tsx | 2 +- .../ChatBox/ChatMessageList/index.tsx | 128 ++---------------- web/src/components/ChatBox/index.tsx | 7 +- 3 files changed, 17 insertions(+), 120 deletions(-) diff --git a/web/src/components/ChatBox/ChatMessageList/VirtualizedList.tsx b/web/src/components/ChatBox/ChatMessageList/VirtualizedList.tsx index 1e6df3b7..d1b27da3 100644 --- a/web/src/components/ChatBox/ChatMessageList/VirtualizedList.tsx +++ b/web/src/components/ChatBox/ChatMessageList/VirtualizedList.tsx @@ -36,7 +36,7 @@ function findMessageIndexWithId( return messages.findIndex((m) => m._id === messageId); } -interface VirtualizedMessageListProps { +export interface VirtualizedMessageListProps { messages: ChatMessage[]; onUpdateReadedMessage: (lastMessageId: string) => void; } diff --git a/web/src/components/ChatBox/ChatMessageList/index.tsx b/web/src/components/ChatBox/ChatMessageList/index.tsx index cc948029..8158fc62 100644 --- a/web/src/components/ChatBox/ChatMessageList/index.tsx +++ b/web/src/components/ChatBox/ChatMessageList/index.tsx @@ -1,116 +1,18 @@ -import React, { - useCallback, - useEffect, - useImperativeHandle, - useRef, -} from 'react'; +import React from 'react'; import { - ChatMessage, - getMessageTimeDiff, - shouldShowMessageTime, - useUpdateRef, -} from 'tailchat-shared'; -import { ChatMessageItem } from './Item'; -import { Divider } from 'antd'; - -interface ChatMessageListProps { - messages: ChatMessage[]; - onUpdateReadedMessage: (lastMessageId: string) => void; -} -export interface ChatMessageListRef { - scrollToBottom: () => void; -} -export const ChatMessageList = React.forwardRef< - ChatMessageListRef, - ChatMessageListProps ->((props, ref) => { - const containerRef = useRef(null); - - useImperativeHandle(ref, () => ({ - scrollToBottom() { - requestAnimationFrame(() => { - if (!containerRef.current) { - return; - } - - containerRef.current.scrollTo({ - top: containerRef.current.scrollHeight, - behavior: 'smooth', - }); - }); - }, - })); - - const onUpdateReadedMessageRef = useUpdateRef(props.onUpdateReadedMessage); - useEffect(() => { - if (props.messages.length === 0) { - return; - } - - if (containerRef.current?.scrollTop === 0) { - // 当前列表在最低 - onUpdateReadedMessageRef.current( - props.messages[props.messages.length - 1]._id - ); - } - }, [props.messages.length]); - - const handleScroll = useCallback(() => { - if (props.messages.length === 0) { - return; - } - - if (containerRef.current?.scrollTop === 0) { - onUpdateReadedMessageRef.current( - props.messages[props.messages.length - 1]._id - ); - } - }, [props.messages]); - - return ( -
-
- {props.messages.map((message, index, arr) => { - let showDate = true; - let showAvatar = true; - const messageCreatedAt = new Date(message.createdAt ?? ''); - if (index > 0) { - // 当不是第一条数据时 - - // 进行时间合并 - const prevMessage = arr[index - 1]; - if ( - !shouldShowMessageTime( - new Date(prevMessage.createdAt ?? ''), - messageCreatedAt - ) - ) { - showDate = false; - } - - // 进行头像合并(在同一时间块下 且发送者为同一人) - if (showDate === false) { - showAvatar = prevMessage.author !== message.author; - } - } - - return ( -
- {showDate && ( - - {getMessageTimeDiff(messageCreatedAt)} - - )} - -
- ); - })} + VirtualizedMessageList, + VirtualizedMessageListProps, +} from './VirtualizedList'; + +export const ChatMessageList: React.FC = + React.memo((props) => { + return ( +
+
-
- ); -}); + ); + }); ChatMessageList.displayName = 'ChatMessageList'; diff --git a/web/src/components/ChatBox/index.tsx b/web/src/components/ChatBox/index.tsx index 1cbfdb74..b377a866 100644 --- a/web/src/components/ChatBox/index.tsx +++ b/web/src/components/ChatBox/index.tsx @@ -3,7 +3,7 @@ import { ChatBoxContextProvider, useConverseMessage } from 'tailchat-shared'; import { AlertErrorView } from '../AlertErrorView'; import { ChatBoxPlaceholder } from './ChatBoxPlaceholder'; import { ChatInputBox } from './ChatInputBox'; -import { ChatMessageList, ChatMessageListRef } from './ChatMessageList'; +import { ChatMessageList } from './ChatMessageList'; import { ChatReply } from './ChatReply'; import { useMessageAck } from './useMessageAck'; @@ -24,7 +24,6 @@ const ChatBoxInner: React.FC = React.memo((props) => { converseId, isGroup, }); - const chatMessageListRef = useRef(null); const { updateConverseAck } = useMessageAck(converseId, messages); if (loading) { @@ -38,7 +37,6 @@ const ChatBoxInner: React.FC = React.memo((props) => { return (
@@ -51,9 +49,6 @@ const ChatBoxInner: React.FC = React.memo((props) => { converseId: props.converseId, groupId: props.groupId, content: msg, - }).then(() => { - // 发送消息后滚动到底部 - chatMessageListRef.current?.scrollToBottom(); }); }} />