diff --git a/web/chat/chat-thread-list.react.js b/web/chat/chat-thread-list.react.js --- a/web/chat/chat-thread-list.react.js +++ b/web/chat/chat-thread-list.react.js @@ -6,6 +6,7 @@ import AutoSizer from 'react-virtualized-auto-sizer'; import { VariableSizeList } from 'react-window'; +import { useFetchLatestMessages } from 'lib/hooks/message-hooks.js'; import { emptyItemText } from 'lib/shared/thread-utils.js'; import ChatThreadListItem from './chat-thread-list-item.react.js'; @@ -17,6 +18,8 @@ import { useSelector } from '../redux/redux-utils.js'; import { useOnClickNewThread } from '../selectors/thread-selectors.js'; +const prefetchMessagesThreadThreshold = 5; + const sizes = { search: 68, thread: 81, @@ -66,6 +69,10 @@ [searchText, setSearchText], ); + const { fetchMoreLatestMessages, loadingStatus } = useFetchLatestMessages( + activeTab === 'Focus', + ); + const threadListContainerRef = React.useRef(); const threads = React.useMemo( @@ -79,6 +86,29 @@ [communityID, threadList], ); + const [currentHeight, setCurrentHeight] = React.useState(0); + React.useEffect(() => { + if (loadingStatus === 'loading') { + return; + } + + let totalHeight = sizes.search; + for (const threadItem of threads) { + totalHeight += getThreadItemSize(threadItem); + if (totalHeight > currentHeight) { + return; + } + } + + fetchMoreLatestMessages(); + }, [ + currentHeight, + fetchMoreLatestMessages, + loadingStatus, + threads, + threads.length, + ]); + React.useEffect(() => { if (threadListContainerRef.current) { threadListContainerRef.current.resetAfterIndex(0, false); @@ -108,8 +138,20 @@ return getThreadItemSize(items[index]); }; + const onItemsRendered = ({ visibleStopIndex }) => { + if ( + visibleStopIndex >= threads.length - prefetchMessagesThreadThreshold && + loadingStatus !== 'loading' + ) { + fetchMoreLatestMessages(); + } + }; + return ( - + setCurrentHeight(height)} + > {({ height }) => ( {renderItem} @@ -125,7 +168,7 @@ )} ); - }, [isBackground, search, threads]); + }, [fetchMoreLatestMessages, isBackground, loadingStatus, search, threads]); return ( <>