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

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

Merged
merged 1 commit into from
Apr 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions web/containers/ListContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions web/screens/Chat/ChatBody/index.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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 &&
Expand All @@ -43,7 +43,7 @@ const ChatBody: React.FC = () => {
</div>
))}
{loadModelError && <LoadModelError />}
</ScrollToBottom>
</ListContainer>
)
}

Expand Down