Changeset View
Changeset View
Standalone View
Standalone View
lib/selectors/user-selectors.js
// @flow | // @flow | ||||
import _memoize from 'lodash/memoize.js'; | import _memoize from 'lodash/memoize.js'; | ||||
import { createSelector } from 'reselect'; | import { createSelector } from 'reselect'; | ||||
import { | import { | ||||
getAvatarForUser, | getAvatarForUser, | ||||
getRandomDefaultEmojiAvatar, | getRandomDefaultEmojiAvatar, | ||||
} from '../shared/avatar-utils.js'; | } from '../shared/avatar-utils.js'; | ||||
import SearchIndex from '../shared/search-index.js'; | |||||
import SentencePrefixSearchIndex from '../shared/sentence-prefix-search-index.js'; | |||||
import { getSingleOtherUser } from '../shared/thread-utils.js'; | import { getSingleOtherUser } from '../shared/thread-utils.js'; | ||||
import type { ClientEmojiAvatar } from '../types/avatar-types'; | import type { ClientEmojiAvatar } from '../types/avatar-types'; | ||||
import type { MinimallyEncodedRawThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; | import type { MinimallyEncodedRawThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js'; | ||||
import type { BaseAppState } from '../types/redux-types.js'; | import type { BaseAppState } from '../types/redux-types.js'; | ||||
import { userRelationshipStatus } from '../types/relationship-types.js'; | import { userRelationshipStatus } from '../types/relationship-types.js'; | ||||
import { threadTypes } from '../types/thread-types-enum.js'; | import { threadTypes } from '../types/thread-types-enum.js'; | ||||
import type { | import type { | ||||
LegacyRawThreadInfo, | LegacyRawThreadInfo, | ||||
▲ Show 20 Lines • Show All 145 Lines • ▼ Show 20 Lines | |||||
const isLoggedIn = (state: BaseAppState<>): boolean => | const isLoggedIn = (state: BaseAppState<>): boolean => | ||||
!!( | !!( | ||||
state.currentUserInfo && | state.currentUserInfo && | ||||
!state.currentUserInfo.anonymous && | !state.currentUserInfo.anonymous && | ||||
state.dataLoaded | state.dataLoaded | ||||
); | ); | ||||
const addUsersToSearchIndex = ( | |||||
userInfos: UserInfos, | |||||
searchIndex: SearchIndex | SentencePrefixSearchIndex, | |||||
): void => { | |||||
for (const id in userInfos) { | |||||
const { username } = userInfos[id]; | |||||
if (!username) { | |||||
continue; | |||||
} | |||||
searchIndex.addEntry(id, username); | |||||
} | |||||
}; | |||||
const userStoreMentionSearchIndex: ( | |||||
state: BaseAppState<>, | |||||
) => SentencePrefixSearchIndex = createSelector( | |||||
(state: BaseAppState<>) => state.userStore.userInfos, | |||||
(userInfos: UserInfos) => { | |||||
const searchIndex = new SentencePrefixSearchIndex(); | |||||
addUsersToSearchIndex(userInfos, searchIndex); | |||||
return searchIndex; | |||||
}, | |||||
); | |||||
const usersWithPersonalThreadSelector: ( | const usersWithPersonalThreadSelector: ( | ||||
state: BaseAppState<>, | state: BaseAppState<>, | ||||
) => $ReadOnlySet<string> = createSelector( | ) => $ReadOnlySet<string> = createSelector( | ||||
(state: BaseAppState<>) => state.currentUserInfo && state.currentUserInfo.id, | (state: BaseAppState<>) => state.currentUserInfo && state.currentUserInfo.id, | ||||
(state: BaseAppState<>) => state.threadStore.threadInfos, | (state: BaseAppState<>) => state.threadStore.threadInfos, | ||||
(viewerID: ?string, threadInfos: RawThreadInfos) => { | (viewerID: ?string, threadInfos: RawThreadInfos) => { | ||||
const personalThreadMembers = new Set<string>(); | const personalThreadMembers = new Set<string>(); | ||||
Show All 30 Lines | |||||
); | ); | ||||
export { | export { | ||||
userIDsToRelativeUserInfos, | userIDsToRelativeUserInfos, | ||||
getRelativeMemberInfos, | getRelativeMemberInfos, | ||||
relativeMemberInfoSelectorForMembersOfThread, | relativeMemberInfoSelectorForMembersOfThread, | ||||
userInfoSelectorForPotentialMembers, | userInfoSelectorForPotentialMembers, | ||||
isLoggedIn, | isLoggedIn, | ||||
userStoreMentionSearchIndex, | |||||
usersWithPersonalThreadSelector, | usersWithPersonalThreadSelector, | ||||
savedEmojiAvatarSelectorForCurrentUser, | savedEmojiAvatarSelectorForCurrentUser, | ||||
}; | }; |