File tree Expand file tree Collapse file tree 1 file changed +16
-1
lines changed
Expand file tree Collapse file tree 1 file changed +16
-1
lines changed Original file line number Diff line number Diff line change @@ -960,9 +960,24 @@ function _Chat() {
960960 ( scrollRef . current . scrollTop + scrollRef . current . clientHeight ) ,
961961 ) <= 1
962962 : false ;
963+ const isAttachWithTop = useMemo ( ( ) => {
964+ const lastMessage = scrollRef . current ?. lastElementChild as HTMLElement ;
965+ // if scrolllRef is not ready or no message, return false
966+ if ( ! scrollRef ?. current || ! lastMessage ) return false ;
967+ const topDistance =
968+ lastMessage ! . getBoundingClientRect ( ) . top -
969+ scrollRef . current . getBoundingClientRect ( ) . top ;
970+ // leave some space for user question
971+ return topDistance < 100 ;
972+ } , [ scrollRef ?. current ?. scrollHeight ] ) ;
973+
974+ const isTyping = userInput !== "" ;
975+
976+ // if user is typing, should auto scroll to bottom
977+ // if user is not typing, should auto scroll to bottom only if already at bottom
963978 const { setAutoScroll, scrollDomToBottom } = useScrollToBottom (
964979 scrollRef ,
965- isScrolledToBottom ,
980+ ( isScrolledToBottom || isAttachWithTop ) && ! isTyping ,
966981 ) ;
967982 const [ hitBottom , setHitBottom ] = useState ( true ) ;
968983 const isMobileScreen = useMobileScreen ( ) ;
You can’t perform that action at this time.
0 commit comments