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