Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [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 | */ |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 18 | import { Box, Card, Grid, Stack, Typography } from '@mui/material'; |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 19 | import { |
| 20 | ComponentType, |
| 21 | Fragment, |
| 22 | ReactNode, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 23 | RefObject, |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 24 | useCallback, |
| 25 | useContext, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 26 | useEffect, |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 27 | useLayoutEffect, |
| 28 | useMemo, |
| 29 | useRef, |
| 30 | useState, |
| 31 | } from 'react'; |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 32 | import Draggable from 'react-draggable'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 33 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 34 | import { ExpandableButtonProps } from '../components/Button'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 35 | import { |
| 36 | CallingChatButton, |
| 37 | CallingEndButton, |
| 38 | CallingExtensionButton, |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 39 | CallingFullScreenButton, |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 40 | CallingGroupButton, |
| 41 | CallingMicButton, |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 42 | CallingMoreVerticalButton, |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 43 | CallingRecordButton, |
| 44 | CallingScreenShareButton, |
| 45 | CallingVideoCameraButton, |
| 46 | CallingVolumeButton, |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 47 | } from '../components/CallButtons'; |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 48 | import CallChatDrawer from '../components/CallChatDrawer'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 49 | import { CallContext, CallStatus } from '../contexts/CallProvider'; |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 50 | import { ConversationContext } from '../contexts/ConversationProvider'; |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 51 | import { WebRtcContext } from '../contexts/WebRtcProvider'; |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 52 | import { VideoElementWithSinkId } from '../utils/utils'; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 53 | import { CallPending } from './CallPending'; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 54 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 55 | export default () => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 56 | const { callStatus, isChatShown, isFullscreen } = useContext(CallContext); |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 57 | const callInterfaceRef = useRef<HTMLDivElement>(); |
| 58 | |
| 59 | useEffect(() => { |
| 60 | if (!callInterfaceRef.current) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (isFullscreen && document.fullscreenElement === null) { |
| 65 | callInterfaceRef.current.requestFullscreen(); |
| 66 | } else if (!isFullscreen && document.fullscreenEnabled !== null) { |
| 67 | document.exitFullscreen(); |
| 68 | } |
| 69 | }, [isFullscreen]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 70 | |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 71 | if (callStatus !== CallStatus.InCall) { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 72 | return <CallPending />; |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 73 | } |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 74 | |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 75 | return ( |
simon | 2a5cf14 | 2022-11-25 15:47:35 -0500 | [diff] [blame] | 76 | <Box ref={callInterfaceRef} flexGrow={1} display="flex"> |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 77 | <CallInterface /> |
| 78 | {isChatShown && <CallChatDrawer />} |
| 79 | </Box> |
| 80 | ); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 81 | }; |
| 82 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 83 | interface Props { |
| 84 | children?: ReactNode; |
| 85 | } |
| 86 | |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 87 | const CallInterface = () => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 88 | const { localStream, remoteStreams } = useContext(WebRtcContext); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 89 | const { |
| 90 | isVideoOn, |
| 91 | currentMediaDeviceIds: { |
| 92 | audiooutput: { id: audioOutDeviceId }, |
| 93 | }, |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 94 | } = useContext(CallContext); |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 95 | const localVideoRef = useRef<VideoElementWithSinkId | null>(null); |
| 96 | const remoteVideoRef = useRef<VideoElementWithSinkId | null>(null); |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 97 | const gridItemRef = useRef<HTMLDivElement | null>(null); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 98 | |
| 99 | useEffect(() => { |
| 100 | if (localStream && localVideoRef.current) { |
| 101 | localVideoRef.current.srcObject = localStream; |
| 102 | } |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 103 | }, [localStream, localVideoRef]); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 104 | |
| 105 | useEffect(() => { |
simon | 9076a9a | 2022-11-29 17:13:01 -0500 | [diff] [blame] | 106 | // TODO: For now, `remoteStream` is the first remote stream in the array. |
| 107 | // There should only be one in the array, but we should make sure this is right. |
| 108 | const remoteStream = remoteStreams?.at(0); |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 109 | if (remoteStream && remoteVideoRef.current) { |
| 110 | remoteVideoRef.current.srcObject = remoteStream; |
| 111 | } |
simon | 492e840 | 2022-11-29 16:48:37 -0500 | [diff] [blame] | 112 | }, [remoteStreams, remoteVideoRef]); |
| 113 | |
| 114 | useEffect(() => { |
| 115 | if (!audioOutDeviceId) { |
| 116 | return; |
| 117 | } |
| 118 | |
| 119 | if (remoteVideoRef.current?.setSinkId) { |
| 120 | // This only work on chrome and other browsers that support `setSinkId` |
| 121 | // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility |
| 122 | remoteVideoRef.current.setSinkId(audioOutDeviceId); |
| 123 | } |
| 124 | }, [audioOutDeviceId, remoteVideoRef]); |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 125 | |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 126 | const hasSetSinkId = remoteVideoRef.current?.setSinkId != null; |
| 127 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 128 | return ( |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 129 | <Box display="flex" flexGrow={1}> |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 130 | <video |
| 131 | ref={remoteVideoRef} |
| 132 | autoPlay |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 133 | style={{ zIndex: -1, backgroundColor: 'black', position: 'absolute', height: '100%', width: '100%' }} |
simon | ce2c0c4 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 134 | /> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 135 | <Box flexGrow={1} margin={2} display="flex" flexDirection="column"> |
| 136 | {/* Guest video, takes the whole screen */} |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 137 | <CallInterfaceInformation /> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 138 | <Box flexGrow={1} marginY={2} position="relative"> |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 139 | <Draggable bounds="parent" nodeRef={localVideoRef ?? undefined}> |
| 140 | <video |
| 141 | ref={localVideoRef} |
| 142 | autoPlay |
Issam E. Maghni | 8961593 | 2022-11-29 19:05:28 +0000 | [diff] [blame] | 143 | muted |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 144 | style={{ |
| 145 | position: 'absolute', |
| 146 | right: 0, |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 147 | borderRadius: '12px', |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 148 | maxHeight: '50%', |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 149 | maxWidth: '50%', |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 150 | visibility: isVideoOn ? 'visible' : 'hidden', |
| 151 | }} |
| 152 | /> |
| 153 | </Draggable> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 154 | </Box> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 155 | <Grid container> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 156 | <Grid item xs /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 157 | <Grid item sx={{ display: 'flex', justifyContent: 'center' }}> |
| 158 | <div> |
| 159 | <CallInterfacePrimaryButtons /> |
| 160 | </div> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 161 | </Grid> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 162 | <Grid item xs sx={{ display: 'flex', justifyContent: 'flex-end' }} ref={gridItemRef}> |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 163 | <CallInterfaceSecondaryButtons showVolumeButton={hasSetSinkId} gridItemRef={gridItemRef} /> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 164 | </Grid> |
| 165 | </Grid> |
simon | 9f814a3 | 2022-11-22 21:40:53 -0500 | [diff] [blame] | 166 | </Box> |
simon | f9d78f2 | 2022-11-25 15:47:15 -0500 | [diff] [blame] | 167 | </Box> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 168 | ); |
simon | 1170c32 | 2022-10-31 14:51:31 -0400 | [diff] [blame] | 169 | }; |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 170 | |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 171 | const formatElapsedSeconds = (elapsedSeconds: number): string => { |
| 172 | const seconds = Math.floor(elapsedSeconds % 60); |
| 173 | elapsedSeconds = Math.floor(elapsedSeconds / 60); |
| 174 | const minutes = elapsedSeconds % 60; |
| 175 | elapsedSeconds = Math.floor(elapsedSeconds / 60); |
| 176 | const hours = elapsedSeconds % 24; |
| 177 | |
| 178 | const times: string[] = []; |
| 179 | if (hours > 0) { |
| 180 | times.push(hours.toString().padStart(2, '0')); |
| 181 | } |
| 182 | times.push(minutes.toString().padStart(2, '0')); |
| 183 | times.push(seconds.toString().padStart(2, '0')); |
| 184 | |
| 185 | return times.join(':'); |
| 186 | }; |
| 187 | |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 188 | const CallInterfaceInformation = () => { |
| 189 | const { callStartTime } = useContext(CallContext); |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 190 | const { conversation } = useContext(ConversationContext); |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 191 | const [elapsedTime, setElapsedTime] = useState(0); |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 192 | const memberName = useMemo(() => conversation.getFirstMember().contact.getRegisteredName(), [conversation]); |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 193 | |
| 194 | useEffect(() => { |
| 195 | if (callStartTime) { |
| 196 | const interval = setInterval(() => { |
Misha Krieger-Raynauld | d0cc3e3 | 2022-11-29 19:59:31 -0500 | [diff] [blame] | 197 | setElapsedTime((Date.now() - callStartTime) / 1000); |
simon | 8b496b0 | 2022-11-29 01:46:46 -0500 | [diff] [blame] | 198 | }, 1000); |
| 199 | return () => clearInterval(interval); |
| 200 | } |
| 201 | }, [callStartTime]); |
| 202 | |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 203 | const elapsedTimerString = formatElapsedSeconds(elapsedTime); |
| 204 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 205 | return ( |
| 206 | <Stack direction="row" justifyContent="space-between" alignItems="center"> |
| 207 | <Typography color="white" component="p"> |
Gabriel Rochon | 15a5fb2 | 2022-11-27 19:25:14 -0500 | [diff] [blame] | 208 | {memberName} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 209 | </Typography> |
| 210 | <Typography color="white" component="p"> |
Gabriel Rochon | e382a30 | 2022-11-23 12:37:04 -0500 | [diff] [blame] | 211 | {elapsedTimerString} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 212 | </Typography> |
| 213 | </Stack> |
| 214 | ); |
| 215 | }; |
| 216 | |
| 217 | const CallInterfacePrimaryButtons = () => { |
| 218 | return ( |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 219 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible' }}> |
| 220 | <Stack direction="row" justifyContent="center" alignItems="center"> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 221 | <CallingMicButton /> |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 222 | <CallingEndButton /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 223 | <CallingVideoCameraButton /> |
| 224 | </Stack> |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 225 | </Card> |
| 226 | ); |
| 227 | }; |
| 228 | |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 229 | const SECONDARY_BUTTONS = [ |
| 230 | CallingVolumeButton, |
| 231 | CallingGroupButton, |
| 232 | CallingChatButton, |
| 233 | CallingScreenShareButton, |
| 234 | CallingRecordButton, |
| 235 | CallingExtensionButton, |
| 236 | CallingFullScreenButton, |
| 237 | ]; |
| 238 | |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 239 | const CallInterfaceSecondaryButtons = ({ |
| 240 | gridItemRef, |
| 241 | showVolumeButton, |
| 242 | }: Props & { showVolumeButton: boolean; gridItemRef: RefObject<HTMLElement> }) => { |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 243 | const stackRef = useRef<HTMLElement>(null); |
| 244 | |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 245 | const [initialMeasurementDone, setInitialMeasurementDone] = useState(false); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 246 | const [hiddenStackCount, setHiddenStackCount] = useState(0); |
| 247 | const [hiddenMenuVisible, setHiddenMenuVisible] = useState(false); |
| 248 | |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 249 | // Audio out options are only available on Chrome and other browsers that support `setSinkId`. |
| 250 | // This removes the `CallingVolumeButton` if `setSinkId` is not defined. |
| 251 | // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/setSinkId#browser_compatibility |
| 252 | const secondaryButtons = useMemo(() => { |
| 253 | return showVolumeButton ? SECONDARY_BUTTONS : SECONDARY_BUTTONS.slice(1); |
| 254 | }, [showVolumeButton]); |
| 255 | |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 256 | const calculateStackCount = useCallback(() => { |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 257 | if (stackRef?.current && gridItemRef?.current) { |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 258 | const buttonWidth = stackRef.current.children[0].clientWidth; |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 259 | const availableSpace = gridItemRef.current.clientWidth; |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 260 | let availableButtons = Math.floor((availableSpace - 1) / buttonWidth); |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 261 | if (availableButtons < secondaryButtons.length) { |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 262 | availableButtons -= 1; // Leave room for CallingMoreVerticalButton |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 263 | } |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 264 | setHiddenStackCount(secondaryButtons.length - availableButtons); |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 265 | } |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 266 | }, [gridItemRef, secondaryButtons]); |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 267 | |
| 268 | useLayoutEffect(() => { |
| 269 | // Run once, at the beginning, for initial measurements |
| 270 | if (!initialMeasurementDone) { |
| 271 | calculateStackCount(); |
| 272 | setInitialMeasurementDone(true); |
| 273 | } |
| 274 | |
| 275 | const onResize = () => { |
| 276 | calculateStackCount(); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 277 | }; |
| 278 | window.addEventListener('resize', onResize); |
| 279 | return () => { |
| 280 | window.removeEventListener('resize', onResize); |
| 281 | }; |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 282 | }, [calculateStackCount, initialMeasurementDone]); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 283 | |
| 284 | const { displayedButtons, hiddenButtons } = useMemo(() => { |
| 285 | const displayedButtons: ComponentType<ExpandableButtonProps>[] = []; |
| 286 | const hiddenButtons: ComponentType<ExpandableButtonProps>[] = []; |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 287 | for (let i = 0; i < secondaryButtons.length; i++) { |
| 288 | const button = secondaryButtons[i]; |
| 289 | if (i < secondaryButtons.length - hiddenStackCount) { |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 290 | displayedButtons.push(button); |
| 291 | } else { |
| 292 | hiddenButtons.push(button); |
| 293 | } |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 294 | } |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 295 | |
| 296 | return { |
| 297 | displayedButtons, |
| 298 | hiddenButtons, |
| 299 | }; |
simon | 571240f | 2022-11-29 23:59:27 -0500 | [diff] [blame] | 300 | }, [hiddenStackCount, secondaryButtons]); |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 301 | |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 302 | return ( |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 303 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', height: '100%' }}> |
| 304 | <Stack direction="row" justifyContent="center" alignItems="center" height="100%" ref={stackRef}> |
| 305 | {initialMeasurementDone && |
| 306 | displayedButtons.map((SecondaryButton, i) => ( |
| 307 | <Fragment key={i}> |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 308 | <SecondaryButton /> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 309 | </Fragment> |
| 310 | ))} |
| 311 | {(!!hiddenButtons.length || !initialMeasurementDone) && ( |
simon | 9a8fe20 | 2022-11-15 18:25:49 -0500 | [diff] [blame] | 312 | <CallingMoreVerticalButton isVertical onClick={() => setHiddenMenuVisible(!hiddenMenuVisible)} /> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 313 | )} |
| 314 | </Stack> |
| 315 | |
| 316 | {!!hiddenButtons.length && hiddenMenuVisible && ( |
| 317 | <Box sx={{ position: 'absolute', right: 0, bottom: '50px' }}> |
| 318 | <Card sx={{ backgroundColor: '#00000088', overflow: 'visible', justifyContent: 'flex-end' }}> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 319 | <Stack |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 320 | direction="column" |
| 321 | justifyContent="flex-end" |
| 322 | alignItems="flex-end" |
| 323 | sx={{ bottom: 0, right: 0, height: '100%' }} |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 324 | > |
| 325 | {hiddenButtons.map((SecondaryButton, i) => ( |
| 326 | <Fragment key={i}> |
simon | 4e7445c | 2022-11-16 21:18:46 -0500 | [diff] [blame] | 327 | <SecondaryButton isVertical /> |
simon | 33c0618 | 2022-11-02 17:39:31 -0400 | [diff] [blame] | 328 | </Fragment> |
| 329 | ))} |
| 330 | </Stack> |
| 331 | </Card> |
Gabriel Rochon | 8321a0d | 2022-11-06 23:18:36 -0500 | [diff] [blame] | 332 | </Box> |
| 333 | )} |
Gabriel Rochon | e3ec0d2 | 2022-10-08 14:27:03 -0400 | [diff] [blame] | 334 | </Card> |
| 335 | ); |
| 336 | }; |