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 { @@ -1758,6 +1759,45 @@ ]); } +function useChatMentionSearchIndex(): { + +[id: string]: SentencePrefixSearchIndex, +} { + const [chatMentionCandidates, resolvedThreadInfos] = + useChatMentionCandidatesObjAndResolvedThreadInfos(); + return React.useMemo(() => { + const result = {}; + const visitedThreads = new Set(); + for (const threadID in chatMentionCandidates) { + if (visitedThreads.has(threadID)) { + continue; + } + visitedThreads.add(threadID); + const searchIndex = new SentencePrefixSearchIndex(); + result[threadID] = searchIndex; + searchIndex.addEntry( + resolvedThreadInfos[threadID].id, + resolvedThreadInfos[threadID].uiName, + ); + for (const key in chatMentionCandidates[threadID]) { + result[key] = searchIndex; + searchIndex.addEntry( + chatMentionCandidates[threadID][key].id, + chatMentionCandidates[threadID][key].uiName, + ); + visitedThreads.add(chatMentionCandidates[threadID][key].id); + } + } + return result; + }, [chatMentionCandidates, resolvedThreadInfos]); +} + +function useThreadChatMentionSearchIndex( + threadInfo: ThreadInfo, +): SentencePrefixSearchIndex { + const chatMentionCandidatesSearchIndex = useChatMentionSearchIndex(); + return chatMentionCandidatesSearchIndex[threadInfo.id]; +} + export { threadHasPermission, viewerIsMember, @@ -1829,4 +1869,5 @@ getThreadsToDeleteText, useChatMentionCandidatesObj, useThreadChatMentionCandidates, + useThreadChatMentionSearchIndex, };