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

162 lines
4.3 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { createSelector } from 'reselect';
import type { StateType } from '../reducer';
import type {
CallingStateType,
CallsByConversationType,
2024-02-22 21:19:50 +00:00
AdhocCallsType,
CallLinksByRoomIdType,
DirectCallStateType,
GroupCallStateType,
} from '../ducks/calling';
2023-07-18 23:57:38 +00:00
import { getIncomingCall as getIncomingCallHelper } from '../ducks/callingHelpers';
import { CallMode } from '../../types/CallDisposition';
2024-02-22 21:19:50 +00:00
import type { CallLinkType } from '../../types/CallLink';
2022-07-08 20:46:25 +00:00
import { getUserACI } from './user';
import { getOwn } from '../../util/getOwn';
import type { AciString } from '../../types/ServiceId';
export type CallStateType = DirectCallStateType | GroupCallStateType;
const getCalling = (state: StateType): CallingStateType => state.calling;
export const getAvailableMicrophones = createSelector(
getCalling,
({ availableMicrophones }) => availableMicrophones
);
export const getSelectedMicrophone = createSelector(
getCalling,
({ selectedMicrophone }) => selectedMicrophone
);
export const getAvailableSpeakers = createSelector(
getCalling,
({ availableSpeakers }) => availableSpeakers
);
export const getSelectedSpeaker = createSelector(
getCalling,
({ selectedSpeaker }) => selectedSpeaker
);
export const getAvailableCameras = createSelector(
getCalling,
({ availableCameras }) => availableCameras
);
export const getSelectedCamera = createSelector(
getCalling,
({ selectedCamera }) => selectedCamera
);
export const getActiveCallState = createSelector(
2021-01-29 22:58:28 +00:00
getCalling,
(state: CallingStateType) => state.activeCallState
2021-01-29 22:58:28 +00:00
);
export const getCallsByConversation = createSelector(
getCalling,
(state: CallingStateType): CallsByConversationType =>
state.callsByConversation
);
2024-02-22 21:19:50 +00:00
export const getAdhocCalls = createSelector(
getCalling,
(state: CallingStateType): AdhocCallsType => state.adhocCalls
);
export const getCallLinksByRoomId = createSelector(
getCalling,
(state: CallingStateType): CallLinksByRoomIdType => state.callLinks
);
export type CallLinkSelectorType = (roomId: string) => CallLinkType | undefined;
export const getCallLinkSelector = createSelector(
getCallLinksByRoomId,
(callLinksByRoomId: CallLinksByRoomIdType): CallLinkSelectorType =>
2024-05-17 23:22:51 +00:00
(roomId: string): CallLinkType | undefined =>
getOwn(callLinksByRoomId, roomId)
);
export const getAllCallLinks = createSelector(
getCallLinksByRoomId,
(lookup): Array<CallLinkType> => Object.values(lookup)
2024-02-22 21:19:50 +00:00
);
export type CallSelectorType = (
conversationId: string
) => CallStateType | undefined;
export const getCallSelector = createSelector(
getCallsByConversation,
2021-11-11 22:43:05 +00:00
(callsByConversation: CallsByConversationType): CallSelectorType =>
(conversationId: string) =>
getOwn(callsByConversation, conversationId)
);
2024-02-22 21:19:50 +00:00
export type AdhocCallSelectorType = (
conversationId: string
) => GroupCallStateType | undefined;
export const getAdhocCallSelector = createSelector(
getAdhocCalls,
(adhocCalls: AdhocCallsType): AdhocCallSelectorType =>
(roomId: string) =>
getOwn(adhocCalls, roomId)
);
export const getActiveCall = createSelector(
getActiveCallState,
getCallSelector,
2024-02-22 21:19:50 +00:00
getAdhocCallSelector,
(
activeCallState,
callSelector,
adhocCallSelector
): undefined | CallStateType => {
const { callMode, conversationId } = activeCallState || {};
if (!conversationId) {
return undefined;
}
2024-02-22 21:19:50 +00:00
return callMode === CallMode.Adhoc
? adhocCallSelector(conversationId)
: callSelector(conversationId);
}
);
export const isInCall = createSelector(
getActiveCall,
(call: CallStateType | undefined): boolean => Boolean(call)
);
export const isInFullScreenCall = createSelector(
getCalling,
(state: CallingStateType): boolean =>
Boolean(state.activeCallState && !state.activeCallState.pip)
);
export const getIncomingCall = createSelector(
getCallsByConversation,
2022-07-08 20:46:25 +00:00
getUserACI,
(
2021-08-20 16:06:15 +00:00
callsByConversation: CallsByConversationType,
ourAci: AciString | undefined
): undefined | DirectCallStateType | GroupCallStateType => {
if (!ourAci) {
return undefined;
}
return getIncomingCallHelper(callsByConversation, ourAci);
}
);
export const areAnyCallsActiveOrRinging = createSelector(
getActiveCall,
getIncomingCall,
(activeCall, incomingCall): boolean => Boolean(activeCall || incomingCall)
);