From 8976fe85e0c14cf0fd3436f296dbad64e8c5b086 Mon Sep 17 00:00:00 2001 From: agatha197 Date: Fri, 3 Jan 2025 20:19:09 +0900 Subject: [PATCH] [FR-46] fix: The `ScrollBottomHandlerButton` is displayed even when the chat message is empty. --- .../VirtualChatMessageList.tsx | 95 +++++++++++-------- 1 file changed, 57 insertions(+), 38 deletions(-) diff --git a/react/src/components/lablupTalkativotUI/VirtualChatMessageList.tsx b/react/src/components/lablupTalkativotUI/VirtualChatMessageList.tsx index 755436f90c..081619a4d9 100644 --- a/react/src/components/lablupTalkativotUI/VirtualChatMessageList.tsx +++ b/react/src/components/lablupTalkativotUI/VirtualChatMessageList.tsx @@ -22,6 +22,7 @@ const VirtualChatMessageList: React.FC = ({ }) => { const virtuosoRef = useRef(null); const [atBottom, setAtBottom] = useState(true); + const [userScrolled, setUserScrolled] = useState(false); const { token } = theme.useToken(); // overscan should be 1.5 times the height of the window const overscan = typeof window !== 'undefined' ? window.innerHeight * 1.5 : 0; @@ -33,7 +34,12 @@ const VirtualChatMessageList: React.FC = ({ style={{ height: '100%', flex: 1 }} > { + setAtBottom(isAtBottom); + if (isAtBottom) { + setUserScrolled(false); + } + }} atBottomThreshold={60} computeItemKey={(_, item) => item.id} data={messages} @@ -80,45 +86,58 @@ const VirtualChatMessageList: React.FC = ({ }} overscan={overscan} ref={virtuosoRef} - /> -
{ + if (!atBottom) { + setUserScrolled(true); + } }} - > - { - const virtuoso = virtuosoRef.current; - switch (type) { - case 'auto': { - virtuoso?.scrollToIndex({ - align: 'end', - behavior: 'auto', - index: 'LAST', - }); - break; - } - case 'click': { - virtuoso?.scrollToIndex({ - align: 'end', - behavior: 'smooth', - index: 'LAST', - }); - break; - } - } + onWheel={() => { + if (!atBottom) { + setUserScrolled(true); + } + }} + /> + {!_.isEmpty(messages) && ( +
-
+ > + { + const virtuoso = virtuosoRef.current; + switch (type) { + case 'auto': { + virtuoso?.scrollToIndex({ + align: 'end', + behavior: 'auto', + index: 'LAST', + }); + break; + } + case 'click': { + virtuoso?.scrollToIndex({ + align: 'end', + behavior: 'smooth', + index: 'LAST', + }); + setUserScrolled(false); + break; + } + } + }} + lastMessageContent={_.get(_.last(messages), 'content')} + /> +
+ )} ); };