mirror of
				https://github.com/Yidadaa/ChatGPT-Next-Web.git
				synced 2025-10-31 05:39:20 +08:00 
			
		
		
		
	Merge pull request #4149 from fred-bf/feat/auto-detach-scrolling
feat: auto detach scrolling
This commit is contained in:
		| @@ -6,6 +6,7 @@ import React, { | |||||||
|   useMemo, |   useMemo, | ||||||
|   useCallback, |   useCallback, | ||||||
|   Fragment, |   Fragment, | ||||||
|  |   RefObject, | ||||||
| } from "react"; | } from "react"; | ||||||
|  |  | ||||||
| import SendWhiteIcon from "../icons/send-white.svg"; | import SendWhiteIcon from "../icons/send-white.svg"; | ||||||
| @@ -382,11 +383,13 @@ function ChatAction(props: { | |||||||
|   ); |   ); | ||||||
| } | } | ||||||
|  |  | ||||||
| function useScrollToBottom() { | function useScrollToBottom( | ||||||
|  |   scrollRef: RefObject<HTMLDivElement>, | ||||||
|  |   detach: boolean = false, | ||||||
|  | ) { | ||||||
|   // for auto-scroll |   // for auto-scroll | ||||||
|   const scrollRef = useRef<HTMLDivElement>(null); |  | ||||||
|   const [autoScroll, setAutoScroll] = useState(true); |  | ||||||
|  |  | ||||||
|  |   const [autoScroll, setAutoScroll] = useState(true); | ||||||
|   function scrollDomToBottom() { |   function scrollDomToBottom() { | ||||||
|     const dom = scrollRef.current; |     const dom = scrollRef.current; | ||||||
|     if (dom) { |     if (dom) { | ||||||
| @@ -399,7 +402,7 @@ function useScrollToBottom() { | |||||||
|  |  | ||||||
|   // auto scroll |   // auto scroll | ||||||
|   useEffect(() => { |   useEffect(() => { | ||||||
|     if (autoScroll) { |     if (autoScroll && !detach) { | ||||||
|       scrollDomToBottom(); |       scrollDomToBottom(); | ||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
| @@ -658,7 +661,17 @@ function _Chat() { | |||||||
|   const [userInput, setUserInput] = useState(""); |   const [userInput, setUserInput] = useState(""); | ||||||
|   const [isLoading, setIsLoading] = useState(false); |   const [isLoading, setIsLoading] = useState(false); | ||||||
|   const { submitKey, shouldSubmit } = useSubmitHandler(); |   const { submitKey, shouldSubmit } = useSubmitHandler(); | ||||||
|   const { scrollRef, setAutoScroll, scrollDomToBottom } = useScrollToBottom(); |   const scrollRef = useRef<HTMLDivElement>(null); | ||||||
|  |   const isScrolledToBottom = scrollRef?.current | ||||||
|  |     ? Math.abs( | ||||||
|  |         scrollRef.current.scrollHeight - | ||||||
|  |           (scrollRef.current.scrollTop + scrollRef.current.clientHeight), | ||||||
|  |       ) <= 1 | ||||||
|  |     : false; | ||||||
|  |   const { setAutoScroll, scrollDomToBottom } = useScrollToBottom( | ||||||
|  |     scrollRef, | ||||||
|  |     isScrolledToBottom, | ||||||
|  |   ); | ||||||
|   const [hitBottom, setHitBottom] = useState(true); |   const [hitBottom, setHitBottom] = useState(true); | ||||||
|   const isMobileScreen = useMobileScreen(); |   const isMobileScreen = useMobileScreen(); | ||||||
|   const navigate = useNavigate(); |   const navigate = useNavigate(); | ||||||
| @@ -1003,7 +1016,6 @@ function _Chat() { | |||||||
|     setHitBottom(isHitBottom); |     setHitBottom(isHitBottom); | ||||||
|     setAutoScroll(isHitBottom); |     setAutoScroll(isHitBottom); | ||||||
|   }; |   }; | ||||||
|  |  | ||||||
|   function scrollToBottom() { |   function scrollToBottom() { | ||||||
|     setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE); |     setMsgRenderIndex(renderMessages.length - CHAT_PAGE_SIZE); | ||||||
|     scrollDomToBottom(); |     scrollDomToBottom(); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user