diff --git a/lib/components/chat-mention-provider.react.js b/lib/components/chat-mention-provider.react.js --- a/lib/components/chat-mention-provider.react.js +++ b/lib/components/chat-mention-provider.react.js @@ -5,6 +5,7 @@ import genesis from '../facts/genesis.js'; import { threadInfoSelector } from '../selectors/thread-selectors.js'; import SentencePrefixSearchIndex from '../shared/sentence-prefix-search-index.js'; +import { threadIsPending } from '../shared/thread-utils.js'; import type { ResolvedThreadInfo, ThreadInfo, @@ -25,7 +26,7 @@ +getChatMentionSearchIndex: ( threadInfo: ThreadInfo, ) => SentencePrefixSearchIndex, - +communityThreadIDForGenesisThreads: { +[id: string]: string }, + +getCommunityThreadIDForGenesisThreads: (threadInfo: ThreadInfo) => string, +chatMentionCandidatesObj: ChatMentionCandidatesObj, }; @@ -33,36 +34,38 @@ const ChatMentionContext: React.Context = React.createContext({ getChatMentionSearchIndex: () => emptySearchIndex, - communityThreadIDForGenesisThreads: {}, + getCommunityThreadIDForGenesisThreads: () => '', chatMentionCandidatesObj: {}, }); function ChatMentionContextProvider(props: Props): React.Node { const { children } = props; - const { communityThreadIDForGenesisThreads, chatMentionCandidatesObj } = + const { chatMentionCandidatesObj, getCommunityThreadIDForGenesisThreads } = useChatMentionCandidatesObjAndUtils(); const searchIndices = useChatMentionSearchIndex(chatMentionCandidatesObj); const getChatMentionSearchIndex = React.useCallback( (threadInfo: ThreadInfo) => { if (threadInfo.community === genesis.id) { - return searchIndices[communityThreadIDForGenesisThreads[threadInfo.id]]; + const communityThreadID = + getCommunityThreadIDForGenesisThreads(threadInfo); + return searchIndices[communityThreadID]; } return searchIndices[threadInfo.community ?? threadInfo.id]; }, - [communityThreadIDForGenesisThreads, searchIndices], + [getCommunityThreadIDForGenesisThreads, searchIndices], ); const value = React.useMemo( () => ({ getChatMentionSearchIndex, - communityThreadIDForGenesisThreads, + getCommunityThreadIDForGenesisThreads, chatMentionCandidatesObj, }), [ getChatMentionSearchIndex, - communityThreadIDForGenesisThreads, + getCommunityThreadIDForGenesisThreads, chatMentionCandidatesObj, ], ); @@ -214,10 +217,7 @@ function useChatMentionCandidatesObjAndUtils(): { chatMentionCandidatesObj: ChatMentionCandidatesObj, - resolvedThreadInfos: { - +[id: string]: ResolvedThreadInfo, - }, - communityThreadIDForGenesisThreads: { +[id: string]: string }, + getCommunityThreadIDForGenesisThreads: (threadInfo: ThreadInfo) => string, } { const threadInfos = useSelector(threadInfoSelector); const resolvedThreadInfos = useResolvedThreadInfosObj( @@ -229,10 +229,22 @@ () => getChatMentionCandidates(threadInfos, resolvedThreadInfos), [threadInfos, resolvedThreadInfos], ); + + const getCommunityThreadIDForGenesisThreads = React.useCallback( + (threadInfo: ThreadInfo): string => { + const threadID = + threadIsPending(threadInfo.id) && threadInfo.containingThreadID + ? threadInfo.containingThreadID + : threadInfo.id; + + return communityThreadIDForGenesisThreads[threadID]; + }, + [communityThreadIDForGenesisThreads], + ); + return { chatMentionCandidatesObj, - resolvedThreadInfos, - communityThreadIDForGenesisThreads, + getCommunityThreadIDForGenesisThreads, }; } diff --git a/lib/hooks/chat-mention-hooks.js b/lib/hooks/chat-mention-hooks.js --- a/lib/hooks/chat-mention-hooks.js +++ b/lib/hooks/chat-mention-hooks.js @@ -21,13 +21,18 @@ function useThreadChatMentionCandidates( threadInfo: ThreadInfo, ): ChatMentionCandidates { - const { communityThreadIDForGenesisThreads, chatMentionCandidatesObj } = + const { getCommunityThreadIDForGenesisThreads, chatMentionCandidatesObj } = useChatMentionContext(); + + const communityThreadIDForGenesisThreads = + getCommunityThreadIDForGenesisThreads(threadInfo); + return React.useMemo(() => { const communityID = threadInfo.community === genesis.id - ? communityThreadIDForGenesisThreads[threadInfo.id] + ? communityThreadIDForGenesisThreads : threadInfo.community ?? threadInfo.id; + const allChatsWithinCommunity = chatMentionCandidatesObj[communityID]; if (!allChatsWithinCommunity) { return {};