import React, { useCallback, useEffect, useMemo, useState } from 'react'; import { useCallState } from '../../contexts/CallProvider'; import { useWaitingRoom } from '../../contexts/WaitingRoomProvider'; import { ReactComponent as IconWaiting } from '../../icons/add-person-lg.svg'; import Button from '../Button'; import { Card, CardBody, CardFooter } from '../Card'; export const WaitingRoomNotification = () => { const { callObject } = useCallState(); const { denyAccess, grantAccess, showModal, setShowModal, waitingParticipants, multipleWaiting, } = useWaitingRoom(); const [showNotification, setShowNotification] = useState(false); /** * Click handlers passed to render functions */ const handleViewAllClick = useCallback(() => { setShowModal(true); setShowNotification(false); }, [setShowModal]); const handleAllowClick = useCallback(() => { grantAccess(waitingParticipants[0].id); }, [grantAccess, waitingParticipants]); const handleDenyClick = useCallback(() => { denyAccess(multipleWaiting ? 'all' : waitingParticipants[0].id); }, [denyAccess, waitingParticipants, multipleWaiting]); /** * Render the full participant waiting list */ const showMultipleParticipants = useMemo(() => { return (

{waitingParticipants.length} people would like to join the call

); }, [waitingParticipants, handleDenyClick, handleViewAllClick]); /** * Render the single waiting participant */ const showSingleParticipant = useMemo(() => { return (

{waitingParticipants[0]?.name} would like to join the call

); }, [waitingParticipants, handleAllowClick, handleDenyClick]); /** * Show notification when waiting participants change. */ useEffect(() => { if (showModal) return false; const handleWaitingParticipantAdded = () => { setShowNotification( Object.keys(callObject.waitingParticipants()).length > 0 ); }; callObject.on('waiting-participant-added', handleWaitingParticipantAdded); return () => { callObject.off( 'waiting-participant-added', handleWaitingParticipantAdded ); }; }, [callObject, showModal]); /** * Hide notification when people panel is opened. */ useEffect(() => { if (showModal) setShowNotification(false); }, [showModal]); if (!showNotification || waitingParticipants.length === 0) return null; return ( {multipleWaiting ? showMultipleParticipants : showSingleParticipant} ); }; export default WaitingRoomNotification;