fix: auto scrolling to bottom (#4256)

This commit is contained in:
Faisal Amir 2024-12-10 13:34:59 +08:00 committed by GitHub
parent c15bb9e9b4
commit 09bfc0549e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,4 +1,4 @@
import { memo, useEffect, useMemo, useRef, useState } from 'react' import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { ThreadMessage } from '@janhq/core' import { ThreadMessage } from '@janhq/core'
import { useVirtualizer } from '@tanstack/react-virtual' import { useVirtualizer } from '@tanstack/react-virtual'
@ -58,6 +58,8 @@ const ChatBody = memo(
}) => { }) => {
// The scrollable element for your list // The scrollable element for your list
const parentRef = useRef(null) const parentRef = useRef(null)
const prevScrollTop = useRef(0)
const isUserManuallyScrollingUp = useRef(false)
const count = useMemo( const count = useMemo(
() => (messages?.length ?? 0) + (loadModelError ? 1 : 0), () => (messages?.length ?? 0) + (loadModelError ? 1 : 0),
@ -72,27 +74,61 @@ const ChatBody = memo(
overscan: 5, overscan: 5,
}) })
useEffect(() => { useEffect(() => {
if (isUserManuallyScrollingUp.current === true || !parentRef.current)
return
if (count > 0 && messages && virtualizer) { if (count > 0 && messages && virtualizer) {
virtualizer.scrollToIndex(count - 1) virtualizer.scrollToIndex(count - 1)
} }
}, [count, virtualizer, messages, loadModelError]) }, [
count,
virtualizer,
messages,
loadModelError,
isUserManuallyScrollingUp,
])
const items = virtualizer.getVirtualItems() const items = virtualizer.getVirtualItems()
virtualizer.shouldAdjustScrollPositionOnItemSizeChange = ( virtualizer.shouldAdjustScrollPositionOnItemSizeChange = (
item, item,
_, _,
instance instance
) => { ) => {
if (isUserManuallyScrollingUp.current === true) return false
return ( return (
// item.start < (instance.scrollOffset ?? 0) && // item.start < (instance.scrollOffset ?? 0) &&
instance.scrollDirection !== 'backward' instance.scrollDirection !== 'backward'
) )
} }
const handleScroll = useCallback((event: React.UIEvent<HTMLElement>) => {
const currentScrollTop = event.currentTarget.scrollTop
if (prevScrollTop.current > currentScrollTop) {
isUserManuallyScrollingUp.current = true
} else {
const currentScrollTop = event.currentTarget.scrollTop
const scrollHeight = event.currentTarget.scrollHeight
const clientHeight = event.currentTarget.clientHeight
if (currentScrollTop + clientHeight >= scrollHeight) {
isUserManuallyScrollingUp.current = false
}
}
if (isUserManuallyScrollingUp.current === true) {
event.preventDefault()
event.stopPropagation()
}
prevScrollTop.current = currentScrollTop
}, [])
return ( return (
<div className="flex h-full w-full flex-col overflow-x-hidden"> <div className="flex h-full w-full flex-col overflow-x-hidden">
<div <div
ref={parentRef} ref={parentRef}
onScroll={handleScroll}
className="List" className="List"
style={{ style={{
flex: 1, flex: 1,