blob: 23cbd509511c32a73be59793a7289bb8c34fd7c6 [file] [log] [blame]
simon2d3b6532022-11-08 21:01:57 -05001/*
2 * Copyright (C) 2022 Savoir-faire Linux Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation; either version 3 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
13 *
14 * You should have received a copy of the GNU Affero General Public
15 * License along with this program. If not, see
16 * <https://www.gnu.org/licenses/>.
17 */
18
simonf929a362022-11-18 16:53:45 -050019import { Box, CircularProgress, Grid, IconButtonProps, Stack, Typography } from '@mui/material';
Gabriel Rochon61f82af2022-11-22 22:28:02 -050020import { ComponentType, ReactNode, useContext, useMemo } from 'react';
simon4e7445c2022-11-16 21:18:46 -050021import { useTranslation } from 'react-i18next';
simon2d3b6532022-11-08 21:01:57 -050022
23import {
24 CallingAnswerAudioButton,
25 CallingAnswerVideoButton,
simonf929a362022-11-18 16:53:45 -050026 CallingCancelButton,
simon2d3b6532022-11-08 21:01:57 -050027 CallingRefuseButton,
28} from '../components/CallButtons';
Gabriel Rochon15a5fb22022-11-27 19:25:14 -050029import ConversationAvatar from '../components/ConversationAvatar';
Gabriel Rochon61f82af2022-11-22 22:28:02 -050030import { ConversationContext } from '../contexts/ConversationProvider';
simon2d3b6532022-11-08 21:01:57 -050031
32export type CallPendingProps = {
33 pending: PendingStatus;
34 caller?: CallerStatus;
35 medium?: CommunicationMedium;
36};
37
38type PendingStatus = 'caller' | 'receiver';
39type CallerStatus = 'calling' | 'connecting';
40type CommunicationMedium = 'audio' | 'video';
41
simon2d3b6532022-11-08 21:01:57 -050042export const CallPending = (props: CallPendingProps) => {
Gabriel Rochon15a5fb22022-11-27 19:25:14 -050043 const { conversation } = useContext(ConversationContext);
44
simon2d3b6532022-11-08 21:01:57 -050045 return (
46 <Stack
47 direction="column"
48 justifyContent="center"
49 alignItems="center"
50 height="100%"
51 spacing={4}
simon9f814a32022-11-22 21:40:53 -050052 flexGrow={1}
simon2d3b6532022-11-08 21:01:57 -050053 sx={{
54 backgroundColor: 'black',
55 }}
56 >
57 <Box
58 sx={{
59 position: 'relative',
60 display: 'flex',
61 alignItems: 'center',
62 justifyContent: 'center',
63 width: '100%',
64 height: '30%',
65 }}
66 >
67 <Box
68 sx={{
69 aspectRatio: '1',
70 height: '100%',
71 position: 'absolute',
72 }}
73 >
74 <CircularProgress
75 disableShrink
76 thickness={1}
77 size="100%"
78 sx={{
79 position: 'absolute',
80 color: 'white',
81 zIndex: 1,
82 }}
83 />
Gabriel Rochon15a5fb22022-11-27 19:25:14 -050084 <ConversationAvatar
simonf929a362022-11-18 16:53:45 -050085 alt="contact profile picture"
Gabriel Rochon15a5fb22022-11-27 19:25:14 -050086 displayName={conversation.getDisplayNameNoFallback()}
simon2d3b6532022-11-08 21:01:57 -050087 style={{
simon2d3b6532022-11-08 21:01:57 -050088 width: '100%',
89 height: '100%',
simon2d3b6532022-11-08 21:01:57 -050090 }}
91 />
92 </Box>
93 </Box>
94 {props.pending === 'caller' ? (
95 <CallPendingCallerInterface {...props} />
96 ) : (
97 <CallPendingReceiverInterface {...props} />
98 )}
99 </Stack>
100 );
101};
102
simonf929a362022-11-18 16:53:45 -0500103const CallPendingDetails = ({
104 title,
105 buttons,
106}: {
107 title: ReactNode;
108 buttons: {
109 ButtonComponent: ComponentType<IconButtonProps>;
110 title: ReactNode;
111 }[];
112}) => {
simon2d3b6532022-11-08 21:01:57 -0500113 return (
simon4e7445c2022-11-16 21:18:46 -0500114 <>
simon2d3b6532022-11-08 21:01:57 -0500115 <Typography variant="h1" color="white">
simonf929a362022-11-18 16:53:45 -0500116 {title}
simon2d3b6532022-11-08 21:01:57 -0500117 </Typography>
simon4e7445c2022-11-16 21:18:46 -0500118 <Box width="50%">
simonf929a362022-11-18 16:53:45 -0500119 <Grid container justifyContent="center">
120 {buttons.map(({ ButtonComponent, title: buttonTitle }, i) => (
121 <Grid item key={i} xs={4}>
122 <Stack direction="column" alignItems="center" spacing={1} sx={{}}>
123 <ButtonComponent color="inherit" size="large" />
124 <Typography variant="body2" color="white" sx={{ opacity: 0.75 }}>
125 {buttonTitle}
126 </Typography>
127 </Stack>
128 </Grid>
129 ))}
130 </Grid>
simon4e7445c2022-11-16 21:18:46 -0500131 </Box>
132 </>
simon2d3b6532022-11-08 21:01:57 -0500133 );
134};
135
simonf929a362022-11-18 16:53:45 -0500136export const CallPendingCallerInterface = ({ caller }: CallPendingProps) => {
simon4e7445c2022-11-16 21:18:46 -0500137 const { t } = useTranslation();
Gabriel Rochon61f82af2022-11-22 22:28:02 -0500138 const { conversation } = useContext(ConversationContext);
139 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
simonf929a362022-11-18 16:53:45 -0500140
simon2d3b6532022-11-08 21:01:57 -0500141 return (
simonf929a362022-11-18 16:53:45 -0500142 <CallPendingDetails
143 title={
144 caller === 'calling'
145 ? t('calling', {
Gabriel Rochon61f82af2022-11-22 22:28:02 -0500146 member0: memberName,
simonf929a362022-11-18 16:53:45 -0500147 })
148 : t('connecting')
149 }
150 buttons={[
151 {
152 ButtonComponent: CallingCancelButton,
153 title: t('end_call'),
154 },
155 ]}
156 />
157 );
158};
159
160export const CallPendingReceiverInterface = ({ medium, caller }: CallPendingProps) => {
161 const { t } = useTranslation();
Gabriel Rochon61f82af2022-11-22 22:28:02 -0500162 const { conversation } = useContext(ConversationContext);
163 const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]);
simonf929a362022-11-18 16:53:45 -0500164
165 return (
166 <CallPendingDetails
167 title={
168 caller === 'connecting'
169 ? t('connecting')
170 : t('incoming_call', {
171 context: medium,
Gabriel Rochon61f82af2022-11-22 22:28:02 -0500172 member0: memberName,
simonf929a362022-11-18 16:53:45 -0500173 })
174 }
175 buttons={[
176 {
177 ButtonComponent: CallingRefuseButton,
178 title: t('refuse_call'),
179 },
180 {
181 ButtonComponent: CallingAnswerAudioButton,
182 title: t('accept_call_audio'),
183 },
184 {
185 ButtonComponent: CallingAnswerVideoButton,
186 title: t('accept_call_video'),
187 },
188 ]}
189 />
simon2d3b6532022-11-08 21:01:57 -0500190 );
191};