|
| 1 | +import { |
| 2 | + HMSNotificationTypes, |
| 3 | + selectIsConnectedToRoom, |
| 4 | + useHMSNotifications, |
| 5 | + useHMSStore |
| 6 | +} from '@100mslive/react-sdk'; |
| 7 | +import { faHand } from '@fortawesome/free-regular-svg-icons'; |
| 8 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 9 | +import React, { useEffect } from 'react'; |
| 10 | +import { toast } from 'react-toastify'; |
| 11 | +import { ROLES } from './constants'; |
| 12 | + |
| 13 | +export const Notifications = () => { |
| 14 | + const isConnected = useHMSStore(selectIsConnectedToRoom); |
| 15 | + const notification = useHMSNotifications(); |
| 16 | + |
| 17 | + let peer; |
| 18 | + let track; |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + console.log('isInPreview', isConnected); |
| 22 | + }, [isConnected]); |
| 23 | + |
| 24 | + useEffect(() => { |
| 25 | + if (!notification) { |
| 26 | + return; |
| 27 | + } |
| 28 | + switch (notification.type) { |
| 29 | + case HMSNotificationTypes.HAND_RAISE_CHANGED: |
| 30 | + peer = notification.data; |
| 31 | + if (peer && !peer.isLocal && peer.isHandRaised) { |
| 32 | + toast(`${peer.name} would like to speak`, { |
| 33 | + icon: ( |
| 34 | + <FontAwesomeIcon |
| 35 | + icon={faHand} |
| 36 | + style={{ fontSize: '1.5rem', color: '#f37726' }} |
| 37 | + /> |
| 38 | + ) |
| 39 | + }); |
| 40 | + } |
| 41 | + break; |
| 42 | + case HMSNotificationTypes.PEER_JOINED: |
| 43 | + if (isConnected) { |
| 44 | + toast.info(`${notification.data.name} has joined the room`); |
| 45 | + } |
| 46 | + break; |
| 47 | + case HMSNotificationTypes.PEER_LEFT: |
| 48 | + if (isConnected) { |
| 49 | + toast.info(`${notification.data.name} has left the room`); |
| 50 | + } |
| 51 | + break; |
| 52 | + case HMSNotificationTypes.ROLE_UPDATED: |
| 53 | + peer = notification.data; |
| 54 | + if (peer && !peer.isLocal && peer.roleName === ROLES.presenter) { |
| 55 | + toast.info(`${peer.name} has started presenting`); |
| 56 | + } |
| 57 | + break; |
| 58 | + case HMSNotificationTypes.TRACK_DEGRADED: |
| 59 | + track = notification.data; |
| 60 | + toast.warn(`${track.type} track degraded due to poor network`, { |
| 61 | + style: { textTransform: 'capitalize' } |
| 62 | + }); |
| 63 | + break; |
| 64 | + case HMSNotificationTypes.REMOVED_FROM_ROOM: |
| 65 | + console.log(`removed from room, reason - ${notification.data.reason}`); |
| 66 | + break; |
| 67 | + case HMSNotificationTypes.ROOM_ENDED: |
| 68 | + toast.info(`room ended, reason - ${notification.data.reason}`); |
| 69 | + break; |
| 70 | + case HMSNotificationTypes.ERROR: |
| 71 | + toast.error(`Something happened \n |
| 72 | + [${notification.data.code}]: ${notification.data}`); |
| 73 | + break; |
| 74 | + default: |
| 75 | + break; |
| 76 | + } |
| 77 | + }, [notification]); |
| 78 | + |
| 79 | + return null; |
| 80 | +}; |
0 commit comments