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: auto scrolling to bottom #4256

Merged
merged 1 commit into from
Dec 10, 2024
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
40 changes: 38 additions & 2 deletions web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx
Original file line number Diff line number Diff line change
@@ -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 { useVirtualizer } from '@tanstack/react-virtual'
Expand All @@ -17,30 +17,30 @@
import { activeThreadAtom } from '@/helpers/atoms/Thread.atom'

const ChatConfigurator = memo(() => {
const messages = useAtomValue(getCurrentChatMessagesAtom)
const currentThread = useAtomValue(activeThreadAtom)

Check warning on line 21 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

20-21 lines are not covered with tests

const [current, setCurrent] = useState<ThreadMessage[]>([])
const loadModelError = useAtomValue(loadModelErrorAtom)

Check warning on line 24 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

23-24 lines are not covered with tests

const isMessagesIdentificial = (

Check warning on line 26 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

26 line is not covered with tests
arr1: ThreadMessage[],
arr2: ThreadMessage[]
): boolean => {
if (arr1.length !== arr2.length) return false
return arr1.every((item, index) => item.id === arr2[index].id)

Check warning on line 31 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

30-31 lines are not covered with tests
}

useEffect(() => {
if (

Check warning on line 35 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

34-35 lines are not covered with tests
!isMessagesIdentificial(messages, current) ||
messages.some((e) => e.thread_id !== currentThread?.id)

Check warning on line 37 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

37 line is not covered with tests
) {
setCurrent(messages)

Check warning on line 39 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

39 line is not covered with tests
}
}, [messages, current, loadModelError, currentThread])

if (!messages.length) return <EmptyThread />

Check warning on line 43 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

43 line is not covered with tests
return (
<div className="flex h-full w-full flex-col">
<ChatBody loadModelError={loadModelError} messages={current} />
Expand All @@ -57,10 +57,12 @@
loadModelError?: string
}) => {
// The scrollable element for your list
const parentRef = useRef(null)
const prevScrollTop = useRef(0)
const isUserManuallyScrollingUp = useRef(false)

Check warning on line 62 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

60-62 lines are not covered with tests

const count = useMemo(
() => (messages?.length ?? 0) + (loadModelError ? 1 : 0),

Check warning on line 65 in web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx

View workflow job for this annotation

GitHub Actions / coverage-check

64-65 lines are not covered with tests
[messages, loadModelError]
)

Expand All @@ -72,27 +74,61 @@
overscan: 5,
})
useEffect(() => {
if (isUserManuallyScrollingUp.current === true || !parentRef.current)
return

if (count > 0 && messages && virtualizer) {
virtualizer.scrollToIndex(count - 1)
}
}, [count, virtualizer, messages, loadModelError])
}, [
count,
virtualizer,
messages,
loadModelError,
isUserManuallyScrollingUp,
])

const items = virtualizer.getVirtualItems()

virtualizer.shouldAdjustScrollPositionOnItemSizeChange = (
item,
_,
instance
) => {
if (isUserManuallyScrollingUp.current === true) return false
return (
// item.start < (instance.scrollOffset ?? 0) &&
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 (
<div className="flex h-full w-full flex-col overflow-x-hidden">
<div
ref={parentRef}
onScroll={handleScroll}
className="List"
style={{
flex: 1,
Expand Down
Loading