Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfung committed May 23, 2024
1 parent bf681ae commit f27891f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions ee/tabby-ui/components/chat/chat-scroll-anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ interface ChatScrollAnchorProps {

export function ChatScrollAnchor({ trackVisibility }: ChatScrollAnchorProps) {
const { container } = React.useContext(ChatContext)
const isAtBottom = useAtBottom(0, container)
const isAtBottom = useAtBottom(150, container)

const { ref, entry, inView } = useInView({
trackVisibility,
delay: 100,
rootMargin: '0px 0px -150px 0px'
rootMargin: '0px 0px -150px 0px',
root: container
})

React.useEffect(() => {
Expand Down
27 changes: 16 additions & 11 deletions ee/tabby-ui/components/chat/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { EmptyScreen } from './empty-screen'
import { QuestionAnswerList } from './question-answer'
import { useTabbyAnswer } from './use-tabby-answer'
import { useLatest } from '@/lib/hooks/use-latest'
import { useDebounceCallback } from '@/lib/hooks/use-debounce'

type ChatContextValue = {
isLoading: boolean
Expand Down Expand Up @@ -228,20 +229,24 @@ function ChatRenderer(
})
}, [answer, isLoading])

const scrollToBottom = useDebounceCallback(() => {
if (container) {
container.scrollTo({
top: container.scrollHeight,
behavior: 'smooth'
})
} else {
window.scrollTo({
top: document.body.offsetHeight,
behavior: 'smooth'
})
}
}, 100)

React.useLayoutEffect(() => {
// scroll to bottom when a request is sent
if (isLoading) {
if (container) {
container.scrollTo({
top: container.scrollHeight,
behavior: 'smooth'
})
} else {
window.scrollTo({
top: document.body.offsetHeight,
behavior: 'smooth'
})
}
scrollToBottom.run()
}
}, [isLoading])

Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-ui/lib/hooks/use-at-bottom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export function useAtBottom(offset = 0, container?: HTMLDivElement) {
const handleScroll = throttle(
() => {
const { scrollTop, clientHeight, scrollHeight } = container
setIsAtBottom(scrollTop + clientHeight >= scrollHeight)
setIsAtBottom(scrollTop + clientHeight >= scrollHeight - offset)
},
100,
{ leading: true }
Expand Down

0 comments on commit f27891f

Please sign in to comment.