MCPcopy Create free account
hub / github.com/CommE2E/comm / useThreadInfoForPossiblyPendingThread

Function useThreadInfoForPossiblyPendingThread

web/utils/thread-utils.js:38–114  ·  view source on GitHub ↗
(
  activeChatThreadID: ?string,
  selectedProtocol: ?ProtocolName,
)

Source from the content-addressed store, hash-verified

36}
37
38function useThreadInfoForPossiblyPendingThread(
39 activeChatThreadID: ?string,
40 selectedProtocol: ?ProtocolName,
41): ?ThreadInfo {
42 const { isChatCreation, selectedUserInfos } = useInfosForPendingThread();
43
44 const loggedInUserInfo = useLoggedInUserInfo();
45 invariant(loggedInUserInfo, 'loggedInUserInfo should be set');
46
47 const pendingThread = React.useMemo(() => {
48 const protocol = getProtocolByName(selectedProtocol) ?? dmThreadProtocol;
49 return createPendingThread({
50 viewerID: loggedInUserInfo.id,
51 threadType: protocol.pendingThreadType(1),
52 members: [loggedInUserInfo],
53 });
54 }, [loggedInUserInfo, selectedProtocol]);
55
56 const newThreadID = 'pending/new_thread';
57 const pendingNewThread = React.useMemo(() => {
58 const protocol = getProtocolByName(selectedProtocol) ?? dmThreadProtocol;
59 return {
60 ...createPendingThread({
61 viewerID: loggedInUserInfo.id,
62 threadType: protocol.pendingThreadType(1),
63 members: [loggedInUserInfo],
64 name: 'New thread',
65 }),
66 id: newThreadID,
67 };
68 }, [loggedInUserInfo, selectedProtocol]);
69
70 const existingThreadInfoFinderForCreatingThread = useExistingThreadInfoFinder(
71 pendingThread,
72 selectedProtocol,
73 );
74
75 const baseThreadInfo = useSelector(state => {
76 if (activeChatThreadID) {
77 const activeThreadInfo = threadInfoSelector(state)[activeChatThreadID];
78 if (activeThreadInfo) {
79 return activeThreadInfo;
80 }
81 }
82 return state.navInfo.pendingThread;
83 });
84 const existingThreadInfoFinder = useExistingThreadInfoFinder(
85 baseThreadInfo,
86 selectedProtocol,
87 );
88
89 const threadInfo = React.useMemo(() => {
90 if (isChatCreation) {
91 if (selectedUserInfos.length === 0) {
92 return pendingNewThread;
93 }
94
95 return existingThreadInfoFinderForCreatingThread({

Callers 3

TopbarFunction · 0.90

Calls 6

useLoggedInUserInfoFunction · 0.90
getProtocolByNameFunction · 0.90
createPendingThreadFunction · 0.90
useSelectorFunction · 0.90
useInfosForPendingThreadFunction · 0.85

Tested by

no test coverage detected