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: scroll bottom when generation text #4323

Merged
merged 5 commits into from
Dec 23, 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
31 changes: 20 additions & 11 deletions web/screens/Thread/ThreadCenterPanel/ChatBody/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,30 @@
} from '@/helpers/atoms/Thread.atom'

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

24-25 lines are not covered with tests

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

27-28 lines are not covered with tests

const isMessagesIdentificial = (

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

View workflow job for this annotation

GitHub Actions / coverage-check

30 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 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
}

useEffect(() => {
if (

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

View workflow job for this annotation

GitHub Actions / coverage-check

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

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

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
}
}, [messages, current, loadModelError, currentThread])

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

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

View workflow job for this annotation

GitHub Actions / coverage-check

47 line is not covered with tests
return (
<div className="flex h-full w-full flex-col">
<ChatBody loadModelError={loadModelError} messages={current} />
Expand All @@ -61,15 +61,15 @@
loadModelError?: string
}) => {
// The scrollable element for your list
const parentRef = useRef<HTMLDivElement>(null)
const prevScrollTop = useRef(0)
const isUserManuallyScrollingUp = useRef(false)
const currentThread = useAtomValue(activeThreadAtom)
const threadStates = useAtomValue(threadStatesAtom)
const isGeneratingResponse = useAtomValue(isGeneratingResponseAtom)

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

View workflow job for this annotation

GitHub Actions / coverage-check

64-69 lines are not covered with tests

const isStreamingResponse = Object.values(threadStates).some(
(threadState) => threadState.waitingForResponse

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

View workflow job for this annotation

GitHub Actions / coverage-check

71-72 lines are not covered with tests
)

const count = useMemo(
Expand All @@ -85,24 +85,33 @@
overscan: 5,
})

useEffect(() => {
if (parentRef.current) {
parentRef.current.scrollTo({ top: parentRef.current.scrollHeight })
virtualizer.scrollToIndex(count - 1)
}
}, [count, virtualizer])

useEffect(() => {
if (parentRef.current && isGeneratingResponse) {
requestAnimationFrame(() => {
if (parentRef.current) {
parentRef.current.scrollTo({ top: parentRef.current.scrollHeight })
}
})
parentRef.current.scrollTo({ top: parentRef.current.scrollHeight })
virtualizer.scrollToIndex(count - 1)
}
}, [count, virtualizer, isGeneratingResponse])

useEffect(() => {
if (parentRef.current && isGeneratingResponse) {
parentRef.current.scrollTo({ top: parentRef.current.scrollHeight })
virtualizer.scrollToIndex(count - 1)
}
}, [count, virtualizer, isGeneratingResponse, currentThread?.id])

useEffect(() => {
isUserManuallyScrollingUp.current = false
requestAnimationFrame(() => {
if (parentRef.current) {
parentRef.current.scrollTo({ top: parentRef.current.scrollHeight })
virtualizer.scrollToIndex(count - 1)
}
})
if (parentRef.current) {
parentRef.current.scrollTo({ top: parentRef.current.scrollHeight })
virtualizer.scrollToIndex(count - 1)
}
}, [count, currentThread?.id, virtualizer])

const items = virtualizer.getVirtualItems()
Expand Down
Loading