1- import React , { useRef , useCallback , useEffect } from 'react' ;
1+ import React , { useRef , useCallback , useEffect , useMemo } from 'react' ;
22import { VStack , Box } from '@chakra-ui/react' ;
33import { useVirtualizer } from '@tanstack/react-virtual' ;
44import Message from './Message' ;
55
6- function ChatWindow ( { messages, onFeedback, scrollTrigger } ) {
6+ function ChatWindow ( { messages, onFeedback, onFollowUpClick , scrollTrigger } ) {
77 const scrollContainerRef = useRef ( null ) ;
88 const shouldAutoScrollRef = useRef ( true ) ;
9+ const isProgrammaticScrollRef = useRef ( false ) ;
910 const bottomThreshold = 80 ;
1011
12+ const lastAssistantMessageId = useMemo ( ( ) => {
13+ for ( let i = messages . length - 1 ; i >= 0 ; i -= 1 ) {
14+ const msg = messages [ i ] ;
15+ if ( msg ?. role === 'assistant' ) {
16+ return msg . id ;
17+ }
18+ }
19+ return null ;
20+ } , [ messages ] ) ;
21+
1122 const rowVirtualizer = useVirtualizer ( {
1223 count : messages . length ,
1324 getScrollElement : ( ) => scrollContainerRef . current ,
@@ -25,11 +36,18 @@ function ChatWindow({ messages, onFeedback, scrollTrigger }) {
2536 if ( ! el ) return ;
2637
2738 requestAnimationFrame ( ( ) => {
28- el . scrollTop = el . scrollHeight ;
39+ if ( ! scrollContainerRef . current ) return ;
40+ isProgrammaticScrollRef . current = true ;
41+ scrollContainerRef . current . scrollTop = scrollContainerRef . current . scrollHeight ;
42+ requestAnimationFrame ( ( ) => {
43+ isProgrammaticScrollRef . current = false ;
44+ } ) ;
2945 } ) ;
3046 } , [ ] ) ;
3147
3248 const handleScroll = useCallback ( ( ) => {
49+ if ( isProgrammaticScrollRef . current ) return ;
50+
3351 const el = scrollContainerRef . current ;
3452 if ( ! el ) return ;
3553
@@ -64,6 +82,7 @@ function ChatWindow({ messages, onFeedback, scrollTrigger }) {
6482 align = "stretch"
6583 spacing = { 0 }
6684 css = { {
85+ overflowAnchor : "none" ,
6786 "&::-webkit-scrollbar" : {
6887 width : "6px" ,
6988 } ,
@@ -95,9 +114,14 @@ function ChatWindow({ messages, onFeedback, scrollTrigger }) {
95114 transform = { `translateY(${ virtualRow . start } px)` }
96115 paddingBottom = "16px"
97116 >
98- < Message
99- message = { messages [ virtualRow . index ] }
100- onFeedback = { onFeedback }
117+ < Message
118+ message = { messages [ virtualRow . index ] }
119+ onFeedback = { onFeedback }
120+ showFollowUpQuestions = {
121+ messages [ virtualRow . index ] ?. id === lastAssistantMessageId &&
122+ ! messages [ virtualRow . index ] ?. isLoading
123+ }
124+ onFollowUpClick = { onFollowUpClick }
101125 />
102126 </ Box >
103127 ) ) }
0 commit comments