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 { @@ -1795,6 +1796,50 @@ ]); } +function useChatMentionSearchIndex(): { + +[id: string]: SentencePrefixSearchIndex, +} { + const { chatMentionCandidatesObj } = useChatMentionCandidatesObjAndUtils(); + return React.useMemo(() => { + const result = {}; + for (const communityThreadID in chatMentionCandidatesObj) { + const searchIndex = new SentencePrefixSearchIndex(); + result[communityThreadID] = searchIndex; + // Sort the keys so that the order of the search results is consistent + const threadIDs = Object.keys( + chatMentionCandidatesObj[communityThreadID], + ).sort((a, b) => { + const aName = chatMentionCandidatesObj[communityThreadID][a].uiName; + const bName = chatMentionCandidatesObj[communityThreadID][b].uiName; + return aName.localeCompare(bName); + }); + for (const key of threadIDs) { + searchIndex.addEntry( + chatMentionCandidatesObj[communityThreadID][key].id, + chatMentionCandidatesObj[communityThreadID][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, @@ -1864,4 +1909,5 @@ getThreadsToDeleteText, useChatMentionCandidatesObj, useThreadChatMentionCandidates, + useThreadChatMentionSearchIndex, };