Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: stop auto scroll if user manually scrolling up #2874

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
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
30 changes: 27 additions & 3 deletions web/containers/ListContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,49 @@
import { ReactNode, useEffect, useRef } from 'react'
import { ReactNode, useCallback, useEffect, useRef } from 'react'

type Props = {
children: ReactNode
}

const ListContainer: React.FC<Props> = ({ children }) => {
const listRef = useRef<HTMLDivElement>(null)
const prevScrollTop = useRef(0)
const isUserManuallyScrollingUp = useRef(false)

const handleScroll = useCallback((event: React.UIEvent<HTMLElement>) => {
const currentScrollTop = event.currentTarget.scrollTop

if (prevScrollTop.current > currentScrollTop) {
console.debug('User is manually scrolling up')
isUserManuallyScrollingUp.current = true
} else {
const currentScrollTop = event.currentTarget.scrollTop
const scrollHeight = event.currentTarget.scrollHeight
const clientHeight = event.currentTarget.clientHeight

if (currentScrollTop + clientHeight >= scrollHeight) {
console.debug('Scrolled to the bottom')
isUserManuallyScrollingUp.current = false
}
}

prevScrollTop.current = currentScrollTop
}, [])

useEffect(() => {
const scrollHeight = listRef.current?.scrollHeight ?? 0
if (isUserManuallyScrollingUp.current === true) return

const scrollHeight = listRef.current?.scrollHeight ?? 0
listRef.current?.scrollTo({
top: scrollHeight,
behavior: 'instant',
})
})
}, [listRef.current?.scrollHeight, isUserManuallyScrollingUp])

return (
<div
ref={listRef}
className="flex h-full w-full flex-col overflow-y-scroll"
onScroll={handleScroll}
>
{children}
</div>
Expand Down
4 changes: 2 additions & 2 deletions web/screens/Chat/ChatBody/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ const ChatBody: React.FC = () => {
const downloadedModels = useAtomValue(downloadedModelsAtom)
const loadModelError = useAtomValue(loadModelErrorAtom)

if (downloadedModels.length === 0) return <EmptyModel />
if (messages.length === 0) return <EmptyThread />
if (!downloadedModels.length) return <EmptyModel />
if (!messages.length) return <EmptyThread />

return (
<ListContainer>
Expand Down
Loading