-
Notifications
You must be signed in to change notification settings - Fork 2
/
Room.js
90 lines (82 loc) · 2.55 KB
/
Room.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 React, { useState, useEffect } from "react";
import Video from "twilio-video";
import Participant from "./Participant";
import TodoList from "./TodoList";
const Room = ({ roomName, token, handleLogout }) => {
const [room, setRoom] = useState(null);
const [participants, setParticipants] = useState([]);
useEffect(() => {
const participantConnected = (participant) => {
setParticipants((prevParticipants) => [...prevParticipants, participant]);
};
const participantDisconnected = (participant) => {
setParticipants((prevParticipants) =>
prevParticipants.filter((p) => p !== participant)
);
};
Video.connect(token, {
name: roomName
}).then((room) => {
setRoom(room);
room.on("participantConnected", participantConnected);
room.on("participantDisconnected", participantDisconnected);
room.participants.forEach(participantConnected);
});
return () => {
setRoom((currentRoom) => {
if (currentRoom && currentRoom.localParticipant.state === "connected") {
currentRoom.localParticipant.tracks.forEach(function (
trackPublication
) {
trackPublication.track.stop();
});
currentRoom.disconnect();
return null;
} else {
return currentRoom;
}
});
};
}, [roomName, token]);
const remoteParticipants = participants.map((participant) => (
<Participant key={participant.sid} participant={participant} />
));
return (
<div className="room">
<h2>Room: {roomName}</h2>
<button onClick={handleLogout}>Log out</button>
<div className="local-participant row">
{room ? (
<div className="col-6">
<div>
<Participant
key={room.localParticipant.sid}
participant={room.localParticipant}
/>
</div>
<div className="remote-participants">{remoteParticipants}</div>
</div>
) : (
""
)}
{room ? (
<div className="col-6">
<div class="happydays">
<div className="row">
<TodoList participant={room.localParticipant}></TodoList>
</div>
{participants.map((participant) => (
<div className="row">
<TodoList participant={participant}></TodoList>
</div>
))}
</div>
</div>
) : (
""
)}
</div>
</div>
);
};
export default Room;