-
Notifications
You must be signed in to change notification settings - Fork 29
/
Chat.js
90 lines (80 loc) · 2.82 KB
/
Chat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { useState, useEffect } from 'react';
import { useLocalParticipant, useRoom } from '@daily-co/daily-react-hooks';
import './Chat.css';
import api from '../../api';
export default function Chat({ showChat }) {
const localParticipant = useLocalParticipant();
const { name: roomName } = useRoom();
const [loaded, setLoaded] = useState(false);
const launchCometChat = (room) => {
window.CometChatWidget.launch({
widgetID: process.env.REACT_APP_COMET_CHAT_WIDGET_ID,
target: '#cometchat',
roundedCorners: 'false',
height: '100%',
width: '100%',
defaultID: room, //default UID (user) or GUID (group) to show,
defaultType: 'group', //user or group
});
setLoaded(true);
};
const createCometChatGroup = () => {
const GUID = roomName;
// make GET request to CometChat's REST API to see if group exists
api.getCometChatGroup(GUID).then((group) => {
if (group?.data) {
// group already exists in CometChat so we don't have to create it
launchCometChat(group.data.guid);
return;
}
// Otherwise, create new CometChat group for this Daily room
const GROUP_NAME = roomName;
const GROUP_TYPE = 'public';
const newGroup = new window.CometChatWidget.CometChat.Group(GUID, GROUP_NAME, GROUP_TYPE);
// CREATE GROUP WITH ROOM INFO
window.CometChatWidget.createOrUpdateGroup(newGroup)
.then(() => {
// Proceed with launching your Chat Widget
launchCometChat(GROUP_NAME);
})
.catch((e) => {
// handle error
console.error(e);
});
});
};
// INITIATE COMETCHAT WIDGET!
useEffect(() => {
// don't initiate chat if it's already there
if (loaded) return;
window.CometChatWidget.init({
appID: process.env.REACT_APP_COMET_CHAT_APP_ID,
appRegion: process.env.REACT_APP_COMET_CHAT_APP_REGION,
authKey: process.env.REACT_APP_COMET_CHAT_APP_AUTH_KEY,
}).then(
() => {
const UID = localParticipant.session_id;
const user = new window.CometChatWidget.CometChat.User(UID);
user.setName(localParticipant.user_name);
// CREATE USER WITH LOCAL PARTICIPANT INFO
window.CometChatWidget.createOrUpdateUser(user).then((user) => {
// Proceed with user login
window.CometChatWidget.login({
uid: UID,
}).then(() => {
createCometChatGroup();
});
});
},
(error) => {
console.error('Initialization failed with error:', error);
//Check the reason for error and take appropriate action.
},
);
}, [localParticipant.session_id, localParticipant.user_name, loaded, roomName]);
return (
<aside className={`chat ${!showChat ? 'hidden' : ''}`}>
<div id="cometchat"></div>
</aside>
);
}