Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions libs/chatbot/lib/components/ChatBot/ChatBotWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ const ChatBotWindow = ({
);
const [isConfirmModalOpen, setIsConfirmModalOpen] = React.useState(false);
const scrollToBottomRef = React.useRef<HTMLDivElement>(null);
const hasInitiallyScrolled = React.useRef(false);

const startNewChat = () => {
setConversationId(undefined);
Expand All @@ -115,10 +116,17 @@ const ChatBotWindow = ({
}
};

const scrollToBottom = React.useCallback(() => {
scrollToBottomRef.current?.scrollIntoView({ behavior: 'smooth' });
const scrollToBottom = React.useCallback((behavior: ScrollBehavior = 'smooth') => {
scrollToBottomRef.current?.scrollIntoView({ behavior });
}, []);

React.useEffect(() => {
// Determine scroll behavior: auto for initial render with existing messages, smooth for new content
const scrollBehavior = !hasInitiallyScrolled.current && messages.length > 0 ? 'auto' : 'smooth';
scrollToBottom(scrollBehavior);
hasInitiallyScrolled.current = true;
}, [messages, scrollToBottom]);

const handleSend = async (message: string | number) => {
setError(undefined);
setIsLoading(true);
Expand Down Expand Up @@ -253,10 +261,6 @@ const ChatBotWindow = ({
[onApiCall, conversationId, messages],
);

React.useEffect(() => {
scrollToBottomRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);

return (
<Chatbot displayMode={ChatbotDisplayMode.default}>
<ChatbotHeader>
Expand Down
Loading