import { PropsWithChildren, useCallback, useEffect, useRef } from 'react' import { ScrollArea } from '@janhq/joi' const ListContainer = ({ children }: PropsWithChildren) => { const listRef = useRef(null) const prevScrollTop = useRef(0) const isUserManuallyScrollingUp = useRef(false) const handleScroll = useCallback((event: React.UIEvent) => { const currentScrollTop = event.currentTarget.scrollTop if (prevScrollTop.current > currentScrollTop) { isUserManuallyScrollingUp.current = true } else { const currentScrollTop = event.currentTarget.scrollTop const scrollHeight = event.currentTarget.scrollHeight const clientHeight = event.currentTarget.clientHeight if (currentScrollTop + clientHeight >= scrollHeight) { isUserManuallyScrollingUp.current = false } } if (isUserManuallyScrollingUp.current === true) { event.preventDefault() event.stopPropagation() } prevScrollTop.current = currentScrollTop }, []) useEffect(() => { if (isUserManuallyScrollingUp.current === true || !listRef.current) return const scrollHeight = listRef.current?.scrollHeight ?? 0 listRef.current?.scrollTo({ top: scrollHeight, behavior: 'instant', }) }, [listRef.current?.scrollHeight, isUserManuallyScrollingUp]) return ( {children} ) } export default ListContainer