diff --git a/web/modals/threads/subchannels/subchannels-modal.react.js b/web/modals/threads/subchannels/subchannels-modal.react.js --- a/web/modals/threads/subchannels/subchannels-modal.react.js +++ b/web/modals/threads/subchannels/subchannels-modal.react.js @@ -2,10 +2,19 @@ import * as React from 'react'; +import { + fetchSingleMostRecentMessagesFromThreads, + fetchSingleMostRecentMessagesFromThreadsActionTypes, +} from 'lib/actions/message-actions'; import { useFilteredChatListData } from 'lib/selectors/chat-selectors'; import { threadSearchIndex } from 'lib/selectors/nav-selectors'; import { childThreadInfos } from 'lib/selectors/thread-selectors'; import { threadIsChannel } from 'lib/shared/thread-utils'; +import threadWatcher from 'lib/shared/thread-watcher'; +import { + useServerCall, + useDispatchActionPromise, +} from 'lib/utils/action-utils'; import Search from '../../../components/search.react'; import { useSelector } from '../../../redux/redux-utils'; @@ -50,6 +59,36 @@ ); }, [allSubchannelsChatList, searchIndex, searchText]); + const threadIDsWithNoMessages = React.useMemo( + () => + filteredSubchannelsChatList + .filter(item => !item.mostRecentMessageInfo) + .map(item => item.threadInfo.id), + [filteredSubchannelsChatList], + ); + const dispatchActionPromise = useDispatchActionPromise(); + const fetchSingleMostRecentMessages = useServerCall( + fetchSingleMostRecentMessagesFromThreads, + ); + + React.useEffect(() => { + if (!threadIDsWithNoMessages.length) { + return; + } + threadIDsWithNoMessages.forEach(tID => threadWatcher.watchID(tID)); + dispatchActionPromise( + fetchSingleMostRecentMessagesFromThreadsActionTypes, + fetchSingleMostRecentMessages(threadIDsWithNoMessages), + ); + return () => { + threadIDsWithNoMessages.forEach(tID => threadWatcher.removeID(tID)); + }; + }, [ + threadIDsWithNoMessages, + fetchSingleMostRecentMessages, + dispatchActionPromise, + ]); + const subchannels = React.useMemo(() => { if (!filteredSubchannelsChatList.length) { return (