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