Changeset View
Changeset View
Standalone View
Standalone View
lib/components/chat-mention-provider.react.js
// @flow | // @flow | ||||
import * as React from 'react'; | import * as React from 'react'; | ||||
import genesis from '../facts/genesis.js'; | import genesis from '../facts/genesis.js'; | ||||
import { threadInfoSelector } from '../selectors/thread-selectors.js'; | import { threadInfoSelector } from '../selectors/thread-selectors.js'; | ||||
import SentencePrefixSearchIndex from '../shared/sentence-prefix-search-index.js'; | import SentencePrefixSearchIndex from '../shared/sentence-prefix-search-index.js'; | ||||
import { threadTypes } from '../types/thread-types-enum.js'; | import { threadTypes } from '../types/thread-types-enum.js'; | ||||
import type { | import type { | ||||
ChatMentionCandidatesObj, | ChatMentionCandidatesObj, | ||||
ChatMentionCandidate, | ChatMentionCandidate, | ||||
ResolvedThreadInfo, | ResolvedThreadInfo, | ||||
ThreadInfo, | ThreadInfo, | ||||
} from '../types/thread-types.js'; | } from '../types/thread-types.js'; | ||||
import { useResolvedThreadInfosObj } from '../utils/entity-helpers.js'; | import { useResolvedThreadInfosObj } from '../utils/entity-helpers.js'; | ||||
import { getNameForThreadEntity } from '../utils/entity-text.js'; | |||||
import { useSelector } from '../utils/redux-utils.js'; | import { useSelector } from '../utils/redux-utils.js'; | ||||
type Props = { | type Props = { | ||||
+children: React.Node, | +children: React.Node, | ||||
}; | }; | ||||
export type ChatMentionContextType = { | export type ChatMentionContextType = { | ||||
+getChatMentionSearchIndex: ( | +getChatMentionSearchIndex: ( | ||||
threadInfo: ThreadInfo, | threadInfo: ThreadInfo, | ||||
▲ Show 20 Lines • Show All 216 Lines • ▼ Show 20 Lines | for (const communityThreadID in chatMentionCandidatesObj) { | ||||
const searchIndex = new SentencePrefixSearchIndex(); | const searchIndex = new SentencePrefixSearchIndex(); | ||||
const searchIndexEntries = []; | const searchIndexEntries = []; | ||||
for (const threadID in chatMentionCandidatesObj[communityThreadID]) { | for (const threadID in chatMentionCandidatesObj[communityThreadID]) { | ||||
searchIndexEntries.push({ | searchIndexEntries.push({ | ||||
id: threadID, | id: threadID, | ||||
uiName: | uiName: | ||||
chatMentionCandidatesObj[communityThreadID][threadID].threadInfo | chatMentionCandidatesObj[communityThreadID][threadID].threadInfo | ||||
.uiName, | .uiName, | ||||
rawChatName: | |||||
chatMentionCandidatesObj[communityThreadID][threadID].rawChatName, | |||||
}); | }); | ||||
} | } | ||||
// Sort the keys so that the order of the search result is consistent | // Sort the keys so that the order of the search result is consistent | ||||
searchIndexEntries.sort(({ uiName: uiNameA }, { uiName: uiNameB }) => | searchIndexEntries.sort(({ uiName: uiNameA }, { uiName: uiNameB }) => | ||||
uiNameA.localeCompare(uiNameB), | uiNameA.localeCompare(uiNameB), | ||||
); | ); | ||||
for (const { id, uiName } of searchIndexEntries) { | for (const { id, uiName, rawChatName } of searchIndexEntries) { | ||||
searchIndex.addEntry(id, uiName); | const names = [uiName]; | ||||
if (rawChatName) { | |||||
typeof rawChatName === 'string' | |||||
? names.push(rawChatName) | |||||
: names.push(getNameForThreadEntity(rawChatName)); | |||||
} | |||||
searchIndex.addEntry(id, names.join(' ')); | |||||
} | } | ||||
result[communityThreadID] = searchIndex; | result[communityThreadID] = searchIndex; | ||||
} | } | ||||
return result; | return result; | ||||
}, [chatMentionCandidatesObj]); | }, [chatMentionCandidatesObj]); | ||||
} | } | ||||
export { ChatMentionContextProvider, ChatMentionContext }; | export { ChatMentionContextProvider, ChatMentionContext }; |