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 @@ -38,6 +38,7 @@ import { threadInfoSelector, pendingToRealizedThreadIDsSelector, + threadInfosSelectorForThreadType, } from '../selectors/thread-selectors.js'; import { getRelativeMemberInfos, @@ -80,6 +81,7 @@ type ResolvedThreadInfo, type ChatMentionCandidatesObj, type ChatMentionCandidates, + type UserProfileThreadInfo, } from '../types/thread-types.js'; import { updateTypes } from '../types/update-types-enum.js'; import { type ClientUpdateInfo } from '../types/update-types.js'; @@ -1789,6 +1791,74 @@ ]); } +function useUserProfileThreadInfo(userInfo: ?UserInfo): ?UserProfileThreadInfo { + const userID = userInfo?.id; + const username = userInfo?.username; + + const loggedInUserInfo = useLoggedInUserInfo(); + const isViewerProfile = loggedInUserInfo?.id === userID; + + const privateThreadInfosSelector = threadInfosSelectorForThreadType( + threadTypes.PRIVATE, + ); + const privateThreadInfos = useSelector(privateThreadInfosSelector); + + const personalThreadInfosSelector = threadInfosSelectorForThreadType( + threadTypes.PERSONAL, + ); + + const personalThreadInfos = useSelector(personalThreadInfosSelector); + + const usersWithPersonalThread = useSelector(usersWithPersonalThreadSelector); + + return React.useMemo(() => { + if (!loggedInUserInfo || !userID || !username) { + return null; + } + + if (isViewerProfile) { + const privateThreadInfo: ?ThreadInfo = privateThreadInfos[0]; + if (!privateThreadInfo) { + return null; + } + + return { + threadInfo: privateThreadInfo, + }; + } + + if (usersWithPersonalThread.has(userID)) { + const personalThreadInfo: ?ThreadInfo = personalThreadInfos.find( + threadInfo => + userID === getSingleOtherUser(threadInfo, loggedInUserInfo.id), + ); + if (!personalThreadInfo) { + return null; + } + + return { + threadInfo: personalThreadInfo, + }; + } + + const pendingPersonalThreadInfo = createPendingPersonalThread( + loggedInUserInfo, + userID, + username, + ); + + return pendingPersonalThreadInfo; + }, [ + isViewerProfile, + loggedInUserInfo, + personalThreadInfos, + privateThreadInfos, + userID, + username, + usersWithPersonalThread, + ]); +} + export { threadHasPermission, viewerIsMember, @@ -1858,4 +1928,5 @@ getThreadsToDeleteText, useChatMentionCandidatesObj, useThreadChatMentionCandidates, + useUserProfileThreadInfo, }; diff --git a/lib/types/thread-types.js b/lib/types/thread-types.js --- a/lib/types/thread-types.js +++ b/lib/types/thread-types.js @@ -464,3 +464,8 @@ export type ChatMentionCandidatesObj = { +[id: string]: ChatMentionCandidates, }; + +export type UserProfileThreadInfo = { + +threadInfo: ThreadInfo, + +pendingPersonalThreadUserInfo?: UserInfo, +};