mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Auto-scroll during streaming after user sends a message
This commit is contained in:
1 parent
8317eb9e98
commit
652b08d363
1 file changed
+28
-1
@@ -36,10 +36,14 @@ export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID,
|
||||
const { data: messages, isLoading, error } = useMessages(opcodeUrl, sessionID, directory)
|
||||
const userScrolledUpRef = useRef(false)
|
||||
const hasInitialScrolledRef = useRef(false)
|
||||
const lastMessageCountRef = useRef(0)
|
||||
const isFollowingRef = useRef(true)
|
||||
|
||||
useEffect(() => {
|
||||
hasInitialScrolledRef.current = false
|
||||
userScrolledUpRef.current = false
|
||||
lastMessageCountRef.current = 0
|
||||
isFollowingRef.current = true
|
||||
}, [sessionID])
|
||||
|
||||
const scrollToBottom = useCallback(() => {
|
||||
@@ -57,6 +61,11 @@ export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID,
|
||||
const handleScroll = () => {
|
||||
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight
|
||||
const isScrolledUp = distanceFromBottom > SCROLL_THRESHOLD
|
||||
|
||||
if (isScrolledUp) {
|
||||
isFollowingRef.current = false
|
||||
}
|
||||
|
||||
if (userScrolledUpRef.current !== isScrolledUp) {
|
||||
userScrolledUpRef.current = isScrolledUp
|
||||
onScrollStateChange?.(isScrolledUp)
|
||||
@@ -70,9 +79,27 @@ export const MessageThread = memo(function MessageThread({ opcodeUrl, sessionID,
|
||||
useEffect(() => {
|
||||
if (!containerRef?.current || !messages) return
|
||||
|
||||
if (!hasInitialScrolledRef.current && messages.length > 0) {
|
||||
const currentCount = messages.length
|
||||
const prevCount = lastMessageCountRef.current
|
||||
lastMessageCountRef.current = currentCount
|
||||
|
||||
if (!hasInitialScrolledRef.current && currentCount > 0) {
|
||||
hasInitialScrolledRef.current = true
|
||||
scrollToBottom()
|
||||
return
|
||||
}
|
||||
|
||||
if (currentCount > prevCount) {
|
||||
const newMessage = messages[currentCount - 1]
|
||||
if (newMessage?.info.role === 'user') {
|
||||
isFollowingRef.current = true
|
||||
scrollToBottom()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if (isFollowingRef.current) {
|
||||
scrollToBottom()
|
||||
}
|
||||
}, [messages, containerRef, scrollToBottom])
|
||||
|
||||
|
||||
Reference in new issue
Block a user