signal-desktop/ts/state/selectors/calling.ts

52 lines
1.6 KiB
TypeScript
Raw Normal View History

// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { createSelector } from 'reselect';
import { StateType } from '../reducer';
import {
CallingStateType,
CallsByConversationType,
DirectCallStateType,
2021-01-29 22:58:28 +00:00
getActiveCall,
} from '../ducks/calling';
2020-11-13 19:57:55 +00:00
import { CallMode, CallState } from '../../types/Calling';
import { getOwn } from '../../util/getOwn';
const getCalling = (state: StateType): CallingStateType => state.calling;
2021-01-29 22:58:28 +00:00
export const isInCall = createSelector(
getCalling,
(state: CallingStateType): boolean => Boolean(getActiveCall(state))
);
export const getCallsByConversation = createSelector(
getCalling,
(state: CallingStateType): CallsByConversationType =>
state.callsByConversation
);
export const getCallSelector = createSelector(
getCallsByConversation,
(callsByConversation: CallsByConversationType) => (conversationId: string) =>
getOwn(callsByConversation, conversationId)
);
// 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: CallsByConversationType
): undefined | DirectCallStateType => {
2020-11-13 19:57:55 +00:00
const result = Object.values(callsByConversation).find(
call =>
call.callMode === CallMode.Direct &&
call.isIncoming &&
call.callState === CallState.Ringing
);
// TypeScript needs a little help to be sure that this is a direct call.
return result?.callMode === CallMode.Direct ? result : undefined;
}
);