diff --git a/lib/hooks/message-hooks.js b/lib/hooks/message-hooks.js --- a/lib/hooks/message-hooks.js +++ b/lib/hooks/message-hooks.js @@ -4,11 +4,15 @@ import { useGetLatestMessageEdit } from './latest-message-edit.js'; import { messageInfoSelector } from '../selectors/chat-selectors.js'; -import { getOldestNonLocalMessageID } from '../shared/message-utils.js'; +import { + getOldestNonLocalMessageID, + useFetchMessages, +} from '../shared/message-utils.js'; import { messageSpecs } from '../shared/messages/message-specs.js'; import type { MessageInfo } from '../types/message-types.js'; import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; import { useSelector } from '../utils/redux-utils.js'; +import sleep from '../utils/sleep.js'; function useOldestMessageServerID(threadID: string): ?string { return useSelector(state => @@ -107,6 +111,24 @@ })(); }, [threadInfo, getMessageInfoForPreview]); + const shouldFetchOlderMessages = + !!messageInfoForPreview?.shouldFetchOlderMessages; + + const [canFetchOlderMessages, setCanFetchOlderMessages] = + React.useState(true); + const fetchMessages = useFetchMessages(threadInfo); + React.useEffect(() => { + if (!canFetchOlderMessages || !shouldFetchOlderMessages) { + return; + } + setCanFetchOlderMessages(false); + void (async () => { + await fetchMessages(); + await sleep(3000); + setCanFetchOlderMessages(true); + })(); + }, [canFetchOlderMessages, shouldFetchOlderMessages, fetchMessages]); + return messageInfoForPreview?.messageInfoForPreview; }