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 | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 29 | import { ConversationContext } from '../contexts/ConversationProvider'; |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 30 | |
| 31 | export type CallPendingProps = { |
| 32 | pending: PendingStatus; |
| 33 | caller?: CallerStatus; |
| 34 | medium?: CommunicationMedium; |
| 35 | }; |
| 36 | |
| 37 | type PendingStatus = 'caller' | 'receiver'; |
| 38 | type CallerStatus = 'calling' | 'connecting'; |
| 39 | type CommunicationMedium = 'audio' | 'video'; |
| 40 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 41 | export const CallPending = (props: CallPendingProps) => { |
| 42 | return ( |
| 43 | <Stack |
| 44 | direction="column" |
| 45 | justifyContent="center" |
| 46 | alignItems="center" |
| 47 | height="100%" |
| 48 | spacing={4} |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 49 | flexGrow={1} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 50 | sx={{ |
| 51 | backgroundColor: 'black', |
| 52 | }} |
| 53 | > |
| 54 | <Box |
| 55 | sx={{ |
| 56 | position: 'relative', |
| 57 | display: 'flex', |
| 58 | alignItems: 'center', |
| 59 | justifyContent: 'center', |
| 60 | width: '100%', |
| 61 | height: '30%', |
| 62 | }} |
| 63 | > |
| 64 | <Box |
| 65 | sx={{ |
| 66 | aspectRatio: '1', |
| 67 | height: '100%', |
| 68 | position: 'absolute', |
| 69 | }} |
| 70 | > |
| 71 | <CircularProgress |
| 72 | disableShrink |
| 73 | thickness={1} |
| 74 | size="100%" |
| 75 | sx={{ |
| 76 | position: 'absolute', |
| 77 | color: 'white', |
| 78 | zIndex: 1, |
| 79 | }} |
| 80 | /> |
| 81 | <img |
| 82 | // TODO: Insert incoming caller icon here |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 83 | alt="contact profile picture" |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 84 | style={{ |
| 85 | position: 'absolute', |
| 86 | objectFit: 'cover', |
| 87 | width: '100%', |
| 88 | height: '100%', |
| 89 | maxWidth: '100%', |
| 90 | borderRadius: '50%', |
| 91 | aspectRatio: '1', |
| 92 | }} |
| 93 | /> |
| 94 | </Box> |
| 95 | </Box> |
| 96 | {props.pending === 'caller' ? ( |
| 97 | <CallPendingCallerInterface {...props} /> |
| 98 | ) : ( |
| 99 | <CallPendingReceiverInterface {...props} /> |
| 100 | )} |
| 101 | </Stack> |
| 102 | ); |
| 103 | }; |
| 104 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 105 | const CallPendingDetails = ({ |
| 106 | title, |
| 107 | buttons, |
| 108 | }: { |
| 109 | title: ReactNode; |
| 110 | buttons: { |
| 111 | ButtonComponent: ComponentType<IconButtonProps>; |
| 112 | title: ReactNode; |
| 113 | }[]; |
| 114 | }) => { |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 115 | return ( |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 116 | <> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 117 | <Typography variant="h1" color="white"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 118 | {title} |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 119 | </Typography> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 120 | <Box width="50%"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 121 | <Grid container justifyContent="center"> |
| 122 | {buttons.map(({ ButtonComponent, title: buttonTitle }, i) => ( |
| 123 | <Grid item key={i} xs={4}> |
| 124 | <Stack direction="column" alignItems="center" spacing={1} sx={{}}> |
| 125 | <ButtonComponent color="inherit" size="large" /> |
| 126 | <Typography variant="body2" color="white" sx={{ opacity: 0.75 }}> |
| 127 | {buttonTitle} |
| 128 | </Typography> |
| 129 | </Stack> |
| 130 | </Grid> |
| 131 | ))} |
| 132 | </Grid> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 133 | </Box> |
| 134 | </> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 135 | ); |
| 136 | }; |
| 137 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 138 | export const CallPendingCallerInterface = ({ caller }: CallPendingProps) => { |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 139 | const { t } = useTranslation(); |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 140 | const { conversation } = useContext(ConversationContext); |
| 141 | const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 142 | |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 143 | return ( |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 144 | <CallPendingDetails |
| 145 | title={ |
| 146 | caller === 'calling' |
| 147 | ? t('calling', { |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 148 | member0: memberName, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 149 | }) |
| 150 | : t('connecting') |
| 151 | } |
| 152 | buttons={[ |
| 153 | { |
| 154 | ButtonComponent: CallingCancelButton, |
| 155 | title: t('end_call'), |
| 156 | }, |
| 157 | ]} |
| 158 | /> |
| 159 | ); |
| 160 | }; |
| 161 | |
| 162 | export const CallPendingReceiverInterface = ({ medium, caller }: CallPendingProps) => { |
| 163 | const { t } = useTranslation(); |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 164 | const { conversation } = useContext(ConversationContext); |
| 165 | const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 166 | |
| 167 | return ( |
| 168 | <CallPendingDetails |
| 169 | title={ |
| 170 | caller === 'connecting' |
| 171 | ? t('connecting') |
| 172 | : t('incoming_call', { |
| 173 | context: medium, |
Gabriel Rochon | 61f82af | 2022-11-22 22:28:02 -0500 | [diff] [blame] | 174 | member0: memberName, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 175 | }) |
| 176 | } |
| 177 | buttons={[ |
| 178 | { |
| 179 | ButtonComponent: CallingRefuseButton, |
| 180 | title: t('refuse_call'), |
| 181 | }, |
| 182 | { |
| 183 | ButtonComponent: CallingAnswerAudioButton, |
| 184 | title: t('accept_call_audio'), |
| 185 | }, |
| 186 | { |
| 187 | ButtonComponent: CallingAnswerVideoButton, |
| 188 | title: t('accept_call_video'), |
| 189 | }, |
| 190 | ]} |
| 191 | /> |
simon | 2d3b653 | 2022-11-08 21:01:57 -0500 | [diff] [blame] | 192 | ); |
| 193 | }; |