Page MenuHomePhabricator

D8833.id29997.diff
No OneTemporary

D8833.id29997.diff

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
@@ -77,6 +77,9 @@
type ClientNewThreadRequest,
type NewThreadResult,
type ChangeThreadSettingsPayload,
+ type ResolvedThreadInfo,
+ type ChatMentionCandidatesObj,
+ type ChatMentionCandidates,
} from '../types/thread-types.js';
import { updateTypes } from '../types/update-types-enum.js';
import { type ClientUpdateInfo } from '../types/update-types.js';
@@ -92,6 +95,7 @@
} from '../utils/action-utils.js';
import type { DispatchActionPromise } from '../utils/action-utils.js';
import type { GetENSNames } from '../utils/ens-helpers.js';
+import { useResolvedThreadInfosObj } from '../utils/entity-helpers.js';
import {
ET,
entityTextToRawString,
@@ -1621,6 +1625,100 @@
} within this ${communityOrThreadNoun(threadInfo)}`;
}
+function getChatMentionCandidates(threadInfos: {
+ +[id: string]: ResolvedThreadInfo,
+}): ChatMentionCandidatesObj {
+ const result = {};
+ const visitedGenesisCommunityThreads = new Set();
+ for (const currentThreadID in threadInfos) {
+ const currentThreadInfo = threadInfos[currentThreadID];
+ const { community: currentThreadCommunity } = currentThreadInfo;
+ if (!currentThreadCommunity) {
+ result[currentThreadID] = { [currentThreadID]: currentThreadInfo };
+ continue;
+ }
+ // Handle GENESIS community case: mentioning inside GENESIS should only
+ // show chats and threads inside the top level that is below GENESIS.
+ if (threadInfos[currentThreadCommunity].type === threadTypes.GENESIS) {
+ if (visitedGenesisCommunityThreads.has(currentThreadID)) {
+ continue;
+ }
+ const threadTraversePath = [currentThreadInfo];
+ visitedGenesisCommunityThreads.add(currentThreadID);
+ let currentlySelectedThreadID = currentThreadInfo.parentThreadID;
+ while (currentlySelectedThreadID) {
+ const currentlySelectedThreadInfo =
+ threadInfos[currentlySelectedThreadID];
+ if (
+ visitedGenesisCommunityThreads.has(currentlySelectedThreadID) ||
+ currentlySelectedThreadInfo.type === threadTypes.GENESIS
+ ) {
+ break;
+ }
+ threadTraversePath.push(currentlySelectedThreadInfo);
+ visitedGenesisCommunityThreads.add(currentlySelectedThreadID);
+ currentlySelectedThreadID = currentlySelectedThreadInfo.parentThreadID;
+ }
+ const lastThreadInTraversePath =
+ threadTraversePath[threadTraversePath.length - 1];
+ const lastThreadInTraversePathParentID =
+ lastThreadInTraversePath.parentThreadID ?? lastThreadInTraversePath.id;
+ if (
+ threadInfos[lastThreadInTraversePathParentID].type ===
+ threadTypes.GENESIS
+ ) {
+ const threadObjectFromTraversePath = {};
+ for (const threadInfo of threadTraversePath) {
+ threadObjectFromTraversePath[threadInfo.id] = threadInfo;
+ result[threadInfo.id] = threadObjectFromTraversePath;
+ }
+ } else {
+ if (!result[lastThreadInTraversePathParentID]) {
+ result[lastThreadInTraversePathParentID] = {};
+ }
+ for (const threadInfo of threadTraversePath) {
+ result[lastThreadInTraversePathParentID][threadInfo.id] = threadInfo;
+ result[threadInfo.id] = result[lastThreadInTraversePathParentID];
+ }
+ }
+ continue;
+ }
+ result[currentThreadCommunity][currentThreadID] = currentThreadInfo;
+ result[currentThreadID] = result[currentThreadCommunity];
+ }
+ return result;
+}
+
+function useChatMentionCandidatesObj(): ChatMentionCandidatesObj {
+ const threadInfos = useSelector(threadInfoSelector);
+ const resolvedThreadInfos = useResolvedThreadInfosObj(threadInfos);
+ return React.useMemo(
+ () => getChatMentionCandidates(resolvedThreadInfos),
+ [resolvedThreadInfos],
+ );
+}
+
+function threadChatMentionCandidates(
+ threadInfo: ThreadInfo,
+ chatMentionCandidates: ChatMentionCandidatesObj,
+): ChatMentionCandidates {
+ return (
+ chatMentionCandidates[threadInfo.id] ??
+ (threadInfo.containingThreadID &&
+ chatMentionCandidates[threadInfo.containingThreadID])
+ );
+}
+
+function useThreadChatMentionCandidates(
+ threadInfo: ThreadInfo,
+): ChatMentionCandidates {
+ const chatMentionCandidates = useChatMentionCandidatesObj();
+ return React.useMemo(
+ () => threadChatMentionCandidates(threadInfo, chatMentionCandidates),
+ [chatMentionCandidates, threadInfo],
+ );
+}
+
export {
threadHasPermission,
viewerIsMember,
@@ -1689,4 +1787,7 @@
useRoleMemberCountsForCommunity,
useRoleUserSurfacedPermissions,
getThreadsToDeleteText,
+ useChatMentionCandidatesObj,
+ threadChatMentionCandidates,
+ useThreadChatMentionCandidates,
};
diff --git a/lib/types/thread-types.js b/lib/types/thread-types.js
--- a/lib/types/thread-types.js
+++ b/lib/types/thread-types.js
@@ -456,3 +456,8 @@
export const maxUnreadSidebars = 5;
export type ThreadStoreThreadInfos = { +[id: string]: RawThreadInfo };
+
+export type ChatMentionCandidates = { +[id: string]: ResolvedThreadInfo };
+export type ChatMentionCandidatesObj = {
+ +[id: string]: ChatMentionCandidates,
+};

File Metadata

Mime Type
text/plain
Expires
Wed, Dec 25, 10:55 PM (11 h, 46 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2703370
Default Alt Text
D8833.id29997.diff (5 KB)

Event Timeline