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

342 lines
8.5 KiB
TypeScript
Raw Normal View History

2021-01-08 18:58:28 +00:00
// Copyright 2020-2021 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';
import { v4 as generateUuid } from 'uuid';
2020-09-12 00:46:52 +00:00
import { storiesOf } from '@storybook/react';
2021-01-08 18:58:28 +00:00
import { boolean, select, number } from '@storybook/addon-knobs';
2020-09-12 00:46:52 +00:00
import { action } from '@storybook/addon-actions';
import {
CallMode,
CallState,
GroupCallConnectionState,
GroupCallJoinState,
GroupCallRemoteParticipantType,
} from '../types/Calling';
import { ConversationType } from '../state/ducks/conversations';
2021-05-28 16:15:17 +00:00
import { AvatarColors } from '../types/Colors';
2020-10-08 01:25:33 +00:00
import { CallScreen, PropsType } from './CallScreen';
2020-06-04 18:16:19 +00:00
import { setup as setupI18n } from '../../js/modules/i18n';
2020-12-02 18:14:03 +00:00
import { missingCaseError } from '../util/missingCaseError';
import { getDefaultConversation } 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';
2021-01-08 18:58:28 +00:00
const MAX_PARTICIPANTS = 32;
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;
2021-01-08 22:57:54 +00:00
isInSpeakerView?: boolean;
};
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>;
};
2020-12-02 18:14:03 +00:00
const createActiveDirectCallProp = (
overrideProps: DirectCallOverrideProps
) => ({
callMode: CallMode.Direct as CallMode.Direct,
conversation,
callState: select(
'callState',
CallState,
overrideProps.callState || CallState.Accepted
),
peekedParticipants: [] as [],
remoteParticipants: [
{
hasRemoteVideo: boolean(
'hasRemoteVideo',
Boolean(overrideProps.hasRemoteVideo)
),
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,
// Because remote participants are a superset, we can use them in place of peeked
// participants.
peekedParticipants:
overrideProps.peekedParticipants || overrideProps.remoteParticipants || [],
remoteParticipants: overrideProps.remoteParticipants || [],
});
const createActiveCallProp = (
overrideProps: DirectCallOverrideProps | GroupCallOverrideProps
) => {
const baseResult = {
joinedAt: Date.now(),
conversation,
hasLocalAudio: boolean(
'hasLocalAudio',
overrideProps.hasLocalAudio || false
2020-11-13 19:57:55 +00:00
),
2020-12-02 18:14:03 +00:00
hasLocalVideo: boolean(
'hasLocalVideo',
overrideProps.hasLocalVideo || false
2020-11-13 19:57:55 +00:00
),
2021-01-08 22:57:54 +00:00
isInSpeakerView: boolean(
'isInSpeakerView',
overrideProps.isInSpeakerView || false
),
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'),
2020-06-04 18:16:19 +00:00
hangUp: action('hang-up'),
i18n,
me: {
2021-05-28 16:15:17 +00:00
color: AvatarColors[1],
name: 'Morty Smith',
profileName: 'Morty Smith',
title: 'Morty Smith',
},
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'),
2020-11-17 15:07:53 +00:00
stickyControls: boolean('stickyControls', false),
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
});
const story = storiesOf('Components/CallScreen', module);
story.add('Default', () => {
return <CallScreen {...createProps()} />;
});
story.add('Pre-Ring', () => {
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,
})}
/>
);
2020-10-08 01:25:33 +00:00
});
story.add('Ringing', () => {
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
});
story.add('Reconnecting', () => {
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
});
story.add('Ended', () => {
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-10-08 01:25:33 +00:00
});
2020-06-04 18:16:19 +00:00
2020-10-08 01:25:33 +00:00
story.add('hasLocalAudio', () => {
2020-12-02 18:14:03 +00:00
return (
<CallScreen
{...createProps({
callMode: CallMode.Direct,
hasLocalAudio: true,
})}
/>
);
2020-10-08 01:25:33 +00:00
});
2020-06-04 18:16:19 +00:00
2020-10-08 01:25:33 +00:00
story.add('hasLocalVideo', () => {
2020-12-02 18:14:03 +00:00
return (
<CallScreen
{...createProps({
callMode: CallMode.Direct,
hasLocalVideo: true,
})}
/>
);
2020-10-08 01:25:33 +00:00
});
2020-06-04 18:16:19 +00:00
2020-10-08 01:25:33 +00:00
story.add('hasRemoteVideo', () => {
2020-12-02 18:14:03 +00:00
return (
<CallScreen
{...createProps({
callMode: CallMode.Direct,
hasRemoteVideo: true,
})}
/>
);
2020-10-08 01:25:33 +00:00
});
2020-11-17 15:07:53 +00:00
story.add('Group call - 1', () => (
<CallScreen
{...createProps({
2020-12-02 18:14:03 +00:00
callMode: CallMode.Group,
remoteParticipants: [
2020-11-17 15:07:53 +00:00
{
demuxId: 0,
hasRemoteAudio: true,
hasRemoteVideo: true,
presenting: false,
sharingScreen: false,
2020-11-17 15:07:53 +00:00
videoAspectRatio: 1.3,
...getDefaultConversation({
isBlocked: false,
uuid: '72fa60e5-25fb-472d-8a56-e56867c57dda',
title: 'Tyler',
}),
2020-11-17 15:07:53 +00:00
},
],
2020-11-17 15:07:53 +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 => ({
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,
...getDefaultConversation({
isBlocked: index === 10 || index === MAX_PARTICIPANTS - 1,
title: `Participant ${index + 1}`,
uuid: generateUuid(),
}),
}));
story.add('Group call - Many', () => {
return (
<CallScreen
{...createProps({
callMode: CallMode.Group,
remoteParticipants: allRemoteParticipants.slice(
0,
number('Participant count', 3, {
range: true,
min: 0,
max: MAX_PARTICIPANTS,
step: 1,
})
),
})}
/>
);
});
story.add('Group call - reconnecting', () => (
<CallScreen
{...createProps({
2020-12-02 18:14:03 +00:00
callMode: CallMode.Group,
connectionState: GroupCallConnectionState.Reconnecting,
remoteParticipants: [
{
demuxId: 0,
hasRemoteAudio: true,
hasRemoteVideo: true,
presenting: false,
sharingScreen: false,
videoAspectRatio: 1.3,
...getDefaultConversation({
isBlocked: false,
title: 'Tyler',
uuid: '33871c64-0c22-45ce-8aa4-0ec237ac4a31',
}),
},
],
})}
/>
));
story.add('Group call - 0', () => (
<CallScreen
{...createProps({
callMode: CallMode.Group,
remoteParticipants: [],
})}
/>
));