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 @@ -44,6 +44,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 { @@ -1773,6 +1774,42 @@ ]); } +function useChatMentionSearchIndex(): { + +[id: string]: SentencePrefixSearchIndex, +} { + const { chatMentionCandidatesObj } = useChatMentionCandidatesObjAndUtils(); + return React.useMemo(() => { + const result = {}; + for (const threadID in chatMentionCandidatesObj) { + const searchIndex = new SentencePrefixSearchIndex(); + result[threadID] = searchIndex; + for (const key in chatMentionCandidatesObj[threadID]) { + searchIndex.addEntry( + chatMentionCandidatesObj[threadID][key].id, + chatMentionCandidatesObj[threadID][key].uiName, + ); + } + } + 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, @@ -1844,4 +1881,5 @@ getThreadsToDeleteText, useChatMentionCandidatesObj, useThreadChatMentionCandidates, + useThreadChatMentionSearchIndex, };