signal-desktop/ts/components/CallScreen.stories.tsx

385 lines
10 KiB
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2020-10-30 20:34:04 +00:00
// SPDX-License-Identifier: AGPL-3.0-only
2020-06-04 18:16:19 +00:00
import * as React from 'react';
2021-01-08 18:58:28 +00:00
import { times } from 'lodash';
2020-09-12 00:46:52 +00:00
import { action } from '@storybook/addon-actions';
import type { Meta } from '@storybook/react';
import type { GroupCallRemoteParticipantType } from '../types/Calling';
import {
CallMode,
CallViewMode,
CallState,
GroupCallConnectionState,
GroupCallJoinState,
} from '../types/Calling';
import { generateAci } from '../types/ServiceId';
import type { ConversationType } from '../state/ducks/conversations';
2021-05-28 16:15:17 +00:00
import { AvatarColors } from '../types/Colors';
import type { PropsType } from './CallScreen';
import { CallScreen } from './CallScreen';
2021-09-18 00:30:08 +00:00
import { setupI18n } from '../util/setupI18n';
2020-12-02 18:14:03 +00:00
import { missingCaseError } from '../util/missingCaseError';
2021-10-26 22:59:08 +00:00
import {
getDefaultConversation,
2023-08-16 20:54:39 +00:00
getDefaultConversationWithServiceId,
2021-10-26 22:59:08 +00:00
} from '../test-both/helpers/getDefaultConversation';
2021-01-08 18:58:28 +00:00
import { fakeGetGroupCallVideoFrameSource } from '../test-both/helpers/fakeGetGroupCallVideoFrameSource';
2020-06-04 18:16:19 +00:00
import enMessages from '../../_locales/en/messages.json';
const MAX_PARTICIPANTS = 64;
2021-01-08 18:58:28 +00:00
2020-06-04 18:16:19 +00:00
const i18n = setupI18n('en', enMessages);
2021-05-07 22:21:10 +00:00
const conversation = getDefaultConversation({
2020-12-02 18:14:03 +00:00
id: '3051234567',
avatarPath: undefined,
2021-05-28 16:15:17 +00:00
color: AvatarColors[0],
2020-12-02 18:14:03 +00:00
title: 'Rick Sanchez',
name: 'Rick Sanchez',
phoneNumber: '3051234567',
profileName: 'Rick Sanchez',
2021-05-07 22:21:10 +00:00
});
2020-12-02 18:14:03 +00:00
type OverridePropsBase = {
2020-12-02 18:14:03 +00:00
hasLocalAudio?: boolean;
hasLocalVideo?: boolean;
2022-05-19 03:28:51 +00:00
localAudioLevel?: number;
viewMode?: CallViewMode;
};
2020-11-17 15:07:53 +00:00
type DirectCallOverrideProps = OverridePropsBase & {
2020-12-02 18:14:03 +00:00
callMode: CallMode.Direct;
callState?: CallState;
hasRemoteVideo?: boolean;
};
2020-12-02 18:14:03 +00:00
type GroupCallOverrideProps = OverridePropsBase & {
2020-12-02 18:14:03 +00:00
callMode: CallMode.Group;
connectionState?: GroupCallConnectionState;
peekedParticipants?: Array<ConversationType>;
2020-12-02 18:14:03 +00:00
remoteParticipants?: Array<GroupCallRemoteParticipantType>;
remoteAudioLevel?: number;
};
2020-12-02 18:14:03 +00:00
const createActiveDirectCallProp = (
overrideProps: DirectCallOverrideProps
) => ({
callMode: CallMode.Direct as CallMode.Direct,
conversation,
callState: overrideProps.callState ?? CallState.Accepted,
2020-12-02 18:14:03 +00:00
peekedParticipants: [] as [],
remoteParticipants: [
{
hasRemoteVideo: overrideProps.hasRemoteVideo ?? false,
presenting: false,
title: 'test',
2020-12-02 18:14:03 +00:00
},
] as [
{
hasRemoteVideo: boolean;
presenting: boolean;
title: string;
2020-12-02 18:14:03 +00:00
}
],
});
const createActiveGroupCallProp = (overrideProps: GroupCallOverrideProps) => ({
callMode: CallMode.Group as CallMode.Group,
connectionState:
overrideProps.connectionState || GroupCallConnectionState.Connected,
conversationsWithSafetyNumberChanges: [],
2020-12-02 18:14:03 +00:00
joinState: GroupCallJoinState.Joined,
maxDevices: 5,
deviceCount: (overrideProps.remoteParticipants || []).length,
groupMembers: overrideProps.remoteParticipants || [],
2020-12-02 18:14:03 +00:00
// Because remote participants are a superset, we can use them in place of peeked
// participants.
isConversationTooBigToRing: false,
2020-12-02 18:14:03 +00:00
peekedParticipants:
overrideProps.peekedParticipants || overrideProps.remoteParticipants || [],
remoteParticipants: overrideProps.remoteParticipants || [],
remoteAudioLevels: new Map<number, number>(
overrideProps.remoteParticipants?.map((_participant, index) => [
index,
overrideProps.remoteAudioLevel ?? 0,
])
),
2020-12-02 18:14:03 +00:00
});
const createActiveCallProp = (
overrideProps: DirectCallOverrideProps | GroupCallOverrideProps
) => {
const baseResult = {
joinedAt: Date.now(),
conversation,
hasLocalAudio: overrideProps.hasLocalAudio ?? false,
hasLocalVideo: overrideProps.hasLocalVideo ?? false,
localAudioLevel: overrideProps.localAudioLevel ?? 0,
viewMode: overrideProps.viewMode ?? CallViewMode.Grid,
outgoingRing: true,
2020-12-02 18:14:03 +00:00
pip: false,
settingsDialogOpen: false,
showParticipantsList: false,
2020-11-17 15:07:53 +00:00
};
2020-12-02 18:14:03 +00:00
switch (overrideProps.callMode) {
case CallMode.Direct:
return { ...baseResult, ...createActiveDirectCallProp(overrideProps) };
case CallMode.Group:
return { ...baseResult, ...createActiveGroupCallProp(overrideProps) };
default:
throw missingCaseError(overrideProps);
}
};
2020-11-17 15:07:53 +00:00
const createProps = (
2020-12-02 18:14:03 +00:00
overrideProps: DirectCallOverrideProps | GroupCallOverrideProps = {
callMode: CallMode.Direct as CallMode.Direct,
}
2020-11-17 15:07:53 +00:00
): PropsType => ({
2020-12-02 18:14:03 +00:00
activeCall: createActiveCallProp(overrideProps),
2021-01-08 18:58:28 +00:00
getGroupCallVideoFrameSource: fakeGetGroupCallVideoFrameSource,
getPresentingSources: action('get-presenting-sources'),
hangUpActiveCall: action('hang-up'),
2020-06-04 18:16:19 +00:00
i18n,
me: getDefaultConversation({
2021-05-28 16:15:17 +00:00
color: AvatarColors[1],
id: '6146087e-f7ef-457e-9a8d-47df1fdd6b25',
name: 'Morty Smith',
profileName: 'Morty Smith',
title: 'Morty Smith',
2023-08-16 20:54:39 +00:00
serviceId: generateAci(),
}),
openSystemPreferencesAction: action('open-system-preferences-action'),
setGroupCallVideoRequest: action('set-group-call-video-request'),
2020-06-04 18:16:19 +00:00
setLocalAudio: action('set-local-audio'),
2020-08-27 00:03:42 +00:00
setLocalPreview: action('set-local-preview'),
2020-06-04 18:16:19 +00:00
setLocalVideo: action('set-local-video'),
setPresenting: action('toggle-presenting'),
2020-08-27 00:03:42 +00:00
setRendererCanvas: action('set-renderer-canvas'),
stickyControls: false,
switchToPresentationView: action('switch-to-presentation-view'),
switchFromPresentationView: action('switch-from-presentation-view'),
2020-11-17 15:07:53 +00:00
toggleParticipants: action('toggle-participants'),
2020-10-01 00:43:05 +00:00
togglePip: action('toggle-pip'),
toggleScreenRecordingPermissionsDialog: action(
'toggle-screen-recording-permissions-dialog'
),
2020-08-27 00:03:42 +00:00
toggleSettings: action('toggle-settings'),
2021-01-08 22:57:54 +00:00
toggleSpeakerView: action('toggle-speaker-view'),
2020-10-08 01:25:33 +00:00
});
2022-06-07 00:48:02 +00:00
export default {
title: 'Components/CallScreen',
argTypes: {},
args: {},
} satisfies Meta<PropsType>;
2020-10-08 01:25:33 +00:00
2022-11-18 00:45:19 +00:00
export function Default(): JSX.Element {
2020-10-08 01:25:33 +00:00
return <CallScreen {...createProps()} />;
2022-11-18 00:45:19 +00:00
}
2020-10-08 01:25:33 +00:00
2022-11-18 00:45:19 +00:00
export function PreRing(): JSX.Element {
2020-11-17 15:07:53 +00:00
return (
<CallScreen
{...createProps({
2020-12-02 18:14:03 +00:00
callMode: CallMode.Direct,
2020-11-17 15:07:53 +00:00
callState: CallState.Prering,
})}
/>
);
2022-11-18 00:45:19 +00:00
}
2022-06-07 00:48:02 +00:00
export function Ringing(): JSX.Element {
2020-11-17 15:07:53 +00:00
return (
<CallScreen
{...createProps({
2020-12-02 18:14:03 +00:00
callMode: CallMode.Direct,
2020-11-17 15:07:53 +00:00
callState: CallState.Ringing,
})}
/>
);
}
2020-10-08 01:25:33 +00:00
export function Reconnecting(): JSX.Element {
2020-11-17 15:07:53 +00:00
return (
<CallScreen
{...createProps({
2020-12-02 18:14:03 +00:00
callMode: CallMode.Direct,
2020-11-17 15:07:53 +00:00
callState: CallState.Reconnecting,
})}
/>
);
}
2020-10-08 01:25:33 +00:00
export function Ended(): JSX.Element {
2020-11-17 15:07:53 +00:00
return (
<CallScreen
{...createProps({
2020-12-02 18:14:03 +00:00
callMode: CallMode.Direct,
2020-11-17 15:07:53 +00:00
callState: CallState.Ended,
})}
/>
);
}
2020-06-04 18:16:19 +00:00
2022-11-18 00:45:19 +00:00
export function HasLocalAudio(): JSX.Element {
2020-12-02 18:14:03 +00:00
return (
<CallScreen
{...createProps({
callMode: CallMode.Direct,
hasLocalAudio: true,
})}
/>
);
2022-11-18 00:45:19 +00:00
}
2020-06-04 18:16:19 +00:00
2022-11-18 00:45:19 +00:00
export function HasLocalVideo(): JSX.Element {
2020-12-02 18:14:03 +00:00
return (
<CallScreen
{...createProps({
callMode: CallMode.Direct,
hasLocalVideo: true,
})}
/>
);
2022-11-18 00:45:19 +00:00
}
2022-06-07 00:48:02 +00:00
2022-11-18 00:45:19 +00:00
export function HasRemoteVideo(): JSX.Element {
2020-12-02 18:14:03 +00:00
return (
<CallScreen
{...createProps({
callMode: CallMode.Direct,
hasRemoteVideo: true,
})}
/>
);
2022-11-18 00:45:19 +00:00
}
2020-11-17 15:07:53 +00:00
2022-11-18 00:45:19 +00:00
export function GroupCall1(): JSX.Element {
return (
<CallScreen
{...createProps({
callMode: CallMode.Group,
remoteParticipants: [
{
aci: generateAci(),
2022-11-18 00:45:19 +00:00
demuxId: 0,
hasRemoteAudio: true,
hasRemoteVideo: true,
presenting: false,
sharingScreen: false,
videoAspectRatio: 1.3,
...getDefaultConversation({
isBlocked: false,
2023-08-16 20:54:39 +00:00
serviceId: generateAci(),
2022-11-18 00:45:19 +00:00
title: 'Tyler',
}),
},
],
})}
/>
);
}
2022-06-07 00:48:02 +00:00
2021-01-08 18:58:28 +00:00
// We generate these upfront so that the list is stable when you move the slider.
const allRemoteParticipants = times(MAX_PARTICIPANTS).map(index => ({
aci: generateAci(),
2021-01-08 18:58:28 +00:00
demuxId: index,
hasRemoteAudio: index % 3 !== 0,
hasRemoteVideo: index % 4 !== 0,
presenting: false,
sharingScreen: false,
2021-01-08 18:58:28 +00:00
videoAspectRatio: 1.3,
2023-08-16 20:54:39 +00:00
...getDefaultConversationWithServiceId({
2021-01-08 18:58:28 +00:00
isBlocked: index === 10 || index === MAX_PARTICIPANTS - 1,
title: `Participant ${index + 1}`,
}),
}));
2022-11-18 00:45:19 +00:00
export function GroupCallMany(): JSX.Element {
2021-01-08 18:58:28 +00:00
return (
<CallScreen
{...createProps({
callMode: CallMode.Group,
remoteParticipants: allRemoteParticipants.slice(0, 40),
2021-01-08 18:58:28 +00:00
})}
/>
);
2022-11-18 00:45:19 +00:00
}
2022-06-07 00:48:02 +00:00
2022-11-18 00:45:19 +00:00
export function GroupCallReconnecting(): JSX.Element {
return (
<CallScreen
{...createProps({
callMode: CallMode.Group,
connectionState: GroupCallConnectionState.Reconnecting,
remoteParticipants: [
{
aci: generateAci(),
2022-11-18 00:45:19 +00:00
demuxId: 0,
hasRemoteAudio: true,
hasRemoteVideo: true,
presenting: false,
sharingScreen: false,
videoAspectRatio: 1.3,
...getDefaultConversation({
isBlocked: false,
title: 'Tyler',
2023-08-16 20:54:39 +00:00
serviceId: generateAci(),
2022-11-18 00:45:19 +00:00
}),
},
],
})}
/>
);
}
2022-06-07 00:48:02 +00:00
2022-11-18 00:45:19 +00:00
export function GroupCall0(): JSX.Element {
return (
<CallScreen
{...createProps({
callMode: CallMode.Group,
remoteParticipants: [],
})}
/>
);
}
2022-06-07 00:48:02 +00:00
2022-11-18 00:45:19 +00:00
export function GroupCallSomeoneIsSharingScreen(): JSX.Element {
return (
<CallScreen
{...createProps({
callMode: CallMode.Group,
remoteParticipants: allRemoteParticipants
.slice(0, 5)
.map((participant, index) => ({
...participant,
presenting: index === 1,
sharingScreen: index === 1,
})),
})}
/>
);
}
2022-11-18 00:45:19 +00:00
export function GroupCallSomeoneIsSharingScreenAndYoureReconnecting(): JSX.Element {
return (
<CallScreen
{...createProps({
callMode: CallMode.Group,
connectionState: GroupCallConnectionState.Reconnecting,
remoteParticipants: allRemoteParticipants
.slice(0, 5)
.map((participant, index) => ({
...participant,
presenting: index === 1,
sharingScreen: index === 1,
})),
})}
/>
2022-06-07 00:48:02 +00:00
);
2022-11-18 00:45:19 +00:00
}