From 22b4afbb34829e5553441cdddabe90c25c29d8de Mon Sep 17 00:00:00 2001 From: harshithpabbati Date: Thu, 30 Dec 2021 14:00:52 +0530 Subject: [PATCH] Update the grid layout whenever someone screenshares (uses participant bar) --- custom/fitness-demo/components/App/App.js | 59 ++++++++++++--- .../fitness-demo/components/Call/VideoGrid.js | 71 ++++++++++++++++--- custom/fitness-demo/pages/_app.js | 2 +- .../ParticipantBar/ParticipantBar.js | 2 +- 4 files changed, 113 insertions(+), 21 deletions(-) diff --git a/custom/fitness-demo/components/App/App.js b/custom/fitness-demo/components/App/App.js index a0b0d71..4fd540d 100644 --- a/custom/fitness-demo/components/App/App.js +++ b/custom/fitness-demo/components/App/App.js @@ -1,13 +1,54 @@ -import React from 'react'; +import React, { useMemo } from 'react'; +import ExpiryTimer from '@custom/shared/components/ExpiryTimer'; +import { useCallState } from '@custom/shared/contexts/CallProvider'; +import { useCallUI } from '@custom/shared/hooks/useCallUI'; +import PropTypes from 'prop-types'; -import App from '@custom/basic-call/components/App'; import { ChatProvider } from '../../contexts/ChatProvider'; +import Room from '../Call/Room'; +import { Asides } from './Asides'; +import { Modals } from './Modals'; -// Extend our basic call app component with the chat context -export const CustomApp = () => ( - - - -); +export const App = ({ customComponentForState }) => { + const { roomExp, state } = useCallState(); -export default CustomApp; + const componentForState = useCallUI({ + state, + room: , + ...customComponentForState, + }); + + // Memoize children to avoid unnecassary renders from HOC + return useMemo( + () => ( + <> + + {roomExp && } +
+ {componentForState()} + + + +
+
+ + ), + [componentForState, roomExp] + ); +}; + +App.propTypes = { + customComponentForState: PropTypes.any, +}; + +export default App; \ No newline at end of file diff --git a/custom/fitness-demo/components/Call/VideoGrid.js b/custom/fitness-demo/components/Call/VideoGrid.js index 7a3fddc..1e3f60c 100644 --- a/custom/fitness-demo/components/Call/VideoGrid.js +++ b/custom/fitness-demo/components/Call/VideoGrid.js @@ -1,9 +1,13 @@ import React, { useState, useMemo, useEffect, useRef } from 'react'; +import ParticipantBar from '@custom/shared/components/ParticipantBar'; import Tile from '@custom/shared/components/Tile'; import { DEFAULT_ASPECT_RATIO } from '@custom/shared/constants'; +import { useCallState } from '@custom/shared/contexts/CallProvider'; import { useParticipants } from '@custom/shared/contexts/ParticipantsProvider'; import { useDeepCompareMemo } from 'use-deep-compare'; +const SIDEBAR_WIDTH = 186; + /** * Basic unpaginated video tile grid, scaled by aspect ratio * @@ -18,7 +22,8 @@ import { useDeepCompareMemo } from 'use-deep-compare'; export const VideoGrid = React.memo( () => { const containerRef = useRef(); - const { allParticipants } = useParticipants(); + const { allParticipants, participants, screens, localParticipant } = useParticipants(); + const { showLocalVideo } = useCallState(); const [dimensions, setDimensions] = useState({ width: 1, height: 1, @@ -45,10 +50,12 @@ export const VideoGrid = React.memo( }; }, []); + const hasScreenshares = useMemo(() => screens.length > 0, [screens]); + // Basic brute-force packing algo const layout = useMemo(() => { const aspectRatio = DEFAULT_ASPECT_RATIO; - const tileCount = allParticipants.length || 0; + const tileCount = hasScreenshares ? screens.length : participants.length || 0; const w = dimensions.width; const h = dimensions.height; @@ -87,29 +94,73 @@ export const VideoGrid = React.memo( } return bestLayout; - }, [dimensions, allParticipants]); + }, [hasScreenshares, screens.length, participants.length, dimensions.width, dimensions.height]); + + const otherParticipants = useMemo( + () => participants.filter(({ isLocal }) => !isLocal), + [participants] + ); + + const fixedItems = useMemo(() => { + const items = []; + if (showLocalVideo) { + items.push(localParticipant); + } + if (hasScreenshares && otherParticipants.length > 0) { + items.push(otherParticipants[0]); + } + return items; + }, [hasScreenshares, localParticipant, otherParticipants, showLocalVideo]); + + const otherItems = useMemo(() => { + if (otherParticipants.length > 1) { + return otherParticipants.slice(hasScreenshares ? 1 : 0); + } + return []; + }, [hasScreenshares, otherParticipants]); // Memoize our tile list to avoid unnecassary re-renders const tiles = useDeepCompareMemo( () => - allParticipants.map((p) => ( + participants.map((p) => ( )), - [layout, allParticipants] + [layout, participants] ); - if (!allParticipants.length) { - return null; - } + const screenShareTiles = useDeepCompareMemo( + () => + screens.map((p) => ( + + )), + [layout, screens] + ); + + if (!participants.length) return null; return (
-
{tiles}
+
+ {screenShareTiles} + {!hasScreenshares && tiles} +
+ {hasScreenshares && ( + + )}