mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
31 lines
1,013 B
TypeScript
31 lines
1,013 B
TypeScript
|
|
import { useState, useRef, useCallback, useEffect } from 'react';
|
||
|
|
import { useChatContext } from '~/Providers';
|
||
|
|
|
||
|
|
export default function useAutoScroll() {
|
||
|
|
const { isSubmitting } = useChatContext();
|
||
|
|
const [showScrollButton, setShowScrollButton] = useState(false);
|
||
|
|
const scrollableRef = useRef<HTMLDivElement | null>(null);
|
||
|
|
const contentEndRef = useRef<HTMLDivElement | null>(null);
|
||
|
|
|
||
|
|
const scrollToBottom = useCallback(() => {
|
||
|
|
if (scrollableRef.current) {
|
||
|
|
scrollableRef.current.scrollTop = scrollableRef.current.scrollHeight;
|
||
|
|
}
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
const handleScroll = useCallback(() => {
|
||
|
|
if (scrollableRef.current) {
|
||
|
|
const { scrollTop, scrollHeight, clientHeight } = scrollableRef.current;
|
||
|
|
setShowScrollButton(scrollHeight - scrollTop - clientHeight > 100);
|
||
|
|
}
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
if (isSubmitting) {
|
||
|
|
scrollToBottom();
|
||
|
|
}
|
||
|
|
}, [isSubmitting, scrollToBottom]);
|
||
|
|
|
||
|
|
return { scrollableRef, contentEndRef, handleScroll, scrollToBottom, showScrollButton };
|
||
|
|
}
|