Changeset View
Changeset View
Standalone View
Standalone View
web/redux/redux-setup.js
// @flow | // @flow | ||||
import invariant from 'invariant'; | import invariant from 'invariant'; | ||||
import type { PersistState } from 'redux-persist/es/types.js'; | import type { PersistState } from 'redux-persist/es/types.js'; | ||||
import { | import { | ||||
logOutActionTypes, | logOutActionTypes, | ||||
deleteAccountActionTypes, | deleteAccountActionTypes, | ||||
} from 'lib/actions/user-actions.js'; | } from 'lib/actions/user-actions.js'; | ||||
import baseReducer from 'lib/reducers/master-reducer.js'; | import baseReducer from 'lib/reducers/master-reducer.js'; | ||||
import { nonThreadCalendarFilters } from 'lib/selectors/calendar-filter-selectors.js'; | |||||
import { mostRecentlyReadThreadSelector } from 'lib/selectors/thread-selectors.js'; | import { mostRecentlyReadThreadSelector } from 'lib/selectors/thread-selectors.js'; | ||||
import { isLoggedIn } from 'lib/selectors/user-selectors.js'; | import { isLoggedIn } from 'lib/selectors/user-selectors.js'; | ||||
import { invalidSessionDowngrade } from 'lib/shared/session-utils.js'; | import { invalidSessionDowngrade } from 'lib/shared/session-utils.js'; | ||||
import type { Shape } from 'lib/types/core.js'; | import type { Shape } from 'lib/types/core.js'; | ||||
import type { | import type { | ||||
CryptoStore, | CryptoStore, | ||||
OLMIdentityKeys, | OLMIdentityKeys, | ||||
PickledOLMAccount, | PickledOLMAccount, | ||||
} from 'lib/types/crypto-types.js'; | } from 'lib/types/crypto-types.js'; | ||||
import type { DraftStore } from 'lib/types/draft-types.js'; | import type { DraftStore } from 'lib/types/draft-types.js'; | ||||
import type { EnabledApps } from 'lib/types/enabled-apps.js'; | import type { EnabledApps } from 'lib/types/enabled-apps.js'; | ||||
import type { EntryStore } from 'lib/types/entry-types.js'; | import type { EntryStore } from 'lib/types/entry-types.js'; | ||||
import { | import { type CalendarFilter } from 'lib/types/filter-types.js'; | ||||
type CalendarFilter, | |||||
calendarThreadFilterTypes, | |||||
} from 'lib/types/filter-types.js'; | |||||
import type { LifecycleState } from 'lib/types/lifecycle-state-types.js'; | import type { LifecycleState } from 'lib/types/lifecycle-state-types.js'; | ||||
import type { LoadingStatus } from 'lib/types/loading-types.js'; | import type { LoadingStatus } from 'lib/types/loading-types.js'; | ||||
import type { MessageStore } from 'lib/types/message-types.js'; | import type { MessageStore } from 'lib/types/message-types.js'; | ||||
import type { UserPolicies } from 'lib/types/policy-types.js'; | import type { UserPolicies } from 'lib/types/policy-types.js'; | ||||
import type { BaseAction } from 'lib/types/redux-types.js'; | import type { BaseAction } from 'lib/types/redux-types.js'; | ||||
import type { ReportStore } from 'lib/types/report-types.js'; | import type { ReportStore } from 'lib/types/report-types.js'; | ||||
import type { ConnectionInfo } from 'lib/types/socket-types.js'; | import type { ConnectionInfo } from 'lib/types/socket-types.js'; | ||||
import type { ThreadStore } from 'lib/types/thread-types.js'; | import type { ThreadStore } from 'lib/types/thread-types.js'; | ||||
import type { CurrentUserInfo, UserStore } from 'lib/types/user-types.js'; | import type { CurrentUserInfo, UserStore } from 'lib/types/user-types.js'; | ||||
import { setNewSessionActionType } from 'lib/utils/action-utils.js'; | import { setNewSessionActionType } from 'lib/utils/action-utils.js'; | ||||
import type { NotifPermissionAlertInfo } from 'lib/utils/push-alerts.js'; | import type { NotifPermissionAlertInfo } from 'lib/utils/push-alerts.js'; | ||||
import { | import { | ||||
updateWindowActiveActionType, | updateWindowActiveActionType, | ||||
setDeviceIDActionType, | setDeviceIDActionType, | ||||
updateNavInfoActionType, | updateNavInfoActionType, | ||||
updateWindowDimensionsActionType, | updateWindowDimensionsActionType, | ||||
updateCalendarCommunityFilter, | |||||
clearCalendarCommunityFilter, | |||||
} from './action-types.js'; | } from './action-types.js'; | ||||
import { reduceCommunityPickerStore } from './community-picker-reducer.js'; | |||||
import { | import { | ||||
reduceCryptoStore, | reduceCryptoStore, | ||||
setPrimaryIdentityKeys, | setPrimaryIdentityKeys, | ||||
setNotificationIdentityKeys, | setNotificationIdentityKeys, | ||||
setPickledNotificationAccount, | setPickledNotificationAccount, | ||||
setPickledPrimaryAccount, | setPickledPrimaryAccount, | ||||
} from './crypto-store-reducer.js'; | } from './crypto-store-reducer.js'; | ||||
import { reduceDeviceID } from './device-id-reducer.js'; | import { reduceDeviceID } from './device-id-reducer.js'; | ||||
import reduceNavInfo from './nav-reducer.js'; | import reduceNavInfo from './nav-reducer.js'; | ||||
import { getVisibility } from './visibility.js'; | import { getVisibility } from './visibility.js'; | ||||
import { databaseModule } from '../database/database-module-provider.js'; | import { databaseModule } from '../database/database-module-provider.js'; | ||||
import { filterThreadIDsBelongingToCommunity } from '../selectors/calendar-selectors.js'; | |||||
import { activeThreadSelector } from '../selectors/nav-selectors.js'; | import { activeThreadSelector } from '../selectors/nav-selectors.js'; | ||||
import { type NavInfo } from '../types/nav-types.js'; | import { type NavInfo } from '../types/nav-types.js'; | ||||
import { workerRequestMessageTypes } from '../types/worker-types.js'; | import { workerRequestMessageTypes } from '../types/worker-types.js'; | ||||
export type WindowDimensions = { width: number, height: number }; | export type WindowDimensions = { width: number, height: number }; | ||||
export type CommunityPickerStore = { | export type CommunityPickerStore = { | ||||
+chat: ?string, | +chat: ?string, | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | | { | ||||
} | } | ||||
| { | | { | ||||
type: 'SET_DEVICE_ID', | type: 'SET_DEVICE_ID', | ||||
payload: string, | payload: string, | ||||
} | } | ||||
| { +type: 'SET_PRIMARY_IDENTITY_KEYS', payload: ?OLMIdentityKeys } | | { +type: 'SET_PRIMARY_IDENTITY_KEYS', payload: ?OLMIdentityKeys } | ||||
| { +type: 'SET_NOTIFICATION_IDENTITY_KEYS', payload: ?OLMIdentityKeys } | | { +type: 'SET_NOTIFICATION_IDENTITY_KEYS', payload: ?OLMIdentityKeys } | ||||
| { +type: 'SET_PICKLED_PRIMARY_ACCOUNT', payload: ?PickledOLMAccount } | | { +type: 'SET_PICKLED_PRIMARY_ACCOUNT', payload: ?PickledOLMAccount } | ||||
| { +type: 'SET_PICKLED_NOTIFICATION_ACCOUNT', payload: ?PickledOLMAccount } | | { +type: 'SET_PICKLED_NOTIFICATION_ACCOUNT', payload: ?PickledOLMAccount }; | ||||
| { | |||||
+type: 'UPDATE_CALENDAR_COMMUNITY_FILTER', | |||||
+payload: string, | |||||
} | |||||
| { | |||||
+type: 'CLEAR_CALENDAR_COMMUNITY_FILTER', | |||||
+payload: void, | |||||
}; | |||||
export function reducer(oldState: AppState | void, action: Action): AppState { | export function reducer(oldState: AppState | void, action: Action): AppState { | ||||
invariant(oldState, 'should be set'); | invariant(oldState, 'should be set'); | ||||
let state = oldState; | let state = oldState; | ||||
if (action.type === updateWindowDimensionsActionType) { | if (action.type === updateWindowDimensionsActionType) { | ||||
return validateState(oldState, { | return validateState(oldState, { | ||||
...state, | ...state, | ||||
windowDimensions: action.payload, | windowDimensions: action.payload, | ||||
}); | }); | ||||
} else if (action.type === updateWindowActiveActionType) { | } else if (action.type === updateWindowActiveActionType) { | ||||
return validateState(oldState, { | return validateState(oldState, { | ||||
...state, | ...state, | ||||
windowActive: action.payload, | windowActive: action.payload, | ||||
}); | }); | ||||
} else if (action.type === updateCalendarCommunityFilter) { | |||||
const nonThreadFilters = nonThreadCalendarFilters(state.calendarFilters); | |||||
const threadIDs = Array.from( | |||||
filterThreadIDsBelongingToCommunity( | |||||
action.payload, | |||||
state.threadStore.threadInfos, | |||||
), | |||||
); | |||||
return { | |||||
...state, | |||||
calendarFilters: [ | |||||
...nonThreadFilters, | |||||
{ | |||||
type: calendarThreadFilterTypes.THREAD_LIST, | |||||
threadIDs, | |||||
}, | |||||
], | |||||
communityPickerStore: { | |||||
...state.communityPickerStore, | |||||
calendar: action.payload, | |||||
}, | |||||
}; | |||||
} else if (action.type === clearCalendarCommunityFilter) { | |||||
const nonThreadFilters = nonThreadCalendarFilters(state.calendarFilters); | |||||
return { | |||||
...state, | |||||
calendarFilters: nonThreadFilters, | |||||
communityPickerStore: { | |||||
...state.communityPickerStore, | |||||
calendar: null, | |||||
}, | |||||
}; | |||||
} else if (action.type === setNewSessionActionType) { | } else if (action.type === setNewSessionActionType) { | ||||
if ( | if ( | ||||
invalidSessionDowngrade( | invalidSessionDowngrade( | ||||
oldState, | oldState, | ||||
action.payload.sessionChange.currentUserInfo, | action.payload.sessionChange.currentUserInfo, | ||||
action.payload.preRequestUserState, | action.payload.preRequestUserState, | ||||
) | ) | ||||
) { | ) { | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | if (draftStoreOperations.length) { | ||||
await databaseModule.schedule({ | await databaseModule.schedule({ | ||||
type: workerRequestMessageTypes.PROCESS_STORE_OPERATIONS, | type: workerRequestMessageTypes.PROCESS_STORE_OPERATIONS, | ||||
storeOperations: { draftStoreOperations }, | storeOperations: { draftStoreOperations }, | ||||
}); | }); | ||||
})(); | })(); | ||||
} | } | ||||
} | } | ||||
const communityPickerStore = reduceCommunityPickerStore( | |||||
state.communityPickerStore, | |||||
action, | |||||
); | |||||
state = { | state = { | ||||
...state, | ...state, | ||||
navInfo: reduceNavInfo( | navInfo: reduceNavInfo( | ||||
state.navInfo, | state.navInfo, | ||||
action, | action, | ||||
state.threadStore.threadInfos, | state.threadStore.threadInfos, | ||||
), | ), | ||||
deviceID: reduceDeviceID(state.deviceID, action), | deviceID: reduceDeviceID(state.deviceID, action), | ||||
cryptoStore: reduceCryptoStore(state.cryptoStore, action), | cryptoStore: reduceCryptoStore(state.cryptoStore, action), | ||||
communityPickerStore, | |||||
}; | }; | ||||
return validateState(oldState, state); | return validateState(oldState, state); | ||||
} | } | ||||
function validateState(oldState: AppState, state: AppState): AppState { | function validateState(oldState: AppState, state: AppState): AppState { | ||||
if ( | if ( | ||||
(state.navInfo.activeChatThreadID && | (state.navInfo.activeChatThreadID && | ||||
▲ Show 20 Lines • Show All 93 Lines • Show Last 20 Lines |