From 4cce6de437d5b9d9fe7766b489a3b1eb68b06453 Mon Sep 17 00:00:00 2001 From: anpigon Date: Mon, 27 May 2024 20:06:56 +0900 Subject: [PATCH] fix(square): Add scroll behavior to message container and autofocus chat box --- src/features/chatbot/chatbot.tsx | 12 ++++- src/features/chatbot/components/chat-box.tsx | 1 + src/main.ts | 51 +++++++++----------- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/features/chatbot/chatbot.tsx b/src/features/chatbot/chatbot.tsx index 4caed1b..07ee874 100644 --- a/src/features/chatbot/chatbot.tsx +++ b/src/features/chatbot/chatbot.tsx @@ -14,6 +14,7 @@ import {useChatbotState} from './context'; import {useCurrentModel} from './hooks/use-current-model'; import {useGetAiModels} from './hooks/use-get-ai-models'; import {useLLM} from './hooks/use-llm'; +import useOnceEffect from '@/hooks/useOnceEffect'; export const Chatbot: React.FC = () => { const app = useApp(); @@ -49,9 +50,18 @@ export const Chatbot: React.FC = () => { }); const scrollToBottom = () => { - messageContainerRef.current?.scrollTo(0, messageContainerRef.current.scrollHeight); + // messageContainerRef.current?.scrollTo(0, messageContainerRef.current.scrollHeight); + messageContainerRef.current?.scroll({ + top: messageContainerRef.current?.scrollHeight, + behavior: 'smooth', + }); }; + useOnceEffect(() => { + chatBoxRef.current?.focus(); + scrollToBottom(); + }); + useEffect(() => { messageEndRef.current?.scrollIntoView({behavior: 'smooth', block: 'end'}); }, [messages]); diff --git a/src/features/chatbot/components/chat-box.tsx b/src/features/chatbot/components/chat-box.tsx index 2726d45..39ab8ab 100644 --- a/src/features/chatbot/components/chat-box.tsx +++ b/src/features/chatbot/components/chat-box.tsx @@ -35,6 +35,7 @@ export const ChatBox = forwardRef(({controlle