diff --git a/ts/state/ducks/calling.ts b/ts/state/ducks/calling.ts index faad50d333..71cfdadf86 100644 --- a/ts/state/ducks/calling.ts +++ b/ts/state/ducks/calling.ts @@ -89,10 +89,12 @@ export interface ActiveCallStateType { settingsDialogOpen: boolean; } +export interface CallsByConversationType { + [conversationId: string]: DirectCallStateType | GroupCallStateType; +} + export type CallingStateType = MediaDeviceSettings & { - callsByConversation: { - [conversationId: string]: DirectCallStateType | GroupCallStateType; - }; + callsByConversation: CallsByConversationType; activeCallState?: ActiveCallStateType; }; diff --git a/ts/state/selectors/calling.ts b/ts/state/selectors/calling.ts index 8a9e7ed86d..5f065399aa 100644 --- a/ts/state/selectors/calling.ts +++ b/ts/state/selectors/calling.ts @@ -3,17 +3,29 @@ import { createSelector } from 'reselect'; -import { CallingStateType, DirectCallStateType } from '../ducks/calling'; +import { StateType } from '../reducer'; +import { + CallingStateType, + CallsByConversationType, + DirectCallStateType, +} from '../ducks/calling'; import { CallMode, CallState } from '../../types/Calling'; -const getCallsByConversation = (state: CallingStateType) => - state.callsByConversation; +const getCalling = (state: StateType): CallingStateType => state.calling; + +const getCallsByConversation = createSelector( + getCalling, + (state: CallingStateType): CallsByConversationType => + state.callsByConversation +); // In theory, there could be multiple incoming calls. In practice, neither RingRTC nor the // UI are ready to handle this. export const getIncomingCall = createSelector( getCallsByConversation, - (callsByConversation): undefined | DirectCallStateType => { + ( + callsByConversation: CallsByConversationType + ): undefined | DirectCallStateType => { const result = Object.values(callsByConversation).find( call => call.callMode === CallMode.Direct && diff --git a/ts/state/smart/CallManager.tsx b/ts/state/smart/CallManager.tsx index b43b4e360d..5c2b65e94c 100644 --- a/ts/state/smart/CallManager.tsx +++ b/ts/state/smart/CallManager.tsx @@ -124,7 +124,7 @@ const mapStateToActiveCallProp = (state: StateType) => { }; const mapStateToIncomingCallProp = (state: StateType) => { - const call = getIncomingCall(state.calling); + const call = getIncomingCall(state); if (!call) { return undefined; } diff --git a/ts/test-electron/state/selectors/calling_test.ts b/ts/test-electron/state/selectors/calling_test.ts index 55394d0991..1748e82513 100644 --- a/ts/test-electron/state/selectors/calling_test.ts +++ b/ts/test-electron/state/selectors/calling_test.ts @@ -2,11 +2,20 @@ // SPDX-License-Identifier: AGPL-3.0-only import { assert } from 'chai'; +import { reducer as rootReducer } from '../../../state/reducer'; +import { noopAction } from '../../../state/ducks/noop'; import { CallMode, CallState } from '../../../types/Calling'; import { getIncomingCall } from '../../../state/selectors/calling'; import { getEmptyState, CallingStateType } from '../../../state/ducks/calling'; describe('state/selectors/calling', () => { + const getEmptyRootState = () => rootReducer(undefined, noopAction()); + + const getCallingState = (calling: CallingStateType) => ({ + ...getEmptyRootState(), + calling, + }); + const stateWithDirectCall: CallingStateType = { ...getEmptyState(), callsByConversation: { @@ -49,23 +58,28 @@ describe('state/selectors/calling', () => { describe('getIncomingCall', () => { it('returns undefined if there are no calls', () => { - assert.isUndefined(getIncomingCall(getEmptyState())); + assert.isUndefined(getIncomingCall(getEmptyRootState())); }); it('returns undefined if there is no incoming call', () => { - assert.isUndefined(getIncomingCall(stateWithDirectCall)); - assert.isUndefined(getIncomingCall(stateWithActiveDirectCall)); + assert.isUndefined(getIncomingCall(getCallingState(stateWithDirectCall))); + assert.isUndefined( + getIncomingCall(getCallingState(stateWithActiveDirectCall)) + ); }); it('returns the incoming call', () => { - assert.deepEqual(getIncomingCall(stateWithIncomingDirectCall), { - callMode: CallMode.Direct, - conversationId: 'fake-direct-call-conversation-id', - callState: CallState.Ringing, - isIncoming: true, - isVideoCall: false, - hasRemoteVideo: false, - }); + assert.deepEqual( + getIncomingCall(getCallingState(stateWithIncomingDirectCall)), + { + callMode: CallMode.Direct, + conversationId: 'fake-direct-call-conversation-id', + callState: CallState.Ringing, + isIncoming: true, + isVideoCall: false, + hasRemoteVideo: false, + } + ); }); }); });