Spaces:
Configuration error
Configuration error
File size: 1,182 Bytes
aa15bce |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import { useCallback, useEffect, useRef } from 'react';
interface AutoScrollOptions {
items: ReadonlyArray<unknown>;
isWaiting: boolean;
}
export const useAutoScroll = ({ items, isWaiting }: AutoScrollOptions) => {
const scrollContainerRef = useRef<HTMLDivElement | null>(null);
const isUserNearBottomRef = useRef(true);
const handleScroll = useCallback(() => {
const container = scrollContainerRef.current;
if (!container) return;
const threshold = 80;
const distanceFromBottom = container.scrollHeight - container.scrollTop - container.clientHeight;
isUserNearBottomRef.current = distanceFromBottom <= threshold;
}, []);
const scrollToBottom = useCallback((behavior: ScrollBehavior = 'smooth') => {
const container = scrollContainerRef.current;
if (!container) return;
container.scrollTo({ top: container.scrollHeight, behavior });
}, []);
useEffect(() => {
if (isUserNearBottomRef.current) {
const behavior = items.length === 0 ? 'auto' : 'smooth';
scrollToBottom(behavior);
}
}, [items, isWaiting, scrollToBottom]);
return {
scrollContainerRef,
handleScroll,
scrollToBottom,
};
};
|