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 @@ -45,6 +45,7 @@ usersWithPersonalThreadSelector, } from '../selectors/user-selectors.js'; import { getUserAvatarForThread } from '../shared/avatar-utils.js'; +import SentencePrefixSearchIndex from '../shared/sentence-prefix-search-index.js'; import type { CalendarQuery } from '../types/entry-types.js'; import { messageTypes } from '../types/message-types-enum.js'; import { @@ -1855,6 +1856,50 @@ ]); } +function useChatMentionSearchIndex(): { + +[id: string]: SentencePrefixSearchIndex, +} { + const { chatMentionCandidatesObj } = useChatMentionCandidatesObjAndUtils(); + return React.useMemo(() => { + const result = {}; + for (const communityThreadID in chatMentionCandidatesObj) { + const searchIndex = new SentencePrefixSearchIndex(); + const searchIndexEntries = []; + for (const threadID in chatMentionCandidatesObj[communityThreadID]) { + searchIndexEntries.push({ + id: threadID, + uiName: chatMentionCandidatesObj[communityThreadID][threadID].uiName, + }); + } + // Sort the keys so that the order of the search result is consistent + searchIndexEntries.sort(({ uiName: uiNameA }, { uiName: uiNameB }) => + uiNameA.localeCompare(uiNameB), + ); + for (const { id, uiName } of searchIndexEntries) { + searchIndex.addEntry(id, uiName); + } + result[communityThreadID] = searchIndex; + } + return result; + }, [chatMentionCandidatesObj]); +} + +function useThreadChatMentionSearchIndex( + threadInfo: ThreadInfo, +): SentencePrefixSearchIndex { + const chatMentionCandidatesSearchIndex = useChatMentionSearchIndex(); + const { communityThreadIDForGenesisThreads } = + useChatMentionCandidatesObjAndUtils(); + if (threadInfo.community === genesis.id) { + return chatMentionCandidatesSearchIndex[ + communityThreadIDForGenesisThreads[threadInfo.id] + ]; + } + return chatMentionCandidatesSearchIndex[ + threadInfo.community ?? threadInfo.id + ]; +} + export { threadHasPermission, viewerIsMember, @@ -1925,4 +1970,5 @@ useChatMentionCandidatesObj, useThreadChatMentionCandidates, useUserProfileThreadInfo, + useThreadChatMentionSearchIndex, };