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