simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 19 | import { Box, CircularProgress, Grid, IconButtonProps, Stack, Typography } from '@mui/material'; |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 20 | import { ComponentType, ReactNode, useContext, useMemo } from 'react'; |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 21 | import { useTranslation } from 'react-i18next'; |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 22 | |
| 23 | import { |
| 24 | CallingAnswerAudioButton, |
| 25 | CallingAnswerVideoButton, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 26 | CallingCancelButton, |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 27 | CallingRefuseButton, |
| 28 | } from '../components/CallButtons'; |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 29 | import ConversationAvatar from '../components/ConversationAvatar'; |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 30 | import { ConversationContext } from '../contexts/ConversationProvider'; |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 31 | |
| 32 | export type CallPendingProps = { |
| 33 | pending: PendingStatus; |
| 34 | caller?: CallerStatus; |
| 35 | medium?: CommunicationMedium; |
| 36 | }; |
| 37 | |
| 38 | type PendingStatus = 'caller' | 'receiver'; |
| 39 | type CallerStatus = 'calling' | 'connecting'; |
| 40 | type CommunicationMedium = 'audio' | 'video'; |
| 41 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 42 | export const CallPending = (props: CallPendingProps) => { |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 43 | const { conversation } = useContext(ConversationContext); |
| 44 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 45 | return ( |
| 46 | <Stack |
| 47 | direction="column" |
| 48 | justifyContent="center" |
| 49 | alignItems="center" |
| 50 | height="100%" |
| 51 | spacing={4} |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 52 | flexGrow={1} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 53 | 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 Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 84 | <ConversationAvatar |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 85 | alt="contact profile picture" |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 86 | displayName={conversation.getDisplayNameNoFallback()} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 87 | style={{ |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 88 | width: '100%', |
| 89 | height: '100%', |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 90 | }} |
| 91 | /> |
| 92 | </Box> |
| 93 | </Box> |
| 94 | {props.pending === 'caller' ? ( |
| 95 | <CallPendingCallerInterface {...props} /> |
| 96 | ) : ( |
| 97 | <CallPendingReceiverInterface {...props} /> |
| 98 | )} |
| 99 | </Stack> |
| 100 | ); |
| 101 | }; |
| 102 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 103 | const CallPendingDetails = ({ |
| 104 | title, |
| 105 | buttons, |
| 106 | }: { |
| 107 | title: ReactNode; |
| 108 | buttons: { |
| 109 | ButtonComponent: ComponentType<IconButtonProps>; |
| 110 | title: ReactNode; |
| 111 | }[]; |
| 112 | }) => { |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 113 | return ( |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 114 | <> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 115 | <Typography variant="h1" color="white"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 116 | {title} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 117 | </Typography> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 118 | <Box width="50%"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 119 | <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> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 131 | </Box> |
| 132 | </> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 133 | ); |
| 134 | }; |
| 135 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 136 | export const CallPendingCallerInterface = ({ caller }: CallPendingProps) => { |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 137 | const { t } = useTranslation(); |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 138 | const { conversation } = useContext(ConversationContext); |
| 139 | const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 140 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 141 | return ( |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 142 | <CallPendingDetails |
| 143 | title={ |
| 144 | caller === 'calling' |
| 145 | ? t('calling', { |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 146 | member0: memberName, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 147 | }) |
| 148 | : t('connecting') |
| 149 | } |
| 150 | buttons={[ |
| 151 | { |
| 152 | ButtonComponent: CallingCancelButton, |
| 153 | title: t('end_call'), |
| 154 | }, |
| 155 | ]} |
| 156 | /> |
| 157 | ); |
| 158 | }; |
| 159 | |
| 160 | export const CallPendingReceiverInterface = ({ medium, caller }: CallPendingProps) => { |
| 161 | const { t } = useTranslation(); |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 162 | const { conversation } = useContext(ConversationContext); |
| 163 | const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 164 | |
| 165 | return ( |
| 166 | <CallPendingDetails |
| 167 | title={ |
| 168 | caller === 'connecting' |
| 169 | ? t('connecting') |
| 170 | : t('incoming_call', { |
| 171 | context: medium, |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 172 | member0: memberName, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 173 | }) |
| 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 | /> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 190 | ); |
| 191 | }; |