diff --git a/lib/shared/thread-utils.js b/lib/shared/thread-utils.js --- a/lib/shared/thread-utils.js +++ b/lib/shared/thread-utils.js @@ -1676,6 +1676,14 @@ } within this ${communityOrThreadNoun(threadInfo)}`; } +type OldestCreatedInput = { +creationTime: number, ... }; +function getOldestCreated(arr: $ReadOnlyArray): ?T { + return arr.reduce( + (a, b) => (!b || (a && a.creationTime < b.creationTime) ? a : b), + null, + ); +} + function useUserProfileThreadInfo(userInfo: ?UserInfo): ?UserProfileThreadInfo { const userID = userInfo?.id; const username = userInfo?.username; @@ -1683,18 +1691,37 @@ const loggedInUserInfo = useLoggedInUserInfo(); const isViewerProfile = loggedInUserInfo?.id === userID; - const privateThreadInfosSelector = threadInfosSelectorForThreadType( + const genesisPrivateThreadInfosSelector = threadInfosSelectorForThreadType( threadTypes.GENESIS_PRIVATE, ); + const genesisPrivateThreadInfos = useSelector( + genesisPrivateThreadInfosSelector, + ); + const privateThreadInfosSelector = threadInfosSelectorForThreadType( + threadTypes.PRIVATE, + ); const privateThreadInfos = useSelector(privateThreadInfosSelector); + const oldestPrivateThreadInfo = React.useMemo( + () => + getOldestCreated([...privateThreadInfos, ...genesisPrivateThreadInfos]), + [privateThreadInfos, genesisPrivateThreadInfos], + ); - const personalThreadInfosSelector = threadInfosSelectorForThreadType( + const usersWithPersonalThread = useSelector(usersWithPersonalThreadSelector); + const genesisPersonalThreadInfosSelector = threadInfosSelectorForThreadType( threadTypes.GENESIS_PERSONAL, ); - + const genesisPersonalThreadInfos = useSelector( + genesisPersonalThreadInfosSelector, + ); + const personalThreadInfosSelector = threadInfosSelectorForThreadType( + threadTypes.PERSONAL, + ); const personalThreadInfos = useSelector(personalThreadInfosSelector); - - const usersWithPersonalThread = useSelector(usersWithPersonalThreadSelector); + const allPersonalThreadInfos = React.useMemo( + () => [...personalThreadInfos, ...genesisPersonalThreadInfos], + [personalThreadInfos, genesisPersonalThreadInfos], + ); const [supportThickThreads, setSupportThickThreads] = React.useState(false); const usersSupportThickThreads = useUsersSupportThickThreads(); @@ -1716,17 +1743,18 @@ } if (isViewerProfile) { - const privateThreadInfo: ?ThreadInfo = privateThreadInfos[0]; - - return privateThreadInfo ? { threadInfo: privateThreadInfo } : null; + return oldestPrivateThreadInfo + ? { threadInfo: oldestPrivateThreadInfo } + : null; } if (usersWithPersonalThread.has(userID)) { - const personalThreadInfo: ?ThreadInfo = personalThreadInfos.find( - threadInfo => - userID === getSingleOtherUser(threadInfo, loggedInUserInfo.id), + const personalThreadInfo: ?ThreadInfo = getOldestCreated( + allPersonalThreadInfos.filter( + threadInfo => + userID === getSingleOtherUser(threadInfo, loggedInUserInfo.id), + ), ); - return personalThreadInfo ? { threadInfo: personalThreadInfo } : null; } @@ -1741,8 +1769,8 @@ }, [ isViewerProfile, loggedInUserInfo, - personalThreadInfos, - privateThreadInfos, + allPersonalThreadInfos, + oldestPrivateThreadInfo, supportThickThreads, userID, username,