Changeset View
Changeset View
Standalone View
Standalone View
web/utils/thread-utils.js
Show All 12 Lines | |||||
import { threadTypes } from 'lib/types/thread-types-enum.js'; | import { threadTypes } from 'lib/types/thread-types-enum.js'; | ||||
import { type ThreadInfo } from 'lib/types/thread-types.js'; | import { type ThreadInfo } from 'lib/types/thread-types.js'; | ||||
import type { AccountUserInfo } from 'lib/types/user-types.js'; | import type { AccountUserInfo } from 'lib/types/user-types.js'; | ||||
import { useSelector } from '../redux/redux-utils.js'; | import { useSelector } from '../redux/redux-utils.js'; | ||||
type InfosForPendingThread = { | type InfosForPendingThread = { | ||||
+isChatCreation: boolean, | +isChatCreation: boolean, | ||||
+selectedUserIDs: ?$ReadOnlyArray<string>, | +selectedUserInfos: $ReadOnlyArray<AccountUserInfo>, | ||||
+otherUserInfos: { [id: string]: AccountUserInfo }, | +otherUserInfos: { [id: string]: AccountUserInfo }, | ||||
+userInfoInputArray: $ReadOnlyArray<AccountUserInfo>, | |||||
}; | }; | ||||
function useInfosForPendingThread(): InfosForPendingThread { | function useInfosForPendingThread(): InfosForPendingThread { | ||||
const isChatCreation = useSelector( | const isChatCreation = useSelector( | ||||
state => state.navInfo.chatMode === 'create', | state => state.navInfo.chatMode === 'create', | ||||
); | ); | ||||
const selectedUserIDs = useSelector(state => state.navInfo.selectedUserList); | const selectedUserInfos = useSelector( | ||||
const otherUserInfos = useSelector(userInfoSelectorForPotentialMembers); | state => state.navInfo.selectedUserList ?? [], | ||||
const userInfoInputArray: $ReadOnlyArray<AccountUserInfo> = React.useMemo( | |||||
() => selectedUserIDs?.map(id => otherUserInfos[id]).filter(Boolean) ?? [], | |||||
[otherUserInfos, selectedUserIDs], | |||||
); | ); | ||||
const otherUserInfos = useSelector(userInfoSelectorForPotentialMembers); | |||||
return { | return { | ||||
isChatCreation, | isChatCreation, | ||||
selectedUserIDs, | selectedUserInfos, | ||||
otherUserInfos, | otherUserInfos, | ||||
userInfoInputArray, | |||||
}; | }; | ||||
} | } | ||||
function useThreadInfoForPossiblyPendingThread( | function useThreadInfoForPossiblyPendingThread( | ||||
activeChatThreadID: ?string, | activeChatThreadID: ?string, | ||||
): ?ThreadInfo { | ): ?ThreadInfo { | ||||
const { isChatCreation, userInfoInputArray } = useInfosForPendingThread(); | const { isChatCreation, selectedUserInfos } = useInfosForPendingThread(); | ||||
const loggedInUserInfo = useLoggedInUserInfo(); | const loggedInUserInfo = useLoggedInUserInfo(); | ||||
invariant(loggedInUserInfo, 'loggedInUserInfo should be set'); | invariant(loggedInUserInfo, 'loggedInUserInfo should be set'); | ||||
const pendingPrivateThread = React.useRef( | const pendingPrivateThread = React.useRef( | ||||
createPendingThread({ | createPendingThread({ | ||||
viewerID: loggedInUserInfo.id, | viewerID: loggedInUserInfo.id, | ||||
threadType: threadTypes.PRIVATE, | threadType: threadTypes.PRIVATE, | ||||
Show All 25 Lines | if (activeChatThreadID) { | ||||
return activeThreadInfo; | return activeThreadInfo; | ||||
} | } | ||||
} | } | ||||
return state.navInfo.pendingThread; | return state.navInfo.pendingThread; | ||||
}); | }); | ||||
const existingThreadInfoFinder = useExistingThreadInfoFinder(baseThreadInfo); | const existingThreadInfoFinder = useExistingThreadInfoFinder(baseThreadInfo); | ||||
const threadInfo = React.useMemo(() => { | const threadInfo = React.useMemo(() => { | ||||
if (isChatCreation) { | if (isChatCreation) { | ||||
if (userInfoInputArray.length === 0) { | if (selectedUserInfos.length === 0) { | ||||
return pendingNewThread; | return pendingNewThread; | ||||
} | } | ||||
return existingThreadInfoFinderForCreatingThread({ | return existingThreadInfoFinderForCreatingThread({ | ||||
searching: true, | searching: true, | ||||
userInfoInputArray, | userInfoInputArray: selectedUserInfos, | ||||
}); | }); | ||||
} | } | ||||
return existingThreadInfoFinder({ | return existingThreadInfoFinder({ | ||||
searching: false, | searching: false, | ||||
userInfoInputArray: [], | userInfoInputArray: [], | ||||
}); | }); | ||||
}, [ | }, [ | ||||
existingThreadInfoFinder, | existingThreadInfoFinder, | ||||
existingThreadInfoFinderForCreatingThread, | existingThreadInfoFinderForCreatingThread, | ||||
isChatCreation, | isChatCreation, | ||||
userInfoInputArray, | |||||
pendingNewThread, | pendingNewThread, | ||||
selectedUserInfos, | |||||
]); | ]); | ||||
return threadInfo; | return threadInfo; | ||||
} | } | ||||
export { useThreadInfoForPossiblyPendingThread, useInfosForPendingThread }; | export { useThreadInfoForPossiblyPendingThread, useInfosForPendingThread }; |