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 @@ -77,6 +77,9 @@ type ClientNewThreadRequest, type NewThreadResult, type ChangeThreadSettingsPayload, + type ResolvedThreadInfo, + type ChatMentionCandidatesObj, + type ChatMentionCandidates, } from '../types/thread-types.js'; import { updateTypes } from '../types/update-types-enum.js'; import { type ClientUpdateInfo } from '../types/update-types.js'; @@ -92,6 +95,7 @@ } from '../utils/action-utils.js'; import type { DispatchActionPromise } from '../utils/action-utils.js'; import type { GetENSNames } from '../utils/ens-helpers.js'; +import { useResolvedThreadInfosObj } from '../utils/entity-helpers.js'; import { ET, entityTextToRawString, @@ -1621,6 +1625,96 @@ } within this ${communityOrThreadNoun(threadInfo)}`; } +function getChatMentionCandidates(threadInfos: { + +[id: string]: ResolvedThreadInfo, +}): ChatMentionCandidatesObj { + const result = {}; + const visitedGenesisCommunityThreads = new Set(); + for (const currentThreadID in threadInfos) { + const currentThreadInfo = threadInfos[currentThreadID]; + const { community: currentThreadCommunity } = currentThreadInfo; + if (!currentThreadCommunity) { + result[currentThreadID] = [currentThreadInfo]; + continue; + } + // Handle GENESIS community case: mentioning inside GENESIS should only + // show chats and threads inside the top level that is below GENESIS. + if (threadInfos[currentThreadCommunity].type === threadTypes.GENESIS) { + if (visitedGenesisCommunityThreads.has(currentThreadID)) { + continue; + } + const threadTraversePath = [currentThreadInfo]; + visitedGenesisCommunityThreads.add(currentThreadID); + let currentlySelectedThreadID = currentThreadInfo.parentThreadID; + while (currentlySelectedThreadID) { + const currentlySelectedThreadInfo = + threadInfos[currentlySelectedThreadID]; + if ( + visitedGenesisCommunityThreads.has(currentlySelectedThreadID) || + currentlySelectedThreadInfo.type === threadTypes.GENESIS + ) { + break; + } + threadTraversePath.push(currentlySelectedThreadInfo); + visitedGenesisCommunityThreads.add(currentlySelectedThreadID); + currentlySelectedThreadID = currentlySelectedThreadInfo.parentThreadID; + } + const lastThreadInTraversePath = + threadTraversePath[threadTraversePath.length - 1]; + const lastThreadInTraversePathID = + lastThreadInTraversePath.parentThreadID ?? lastThreadInTraversePath.id; + if ( + threadInfos[lastThreadInTraversePathID].type === threadTypes.GENESIS + ) { + const threadObjectFromTraversePath = {}; + for (const threadInfo of threadTraversePath) { + threadObjectFromTraversePath[threadInfo.id] = threadInfo; + result[threadInfo.id] = threadObjectFromTraversePath; + } + } else { + for (const threadInfo of threadTraversePath) { + result[lastThreadInTraversePathID][threadInfo.id] = threadInfo; + result[threadInfo.id] = result[lastThreadInTraversePathID]; + } + } + continue; + } + result[currentThreadCommunity][currentThreadID] = currentThreadInfo; + result[currentThreadID] = result[currentThreadCommunity]; + } + return result; +} + +function useChatMentionCandidatesObj(): ChatMentionCandidatesObj { + const threadInfos = useSelector(threadInfoSelector); + const resolvedThreadInfos = useResolvedThreadInfosObj(threadInfos); + return React.useMemo( + () => getChatMentionCandidates(resolvedThreadInfos), + [resolvedThreadInfos], + ); +} + +function threadChatMentionCandidates( + threadInfo: ThreadInfo, + chatMentionCandidates: ChatMentionCandidatesObj, +): ChatMentionCandidates { + return ( + chatMentionCandidates[threadInfo.id] ?? + (threadInfo.containingThreadID && + chatMentionCandidates[threadInfo.containingThreadID]) + ); +} + +function useThreadChatMentionCandidates( + threadInfo: ThreadInfo, +): ChatMentionCandidates { + const chatMentionCandidates = useChatMentionCandidatesObj(); + return React.useMemo( + () => threadChatMentionCandidates(threadInfo, chatMentionCandidates), + [chatMentionCandidates, threadInfo], + ); +} + export { threadHasPermission, viewerIsMember, @@ -1689,4 +1783,7 @@ useRoleMemberCountsForCommunity, useRoleUserSurfacedPermissions, getThreadsToDeleteText, + useChatMentionCandidatesObj, + threadChatMentionCandidates, + useThreadChatMentionCandidates, }; 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 @@ -456,3 +456,8 @@ export const maxUnreadSidebars = 5; export type ThreadStoreThreadInfos = { +[id: string]: RawThreadInfo }; + +export type ChatMentionCandidates = { +[id: string]: ResolvedThreadInfo }; +export type ChatMentionCandidatesObj = { + +[id: string]: ChatMentionCandidates, +};