diff --git a/ee/tabby-ui/components/chat/chat-scroll-anchor.tsx b/ee/tabby-ui/components/chat/chat-scroll-anchor.tsx index 9c5cd882ff0c..5176a3e7b59e 100644 --- a/ee/tabby-ui/components/chat/chat-scroll-anchor.tsx +++ b/ee/tabby-ui/components/chat/chat-scroll-anchor.tsx @@ -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(() => { diff --git a/ee/tabby-ui/components/chat/chat.tsx b/ee/tabby-ui/components/chat/chat.tsx index f3c8547276e1..a762504fbba5 100644 --- a/ee/tabby-ui/components/chat/chat.tsx +++ b/ee/tabby-ui/components/chat/chat.tsx @@ -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 @@ -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]) diff --git a/ee/tabby-ui/lib/hooks/use-at-bottom.tsx b/ee/tabby-ui/lib/hooks/use-at-bottom.tsx index f0ed62e4282a..5e14e0f0d8d3 100644 --- a/ee/tabby-ui/lib/hooks/use-at-bottom.tsx +++ b/ee/tabby-ui/lib/hooks/use-at-bottom.tsx @@ -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 }