diff --git a/src/caughtup/caughtUpSelectors.js b/src/caughtup/caughtUpSelectors.js index 49964b78d3f..6372bb0af62 100644 --- a/src/caughtup/caughtUpSelectors.js +++ b/src/caughtup/caughtUpSelectors.js @@ -1,5 +1,5 @@ /* @flow strict-local */ -import type { CaughtUp, CaughtUpState, GlobalState, Narrow } from '../types'; +import type { CaughtUp, CaughtUpState, PerAccountState, Narrow } from '../types'; import { NULL_OBJECT } from '../nullObjects'; import { keyFromNarrow } from '../utils/narrow'; @@ -9,7 +9,7 @@ export const DEFAULT_CAUGHTUP: CaughtUp = { newer: false, }; -export const getCaughtUp = (state: GlobalState): CaughtUpState => state.caughtUp || NULL_OBJECT; +export const getCaughtUp = (state: PerAccountState): CaughtUpState => state.caughtUp || NULL_OBJECT; -export const getCaughtUpForNarrow = (state: GlobalState, narrow: Narrow): CaughtUp => +export const getCaughtUpForNarrow = (state: PerAccountState, narrow: Narrow): CaughtUp => getCaughtUp(state)[keyFromNarrow(narrow)] || DEFAULT_CAUGHTUP; diff --git a/src/chat/fetchingSelectors.js b/src/chat/fetchingSelectors.js index 173af5498aa..7a97ea68430 100644 --- a/src/chat/fetchingSelectors.js +++ b/src/chat/fetchingSelectors.js @@ -1,10 +1,10 @@ /* @flow strict-local */ -import type { Fetching, GlobalState, Narrow } from '../types'; +import type { Fetching, PerAccountState, Narrow } from '../types'; import { getFetching } from '../directSelectors'; import { keyFromNarrow } from '../utils/narrow'; /** The value implicitly represented by a missing entry in FetchingState. */ export const DEFAULT_FETCHING = { older: false, newer: false }; -export const getFetchingForNarrow = (state: GlobalState, narrow: Narrow): Fetching => +export const getFetchingForNarrow = (state: PerAccountState, narrow: Narrow): Fetching => getFetching(state)[keyFromNarrow(narrow)] || DEFAULT_FETCHING; diff --git a/src/chat/narrowsSelectors.js b/src/chat/narrowsSelectors.js index 8242045d16c..70de15c7d9b 100644 --- a/src/chat/narrowsSelectors.js +++ b/src/chat/narrowsSelectors.js @@ -4,7 +4,7 @@ import isEqual from 'lodash.isequal'; import { createSelector } from 'reselect'; import type { - GlobalState, + PerAccountState, Message, Narrow, Outbox, @@ -61,7 +61,7 @@ export const outboxMessagesForNarrow: Selector<$ReadOnlyArray, Narrow> = ); export const getFetchedMessageIdsForNarrow = ( - state: GlobalState, + state: PerAccountState, narrow: Narrow, ): $ReadOnlyArray => getAllNarrows(state).get(keyFromNarrow(narrow)) || NULL_ARRAY; @@ -181,12 +181,12 @@ export const getShownMessagesForNarrow: Selector<$ReadOnlyArray { +export const getFirstMessageId = (state: PerAccountState, narrow: Narrow): number | void => { const ids = getFetchedMessageIdsForNarrow(state, narrow); return ids.length > 0 ? ids[0] : undefined; }; -export const getLastMessageId = (state: GlobalState, narrow: Narrow): number | void => { +export const getLastMessageId = (state: PerAccountState, narrow: Narrow): number | void => { const ids = getFetchedMessageIdsForNarrow(state, narrow); return ids.length > 0 ? ids[ids.length - 1] : undefined; }; diff --git a/src/directSelectors.js b/src/directSelectors.js index 33bb190f3cf..84698c1dd69 100644 --- a/src/directSelectors.js +++ b/src/directSelectors.js @@ -1,5 +1,6 @@ /* @flow strict-local */ import type { + PerAccountState, GlobalState, AccountsState, SubscriptionsState, @@ -36,42 +37,43 @@ export const getIsOnline = (state: GlobalState): boolean | null => state.session export const getDebug = (state: GlobalState): Debug => state.session.debug; export const getIsHydrated = (state: GlobalState): boolean => state.session.isHydrated; -export const getCanCreateStreams = (state: GlobalState): boolean => state.realm.canCreateStreams; +export const getCanCreateStreams = (state: PerAccountState): boolean => + state.realm.canCreateStreams; -export const getDrafts = (state: GlobalState): DraftsState => state.drafts; +export const getDrafts = (state: PerAccountState): DraftsState => state.drafts; -export const getLoading = (state: GlobalState): boolean => state.session.loading; +export const getLoading = (state: PerAccountState): boolean => state.session.loading; -export const getMessages = (state: GlobalState): MessagesState => state.messages; +export const getMessages = (state: PerAccountState): MessagesState => state.messages; -export const getMute = (state: GlobalState): MuteState => state.mute; +export const getMute = (state: PerAccountState): MuteState => state.mute; -export const getMutedUsers = (state: GlobalState): MutedUsersState => state.mutedUsers; +export const getMutedUsers = (state: PerAccountState): MutedUsersState => state.mutedUsers; -export const getTyping = (state: GlobalState): TypingState => state.typing; +export const getTyping = (state: PerAccountState): TypingState => state.typing; -export const getTopics = (state: GlobalState): TopicsState => state.topics; +export const getTopics = (state: PerAccountState): TopicsState => state.topics; -export const getUserGroups = (state: GlobalState): UserGroupsState => state.userGroups; +export const getUserGroups = (state: PerAccountState): UserGroupsState => state.userGroups; -export const getUserStatus = (state: GlobalState): UserStatusState => state.userStatus; +export const getUserStatus = (state: PerAccountState): UserStatusState => state.userStatus; /** * WARNING: despite the name, only (a) `is_active` users (b) excluding cross-realm bots. * * See `getAllUsers`. */ -export const getUsers = (state: GlobalState): UsersState => state.users; +export const getUsers = (state: PerAccountState): UsersState => state.users; -export const getFetching = (state: GlobalState): FetchingState => state.fetching; +export const getFetching = (state: PerAccountState): FetchingState => state.fetching; -export const getFlags = (state: GlobalState): FlagsState => state.flags; +export const getFlags = (state: PerAccountState): FlagsState => state.flags; -export const getAllNarrows = (state: GlobalState): NarrowsState => state.narrows; +export const getAllNarrows = (state: PerAccountState): NarrowsState => state.narrows; export const getSettings = (state: GlobalState): SettingsState => state.settings; -export const getSubscriptions = (state: GlobalState): SubscriptionsState => state.subscriptions; +export const getSubscriptions = (state: PerAccountState): SubscriptionsState => state.subscriptions; /** * All streams in the current realm. @@ -79,23 +81,23 @@ export const getSubscriptions = (state: GlobalState): SubscriptionsState => stat * This is rarely the right selector to use: consider `getStreamForId` * or `getStreamsById` instead. */ -export const getStreams = (state: GlobalState): StreamsState => state.streams; +export const getStreams = (state: PerAccountState): StreamsState => state.streams; -export const getPresence = (state: GlobalState): PresenceState => state.presence; +export const getPresence = (state: PerAccountState): PresenceState => state.presence; -export const getOutbox = (state: GlobalState): OutboxState => state.outbox; +export const getOutbox = (state: PerAccountState): OutboxState => state.outbox; -export const getRealm = (state: GlobalState): RealmState => state.realm; +export const getRealm = (state: PerAccountState): RealmState => state.realm; -export const getCrossRealmBots = (state: GlobalState): $ReadOnlyArray => +export const getCrossRealmBots = (state: PerAccountState): $ReadOnlyArray => state.realm.crossRealmBots; -export const getRawRealmEmoji = (state: GlobalState): RealmEmojiById => state.realm.emoji; +export const getRawRealmEmoji = (state: PerAccountState): RealmEmojiById => state.realm.emoji; -export const getNonActiveUsers = (state: GlobalState): $ReadOnlyArray => +export const getNonActiveUsers = (state: PerAccountState): $ReadOnlyArray => state.realm.nonActiveUsers; -export const getIsAdmin = (state: GlobalState): boolean => state.realm.isAdmin; +export const getIsAdmin = (state: PerAccountState): boolean => state.realm.isAdmin; -export const getVideoChatProvider = (state: GlobalState): VideoChatProvider | null => +export const getVideoChatProvider = (state: PerAccountState): VideoChatProvider | null => state.realm.videoChatProvider; diff --git a/src/drafts/draftsSelectors.js b/src/drafts/draftsSelectors.js index 2b4015b0b03..6d55442a5d6 100644 --- a/src/drafts/draftsSelectors.js +++ b/src/drafts/draftsSelectors.js @@ -1,6 +1,6 @@ /* @flow strict-local */ -import type { Narrow, GlobalState } from '../types'; +import type { Narrow, PerAccountState } from '../types'; import { keyFromNarrow } from '../utils/narrow'; -export const getDraftForNarrow = (state: GlobalState, narrow: Narrow): string => +export const getDraftForNarrow = (state: PerAccountState, narrow: Narrow): string => state.drafts[keyFromNarrow(narrow)] || ''; diff --git a/src/pm-conversations/pmConversationsSelectors.js b/src/pm-conversations/pmConversationsSelectors.js index 871c24e8326..6bf5e5ef6ed 100644 --- a/src/pm-conversations/pmConversationsSelectors.js +++ b/src/pm-conversations/pmConversationsSelectors.js @@ -2,7 +2,7 @@ import invariant from 'invariant'; import { createSelector } from 'reselect'; -import type { GlobalState, Message, PmConversationData, Selector } from '../types'; +import type { PerAccountState, Message, PmConversationData, Selector } from '../types'; import { assumeSecretlyGlobalState } from '../reduxTypes'; import { getPrivateMessages } from '../message/messageSelectors'; import { getAllUsersById, getOwnUserId } from '../users/userSelectors'; @@ -129,7 +129,7 @@ const getServerIsOld: Selector = createSelector( /** * The most recent PM conversations, with unread count and latest message ID. */ -export const getRecentConversations = (state: GlobalState): PmConversationData[] => +export const getRecentConversations = (state: PerAccountState): PmConversationData[] => getServerIsOld(state) ? getRecentConversationsLegacy(state) : getRecentConversationsModern(state); export const getUnreadConversations: Selector< diff --git a/src/subscriptions/subscriptionSelectors.js b/src/subscriptions/subscriptionSelectors.js index de75d562e7d..9583f7f7b51 100644 --- a/src/subscriptions/subscriptionSelectors.js +++ b/src/subscriptions/subscriptionSelectors.js @@ -1,7 +1,7 @@ /* @flow strict-local */ import { createSelector } from 'reselect'; -import type { GlobalState, Narrow, Selector, Stream, Subscription } from '../types'; +import type { PerAccountState, Narrow, Selector, Stream, Subscription } from '../types'; import { isStreamOrTopicNarrow, streamNameOfNarrow } from '../utils/narrow'; import { getSubscriptions, getStreams } from '../directSelectors'; @@ -70,7 +70,7 @@ export const getSubscribedStreams: Selector = createSelector( * See `getStreamsById` for use when a stream might not exist, or when * multiple streams are relevant. */ -export const getStreamForId = (state: GlobalState, streamId: number): Stream => { +export const getStreamForId = (state: PerAccountState, streamId: number): Stream => { const stream = getStreamsById(state).get(streamId); if (!stream) { throw new Error(`getStreamForId: missing stream: id ${streamId}`); @@ -97,7 +97,7 @@ export const getIsActiveStreamAnnouncementOnly: Selector = crea * * Gives undefined for narrows that are not stream or topic narrows. */ -export const getStreamColorForNarrow = (state: GlobalState, narrow: Narrow): string | void => { +export const getStreamColorForNarrow = (state: PerAccountState, narrow: Narrow): string | void => { if (!isStreamOrTopicNarrow(narrow)) { return undefined; } diff --git a/src/unread/unreadModel.js b/src/unread/unreadModel.js index 54e8aa65ced..2c9671c9e0c 100644 --- a/src/unread/unreadModel.js +++ b/src/unread/unreadModel.js @@ -10,7 +10,7 @@ import type { UnreadHuddlesState, UnreadMentionsState, } from './unreadModelTypes'; -import type { GlobalState } from '../reduxTypes'; +import type { PerAccountState } from '../reduxTypes'; import unreadPmsReducer from './unreadPmsReducer'; import unreadHuddlesReducer from './unreadHuddlesReducer'; import unreadMentionsReducer from './unreadMentionsReducer'; @@ -32,15 +32,18 @@ import { // /** The unread-messages state as a whole. */ -export const getUnread = (state: GlobalState): UnreadState => state.unread; +export const getUnread = (state: PerAccountState): UnreadState => state.unread; -export const getUnreadStreams = (state: GlobalState): UnreadStreamsState => state.unread.streams; +export const getUnreadStreams = (state: PerAccountState): UnreadStreamsState => + state.unread.streams; -export const getUnreadPms = (state: GlobalState): UnreadPmsState => state.unread.pms; +export const getUnreadPms = (state: PerAccountState): UnreadPmsState => state.unread.pms; -export const getUnreadHuddles = (state: GlobalState): UnreadHuddlesState => state.unread.huddles; +export const getUnreadHuddles = (state: PerAccountState): UnreadHuddlesState => + state.unread.huddles; -export const getUnreadMentions = (state: GlobalState): UnreadMentionsState => state.unread.mentions; +export const getUnreadMentions = (state: PerAccountState): UnreadMentionsState => + state.unread.mentions; // // @@ -116,7 +119,7 @@ function deleteMessages( function streamsReducer( state: UnreadStreamsState = initialStreamsState, action: Action, - globalState: GlobalState, + globalState: PerAccountState, ): UnreadStreamsState { switch (action.type) { case LOGOUT: @@ -208,7 +211,7 @@ function streamsReducer( export const reducer = ( state: void | UnreadState, action: Action, - globalState: GlobalState, + globalState: PerAccountState, ): UnreadState => { const nextState = { streams: streamsReducer(state?.streams, action, globalState), diff --git a/src/user-status/userStatusSelectors.js b/src/user-status/userStatusSelectors.js index 0bb661b3304..81090ec407d 100644 --- a/src/user-status/userStatusSelectors.js +++ b/src/user-status/userStatusSelectors.js @@ -1,5 +1,5 @@ /* @flow strict-local */ -import type { GlobalState, Selector, UserId, UserStatus } from '../types'; +import type { PerAccountState, Selector, UserId, UserStatus } from '../types'; import { getUserStatus } from '../directSelectors'; import { getOwnUserId } from '../users/userSelectors'; @@ -7,7 +7,7 @@ import { getOwnUserId } from '../users/userSelectors'; * Extract the user status object for the logged in user. * If no away status and status text have been set we do not have any data thus `undefined`. */ -export const getSelfUserStatus: Selector = (state: GlobalState) => { +export const getSelfUserStatus: Selector = (state: PerAccountState) => { const userStatus = getUserStatus(state); return userStatus[getOwnUserId(state)]; }; @@ -17,7 +17,7 @@ export const getSelfUserStatus: Selector = (state: GlobalState) => * It is `true` if explicitly set to that value. * If no value is set we consider it `false`. */ -export const getSelfUserAwayStatus = (state: GlobalState): boolean => { +export const getSelfUserAwayStatus = (state: PerAccountState): boolean => { const selfUserStatus = getSelfUserStatus(state); return !!(selfUserStatus && selfUserStatus.away); }; @@ -27,7 +27,7 @@ export const getSelfUserAwayStatus = (state: GlobalState): boolean => { * If it is set we get as result that value. * If no value is set we get a valid but empty string. */ -export const getSelfUserStatusText = (state: GlobalState): string => { +export const getSelfUserStatusText = (state: PerAccountState): string => { const selfUserStatus = getSelfUserStatus(state); return (selfUserStatus && selfUserStatus.status_text) || ''; }; @@ -36,7 +36,7 @@ export const getSelfUserStatusText = (state: GlobalState): string => { * Returns the `status text` value of the user with the given userId. * We return `undefined` if no value is set. */ -export const getUserStatusTextForUser = (state: GlobalState, userId: UserId): string | void => { +export const getUserStatusTextForUser = (state: PerAccountState, userId: UserId): string | void => { const userStatus = getUserStatus(state); return userStatus[userId] && userStatus[userId].status_text; }; diff --git a/src/users/userSelectors.js b/src/users/userSelectors.js index 8070092de07..d91c57789e3 100644 --- a/src/users/userSelectors.js +++ b/src/users/userSelectors.js @@ -1,7 +1,7 @@ /* @flow strict-local */ import { createSelector } from 'reselect'; -import type { GlobalState, UserOrBot, Selector, User, UserId } from '../types'; +import type { GlobalState, PerAccountState, UserOrBot, Selector, User, UserId } from '../types'; import { getUsers, getCrossRealmBots, getNonActiveUsers } from '../directSelectors'; import { getHasAuth, tryGetActiveAccount } from '../account/accountsSelectors'; @@ -82,7 +82,7 @@ export const getSortedUsers: Selector = createSelector(getUsers, users = */ // Not currently used, but should replace uses of `getOwnEmail` (e.g. inside // `getOwnUser`). See #3764. -export const getOwnUserId = (state: GlobalState): UserId => { +export const getOwnUserId = (state: PerAccountState): UserId => { const { user_id } = state.realm; if (user_id === undefined) { throw new Error('No server data found'); @@ -97,7 +97,7 @@ export const getOwnUserId = (state: GlobalState): UserId => { * * Prefer using `getOwnUserId` or `getOwnUser`; see #3764. */ -export const getOwnEmail = (state: GlobalState): string => { +export const getOwnEmail = (state: PerAccountState): string => { const { email } = state.realm; if (email === undefined) { throw new Error('No server data found'); @@ -116,7 +116,7 @@ export const getOwnEmail = (state: GlobalState): string => { * * See also `getOwnUserId` and `getOwnEmail`. */ -export const getOwnUser = (state: GlobalState): User => { +export const getOwnUser = (state: PerAccountState): User => { const ownUser = getUsersById(state).get(getOwnUserId(state)); if (ownUser === undefined) { throw new Error('Have ownUserId, but not found in user data'); @@ -134,7 +134,7 @@ export const getOwnUser = (state: GlobalState): User => { * throwing if none. That makes it a bit simpler to use in contexts where * we assume the relevant user must exist, which is true of most of the app. */ -export const tryGetUserForId = (state: GlobalState, userId: UserId): UserOrBot | null => +export const tryGetUserForId = (state: PerAccountState, userId: UserId): UserOrBot | null => getAllUsersById(state).get(userId) ?? null; /** @@ -147,7 +147,7 @@ export const tryGetUserForId = (state: GlobalState, userId: UserId): UserOrBot | * * See `tryGetUserForId` for a non-throwing version. */ -export const getUserForId = (state: GlobalState, userId: UserId): UserOrBot => { +export const getUserForId = (state: PerAccountState, userId: UserId): UserOrBot => { const user = tryGetUserForId(state, userId); if (!user) { throw new Error(`getUserForId: missing user: id ${userId}`); @@ -185,7 +185,7 @@ const getActiveUsersById: Selector> = createSelector( */ // To understand this implementation, see the comment about `is_active` in // the `User` type definition. -export const getUserIsActive = (state: GlobalState, userId: UserId): boolean => +export const getUserIsActive = (state: PerAccountState, userId: UserId): boolean => !!getActiveUsersById(state).get(userId); /**