Skip to content

Commit caded6e

Browse files
authored
Notifications (#25)
* Add notification toasts * Use theme for toasts
1 parent b79a70e commit caded6e

File tree

6 files changed

+133
-1
lines changed

6 files changed

+133
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"rc-slider": "^10.6.2",
7777
"react": "^18.2.0",
7878
"react-dom": "^18.2.0",
79+
"react-toastify": "^10.0.5",
7980
"three": "^0.162.0",
8081
"ts-xor": "^1.3.0",
8182
"unique-names-generator": "^4.7.1",

src/Notifications.tsx

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
};

src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,8 @@ export const SCROLL_BAR_WIDTH = 6;
1212
export const SIDEPANE_PEER_LIST_TILE = 128;
1313
export const SIDEPANE_PEER_LIST_PADDING = 3;
1414
export const SIDEPANE_PEER_LIST_MARGIN = 13;
15+
16+
export const ROLES = {
17+
host: 'host',
18+
presenter: 'presenter'
19+
};

src/utils/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Thanks JupyterCAD
2+
export function isLightTheme(): boolean {
3+
return document.body.getAttribute('data-jp-theme-light') === 'true';
4+
}

src/widgets/RootDisplay.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import { IStateDB } from '@jupyterlab/statedb';
44
import { ReactWidget } from '@jupyterlab/ui-components';
55
import { ISignal } from '@lumino/signaling';
66
import React, { useEffect, useRef } from 'react';
7+
import { Bounce, ToastContainer } from 'react-toastify';
8+
import 'react-toastify/dist/ReactToastify.css';
9+
import { Notifications } from '../Notifications';
710
import { MainDisplay } from '../components/MainDisplay';
811
import { TypedHMSRoomProvider, hmsActions } from '../hms';
912
import { IModelRegistry, IModelRegistryData } from '../registry';
13+
import { isLightTheme } from '../utils/utils';
1014

1115
interface IRootDisplayProps {
1216
node: HTMLElement;
@@ -25,6 +29,7 @@ const RootDisplay = ({
2529
themeChangedSignal
2630
}: IRootDisplayProps) => {
2731
const rootRef = useRef(null);
32+
const isLight = isLightTheme();
2833

2934
useEffect(() => {
3035
hmsActions.setAppData('themeChanged', themeChangedSignal);
@@ -42,7 +47,24 @@ const RootDisplay = ({
4247

4348
return (
4449
<div ref={rootRef} className="jlab-gather-root">
45-
<MainDisplay state={state} />
50+
<>
51+
<MainDisplay state={state} />
52+
<Notifications />
53+
<ToastContainer
54+
position="bottom-right"
55+
autoClose={5000}
56+
hideProgressBar={true}
57+
stacked
58+
newestOnTop={false}
59+
closeOnClick
60+
rtl={false}
61+
pauseOnFocusLoss
62+
draggable
63+
pauseOnHover
64+
theme={isLight ? 'light' : 'dark'}
65+
transition={Bounce}
66+
/>
67+
</>
4668
</div>
4769
);
4870
};

yarn.lock

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4996,6 +4996,13 @@ __metadata:
49964996
languageName: node
49974997
linkType: hard
49984998

4999+
"clsx@npm:^2.1.0":
5000+
version: 2.1.1
5001+
resolution: "clsx@npm:2.1.1"
5002+
checksum: acd3e1ab9d8a433ecb3cc2f6a05ab95fe50b4a3cfc5ba47abb6cbf3754585fcb87b84e90c822a1f256c4198e3b41c7f6c391577ffc8678ad587fc0976b24fd57
5003+
languageName: node
5004+
linkType: hard
5005+
49995006
"co@npm:^4.6.0":
50005007
version: 4.6.0
50015008
resolution: "co@npm:4.6.0"
@@ -8044,6 +8051,7 @@ __metadata:
80448051
rc-slider: ^10.6.2
80458052
react: ^18.2.0
80468053
react-dom: ^18.2.0
8054+
react-toastify: ^10.0.5
80478055
rimraf: ^5.0.1
80488056
source-map-loader: ^1.0.2
80498057
style-loader: ^3.3.1
@@ -9570,6 +9578,18 @@ __metadata:
95709578
languageName: node
95719579
linkType: hard
95729580

9581+
"react-toastify@npm:^10.0.5":
9582+
version: 10.0.5
9583+
resolution: "react-toastify@npm:10.0.5"
9584+
dependencies:
9585+
clsx: ^2.1.0
9586+
peerDependencies:
9587+
react: ">=18"
9588+
react-dom: ">=18"
9589+
checksum: 982d33918c63e99464623a1b3b023ab5fe18fe341143cb0cb04287e37fd0611c560d9ef451e151ac8ade15a1cda09df09d4c442b3db582965d71aaf42d49b230
9590+
languageName: node
9591+
linkType: hard
9592+
95739593
"react@npm:>=17.0.0 <19.0.0, react@npm:^18.2.0":
95749594
version: 18.2.0
95759595
resolution: "react@npm:18.2.0"

0 commit comments

Comments
 (0)