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

Function useExistingThreadInfoFinder

lib/shared/thread-utils.js:1177–1253  ·  view source on GitHub ↗
(
  baseThreadInfo: ?ThreadInfo,
)

Source from the content-addressed store, hash-verified

1175) => ?ThreadInfo;
1176
1177function useExistingThreadInfoFinder(
1178 baseThreadInfo: ?ThreadInfo,
1179): ExistingThreadInfoFinder {
1180 const threadInfos = useSelector(threadInfoSelector);
1181 const loggedInUserInfo = useLoggedInUserInfo();
1182
1183 const pendingToRealizedThreadIDs = useSelector(state =>
1184 pendingToRealizedThreadIDsSelector(state.threadStore.threadInfos),
1185 );
1186 return React.useCallback(
1187 (params: ExistingThreadInfoFinderParams): ?ThreadInfo => {
1188 if (!baseThreadInfo) {
1189 return null;
1190 }
1191
1192 const realizedThreadInfo = threadInfos[baseThreadInfo.id];
1193 if (realizedThreadInfo) {
1194 return realizedThreadInfo;
1195 }
1196
1197 if (!loggedInUserInfo || !threadIsPending(baseThreadInfo.id)) {
1198 return baseThreadInfo;
1199 }
1200 const viewerID = loggedInUserInfo?.id;
1201
1202 invariant(
1203 threadTypeCanBePending(baseThreadInfo.type),
1204 `ThreadInfo has pending ID ${baseThreadInfo.id}, but type that ` +
1205 `should not be pending ${baseThreadInfo.type}`,
1206 );
1207
1208 const { sourceMessageID } = baseThreadInfo;
1209
1210 let pendingThreadID;
1211 if (params.searching) {
1212 const { userInfoInputArray, matchProtocol, pendingThreadProtocol } =
1213 params;
1214 const protocol = getProtocolByName(pendingThreadProtocol);
1215 invariant(protocol, `unsupported protocol ${pendingThreadProtocol}`);
1216 pendingThreadID = getPendingThreadID(
1217 protocol.pendingThreadType(userInfoInputArray.length),
1218 [...userInfoInputArray.map(user => user.id), viewerID],
1219 sourceMessageID,
1220 matchProtocol,
1221 );
1222 } else {
1223 pendingThreadID = getPendingThreadID(
1224 baseThreadInfo.type,
1225 baseThreadInfo.members.map(member => member.id),
1226 sourceMessageID,
1227 threadSpecs[baseThreadInfo.type].protocol().protocolName,
1228 );
1229 }
1230 const realizedThreadID = pendingToRealizedThreadIDs.get(pendingThreadID);
1231 if (realizedThreadID && threadInfos[realizedThreadID]) {
1232 return threadInfos[realizedThreadID];
1233 }
1234

Calls 8

useSelectorFunction · 0.90
useLoggedInUserInfoFunction · 0.90
getProtocolByNameFunction · 0.90
threadIsPendingFunction · 0.85
threadTypeCanBePendingFunction · 0.85
getPendingThreadIDFunction · 0.85
createPendingThreadFunction · 0.85
getMethod · 0.65

Tested by

no test coverage detected