refactor: good bye react-scroll-to-bottom (#2769)

Co-authored-by: James <james@jan.ai>
This commit is contained in:
NamH 2024-04-21 22:30:25 +07:00 committed by GitHub
parent b058dda8bb
commit da6c648e47
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 33 additions and 4 deletions

View File

@ -0,0 +1,29 @@
import { ReactNode, useEffect, useRef } from 'react'
type Props = {
children: ReactNode
}
const ListContainer: React.FC<Props> = ({ children }) => {
const listRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const scrollHeight = listRef.current?.scrollHeight ?? 0
listRef.current?.scrollTo({
top: scrollHeight,
behavior: 'smooth',
})
})
return (
<div
ref={listRef}
className="flex h-full w-full flex-col overflow-y-scroll"
>
{children}
</div>
)
}
export default ListContainer

View File

@ -1,8 +1,8 @@
import ScrollToBottom from 'react-scroll-to-bottom'
import { MessageStatus } from '@janhq/core'
import { useAtomValue } from 'jotai'
import ListContainer from '@/containers/ListContainer'
import { loadModelErrorAtom } from '@/hooks/useActiveModel'
import ChatItem from '../ChatItem'
@ -26,7 +26,7 @@ const ChatBody: React.FC = () => {
if (messages.length === 0) return <EmptyThread />
return (
<ScrollToBottom className="flex h-full w-full flex-col">
<ListContainer>
{messages.map((message, index) => (
<div key={message.id}>
{message.status !== MessageStatus.Error &&
@ -43,7 +43,7 @@ const ChatBody: React.FC = () => {
</div>
))}
{loadModelError && <LoadModelError />}
</ScrollToBottom>
</ListContainer>
)
}