From 72870cb520ac66d54fe5ff3a1d519b53bb8babf6 Mon Sep 17 00:00:00 2001 From: Pete Miller Date: Wed, 5 Apr 2023 01:22:55 -0700 Subject: [PATCH] AI Chat: scroll new conversation entry in to view --- .../components/conversation_list/index.tsx | 36 +++++++++++++++++-- .../components/main/style.module.scss | 3 -- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/components/ai_chat_ui/components/conversation_list/index.tsx b/components/ai_chat_ui/components/conversation_list/index.tsx index 4787d86d160..65cfb83e123 100644 --- a/components/ai_chat_ui/components/conversation_list/index.tsx +++ b/components/ai_chat_ui/components/conversation_list/index.tsx @@ -15,7 +15,34 @@ interface ConversationListProps { isLoading: boolean } +const elementScrollBehavior: ScrollIntoViewOptions = { + behavior: 'smooth', + block: 'start' +} + function ConversationList (props: ConversationListProps) { + // Scroll the last conversation item in to view when entries are added. + const lastConversationEntryElementRef = React.useRef(null) + const loadingElementRef = React.useRef(null) + React.useEffect(() => { + if (!props.list.length && !props.isLoading) { + return + } + if (props.isLoading) { + if (!loadingElementRef.current) { + console.error('Conversation loading element did not exist when expected') + } else { + loadingElementRef.current.scrollIntoView(elementScrollBehavior) + } + return + } + if (!lastConversationEntryElementRef.current) { + console.error('Conversation entry element did not exist when expected') + } else { + lastConversationEntryElementRef.current.scrollIntoView(elementScrollBehavior) + } + }, [props.list.length, props.isLoading]) + return (
{props.list.map((turn, id) => { @@ -24,8 +51,13 @@ function ConversationList (props: ConversationListProps) { [styles.turnHuman]: turn.characterType === CharacterType.HUMAN, }) + const isLastEntry = (id === (props.list.length - 1)) + const elementRef = isLastEntry + ? lastConversationEntryElementRef + : null + return ( -
+

{turn.text}

@@ -33,7 +65,7 @@ function ConversationList (props: ConversationListProps) { ) })} {props.isLoading && ( -
+

{getLocale('loadingLabel')} diff --git a/components/ai_chat_ui/components/main/style.module.scss b/components/ai_chat_ui/components/main/style.module.scss index 2953fdcedde..c134faf5c23 100644 --- a/components/ai_chat_ui/components/main/style.module.scss +++ b/components/ai_chat_ui/components/main/style.module.scss @@ -13,13 +13,10 @@ } .inputBox { - position: absolute; - bottom: 0; width: 100%; } .scroller { overflow: auto; height: 100%; - padding-bottom: 150px; }