Skip to content

Commit

Permalink
fix scroll bug (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
glowingjade authored Oct 11, 2024
1 parent 986468a commit e28d77c
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
useCallback,
useEffect,
useImperativeHandle,
useLayoutEffect,
useRef,
useState,
} from 'react'
Expand Down Expand Up @@ -112,14 +111,14 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
}
}

useLayoutEffect(() => {
const handleScrollToBottom = () => {
if (chatMessagesRef.current) {
const scrollContainer = chatMessagesRef.current
if (scrollContainer.scrollTop !== scrollContainer.scrollHeight) {
scrollContainer.scrollTop = scrollContainer.scrollHeight
}
}
}, [chatMessages])
}

const abortActiveStreams = () => {
for (const abortController of activeStreamAbortControllersRef.current) {
Expand Down Expand Up @@ -194,6 +193,7 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
: message,
),
)
handleScrollToBottom()
}
} catch (error) {
if (error.name === 'AbortError') {
Expand Down Expand Up @@ -488,6 +488,7 @@ const Chat = forwardRef<ChatRef, ChatProps>((props, ref) => {
handleSubmit([...chatMessages, { ...inputMessage, content }])
chatUserInputRefs.current.get(inputMessage.id)?.clear()
setInputMessage(getNewInputMessage(app))
handleScrollToBottom()
}}
onFocus={() => {
setFocusedMessageId(inputMessage.id)
Expand Down

0 comments on commit e28d77c

Please sign in to comment.