maaz-ht
commited on
Commit
·
64242ab
1
Parent(s):
718af96
ui improved
Browse files
streaming-react-app/src/RoomConfig.tsx
CHANGED
@@ -1,22 +1,23 @@
|
|
1 |
-
import Stack from
|
2 |
-
import TextField from
|
3 |
-
import {isValidRoomID, isValidPartialRoomID} from
|
4 |
-
import {useCallback, useEffect, useState} from
|
5 |
-
import Button from
|
6 |
-
import {useSocket} from
|
7 |
-
import FormGroup from
|
8 |
-
import FormControlLabel from
|
9 |
-
import Checkbox from
|
10 |
-
import {RoomState} from
|
11 |
-
import setURLParam from
|
12 |
-
import {getURLParams} from
|
13 |
import {
|
14 |
JoinRoomConfig,
|
15 |
Roles,
|
16 |
ServerState,
|
17 |
StreamingStatus,
|
18 |
-
} from
|
19 |
-
import Alert from
|
|
|
20 |
|
21 |
function capitalize(str: string): string {
|
22 |
return str.charAt(0).toUpperCase() + str.slice(1);
|
@@ -35,22 +36,22 @@ export default function RoomConfig({
|
|
35 |
onJoinRoomOrUpdateRoles,
|
36 |
streamingStatus,
|
37 |
}: Props) {
|
38 |
-
const {socket, clientID} = useSocket();
|
39 |
|
40 |
const urlParams = getURLParams();
|
41 |
const roomIDParam = urlParams.roomID;
|
42 |
const autoJoinRoom = urlParams.autoJoin;
|
43 |
|
44 |
const [roomID, setRoomID] = useState<string>(
|
45 |
-
(roomIDParam ??
|
46 |
);
|
47 |
const [roomIDError, setRoomIDError] = useState<boolean>(false);
|
48 |
-
const [roles, setRoles] = useState<{speaker: boolean; listener: boolean}>({
|
49 |
speaker: false,
|
50 |
listener: false,
|
51 |
});
|
52 |
const [lockServer, setLockServer] = useState<boolean>(false);
|
53 |
-
const [lockServerName, setLockServerName] = useState<string>(
|
54 |
|
55 |
const [joinInProgress, setJoinInProgress] = useState<boolean>(false);
|
56 |
const [didAttemptAutoJoin, setDidAttemptAutoJoin] = useState<boolean>(false);
|
@@ -66,39 +67,39 @@ export default function RoomConfig({
|
|
66 |
const onJoinRoom = useCallback(
|
67 |
(createNewRoom: boolean) => {
|
68 |
if (socket == null) {
|
69 |
-
console.error(
|
70 |
return;
|
71 |
}
|
72 |
console.debug(`Attempting to join roomID ${roomID}...`);
|
73 |
|
74 |
const lockServerValidated: string | null =
|
75 |
-
lockServer && roles[
|
76 |
|
77 |
setJoinInProgress(true);
|
78 |
|
79 |
const configObject: JoinRoomConfig = {
|
80 |
roles: (Object.keys(roles) as Array<Roles>).filter(
|
81 |
-
(role) => roles[role] === true
|
82 |
),
|
83 |
lockServerName: lockServerValidated,
|
84 |
};
|
85 |
|
86 |
socket.emit(
|
87 |
-
|
88 |
clientID,
|
89 |
createNewRoom ? null : roomID,
|
90 |
configObject,
|
91 |
(result) => {
|
92 |
-
console.log(
|
93 |
if (createNewRoom) {
|
94 |
setRoomID(result.roomID);
|
95 |
}
|
96 |
if (onJoinRoomOrUpdateRoles != null) {
|
97 |
onJoinRoomOrUpdateRoles();
|
98 |
}
|
99 |
-
setURLParam(
|
100 |
setJoinInProgress(false);
|
101 |
-
}
|
102 |
);
|
103 |
},
|
104 |
[
|
@@ -109,7 +110,7 @@ export default function RoomConfig({
|
|
109 |
roles,
|
110 |
roomID,
|
111 |
socket,
|
112 |
-
]
|
113 |
);
|
114 |
|
115 |
useEffect(() => {
|
@@ -126,11 +127,11 @@ export default function RoomConfig({
|
|
126 |
joinInProgress === false &&
|
127 |
roomIDFromServer == null
|
128 |
) {
|
129 |
-
console.debug(
|
130 |
|
131 |
onJoinRoom(false);
|
132 |
} else {
|
133 |
-
console.debug(
|
134 |
isValidAllInputs,
|
135 |
joinInProgress,
|
136 |
roomIDFromServer,
|
@@ -148,86 +149,129 @@ export default function RoomConfig({
|
|
148 |
]);
|
149 |
|
150 |
return (
|
151 |
-
<Stack direction="column" spacing="12px">
|
152 |
-
<Stack
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
variant="
|
176 |
-
disabled={
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
|
|
|
|
|
|
187 |
<div>
|
188 |
<Button
|
|
|
189 |
variant="contained"
|
190 |
disabled={
|
191 |
-
|
192 |
joinInProgress ||
|
193 |
-
streamingStatus !==
|
194 |
}
|
195 |
-
onClick={() => onJoinRoom(
|
196 |
-
|
|
|
197 |
</Button>
|
198 |
</div>
|
199 |
-
|
200 |
-
</
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
</Stack>
|
202 |
|
203 |
<FormGroup>
|
204 |
<Stack direction="row" justifyContent="space-between">
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
</Stack>
|
226 |
|
227 |
-
{urlParams.enableServerLock && roles[
|
228 |
<>
|
229 |
<FormControlLabel
|
230 |
-
disabled={streamingStatus !==
|
231 |
control={
|
232 |
<Checkbox
|
233 |
checked={lockServer}
|
@@ -243,10 +287,10 @@ export default function RoomConfig({
|
|
243 |
</FormGroup>
|
244 |
|
245 |
{urlParams.enableServerLock &&
|
246 |
-
roles[
|
247 |
lockServer && (
|
248 |
<TextField
|
249 |
-
disabled={streamingStatus !==
|
250 |
label="Enter Your Name + Expected Lock End Time"
|
251 |
variant="outlined"
|
252 |
value={lockServerName}
|
|
|
1 |
+
import Stack from "@mui/material/Stack";
|
2 |
+
import TextField from "@mui/material/TextField";
|
3 |
+
import { isValidRoomID, isValidPartialRoomID } from "./generateNewRoomID";
|
4 |
+
import { useCallback, useEffect, useState } from "react";
|
5 |
+
import Button from "@mui/material/Button";
|
6 |
+
import { useSocket } from "./useSocket";
|
7 |
+
import FormGroup from "@mui/material/FormGroup";
|
8 |
+
import FormControlLabel from "@mui/material/FormControlLabel";
|
9 |
+
import Checkbox from "@mui/material/Checkbox";
|
10 |
+
import { RoomState } from "./types/RoomState";
|
11 |
+
import setURLParam from "./setURLParam";
|
12 |
+
import { getURLParams } from "./URLParams";
|
13 |
import {
|
14 |
JoinRoomConfig,
|
15 |
Roles,
|
16 |
ServerState,
|
17 |
StreamingStatus,
|
18 |
+
} from "./types/StreamingTypes";
|
19 |
+
import Alert from "@mui/material/Alert";
|
20 |
+
import { Paper, Typography } from "@mui/material";
|
21 |
|
22 |
function capitalize(str: string): string {
|
23 |
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
36 |
onJoinRoomOrUpdateRoles,
|
37 |
streamingStatus,
|
38 |
}: Props) {
|
39 |
+
const { socket, clientID } = useSocket();
|
40 |
|
41 |
const urlParams = getURLParams();
|
42 |
const roomIDParam = urlParams.roomID;
|
43 |
const autoJoinRoom = urlParams.autoJoin;
|
44 |
|
45 |
const [roomID, setRoomID] = useState<string>(
|
46 |
+
(roomIDParam ?? "").toUpperCase()
|
47 |
);
|
48 |
const [roomIDError, setRoomIDError] = useState<boolean>(false);
|
49 |
+
const [roles, setRoles] = useState<{ speaker: boolean; listener: boolean }>({
|
50 |
speaker: false,
|
51 |
listener: false,
|
52 |
});
|
53 |
const [lockServer, setLockServer] = useState<boolean>(false);
|
54 |
+
const [lockServerName, setLockServerName] = useState<string>("");
|
55 |
|
56 |
const [joinInProgress, setJoinInProgress] = useState<boolean>(false);
|
57 |
const [didAttemptAutoJoin, setDidAttemptAutoJoin] = useState<boolean>(false);
|
|
|
67 |
const onJoinRoom = useCallback(
|
68 |
(createNewRoom: boolean) => {
|
69 |
if (socket == null) {
|
70 |
+
console.error("Socket is null, cannot join room");
|
71 |
return;
|
72 |
}
|
73 |
console.debug(`Attempting to join roomID ${roomID}...`);
|
74 |
|
75 |
const lockServerValidated: string | null =
|
76 |
+
lockServer && roles["speaker"] ? lockServerName : null;
|
77 |
|
78 |
setJoinInProgress(true);
|
79 |
|
80 |
const configObject: JoinRoomConfig = {
|
81 |
roles: (Object.keys(roles) as Array<Roles>).filter(
|
82 |
+
(role) => roles[role] === true
|
83 |
),
|
84 |
lockServerName: lockServerValidated,
|
85 |
};
|
86 |
|
87 |
socket.emit(
|
88 |
+
"join_room",
|
89 |
clientID,
|
90 |
createNewRoom ? null : roomID,
|
91 |
configObject,
|
92 |
(result) => {
|
93 |
+
console.log("join_room result:", result);
|
94 |
if (createNewRoom) {
|
95 |
setRoomID(result.roomID);
|
96 |
}
|
97 |
if (onJoinRoomOrUpdateRoles != null) {
|
98 |
onJoinRoomOrUpdateRoles();
|
99 |
}
|
100 |
+
setURLParam("roomID", result.roomID);
|
101 |
setJoinInProgress(false);
|
102 |
+
}
|
103 |
);
|
104 |
},
|
105 |
[
|
|
|
110 |
roles,
|
111 |
roomID,
|
112 |
socket,
|
113 |
+
]
|
114 |
);
|
115 |
|
116 |
useEffect(() => {
|
|
|
127 |
joinInProgress === false &&
|
128 |
roomIDFromServer == null
|
129 |
) {
|
130 |
+
console.debug("Attempting to auto-join room...");
|
131 |
|
132 |
onJoinRoom(false);
|
133 |
} else {
|
134 |
+
console.debug("Unable to auto-join room", {
|
135 |
isValidAllInputs,
|
136 |
joinInProgress,
|
137 |
roomIDFromServer,
|
|
|
149 |
]);
|
150 |
|
151 |
return (
|
152 |
+
<Stack direction="column" spacing="12px" sx={{ width: "80%" }}>
|
153 |
+
<Stack spacing={2}>
|
154 |
+
<Paper
|
155 |
+
elevation={3}
|
156 |
+
style={{
|
157 |
+
padding: "1em",
|
158 |
+
borderRadius: "15px",
|
159 |
+
border: "1px solid #ccc",
|
160 |
+
}}
|
161 |
+
>
|
162 |
+
<Stack
|
163 |
+
direction="column"
|
164 |
+
spacing={1}
|
165 |
+
sx={{ alignItems: "center", width: "100%" }}
|
166 |
+
>
|
167 |
+
<Typography fontWeight={600}>Join an Existing Room</Typography>
|
168 |
+
<Typography variant="body2">
|
169 |
+
Join an existing room by entering the room code provided by the
|
170 |
+
host.
|
171 |
+
</Typography>
|
172 |
+
<TextField
|
173 |
+
size="small"
|
174 |
+
label="Room Code"
|
175 |
+
placeholder="Enter Room Code"
|
176 |
+
variant="outlined"
|
177 |
+
disabled={roomState?.room_id != null}
|
178 |
+
value={roomID}
|
179 |
+
error={roomIDError}
|
180 |
+
onChange={(e) => {
|
181 |
+
const id = e.target.value.toUpperCase();
|
182 |
+
if (isValidPartialRoomID(id)) {
|
183 |
+
setRoomIDError(false);
|
184 |
+
setRoomID(id);
|
185 |
+
} else {
|
186 |
+
setRoomIDError(true);
|
187 |
+
}
|
188 |
+
}}
|
189 |
+
sx={{ width: "100%" }}
|
190 |
+
/>
|
191 |
<div>
|
192 |
<Button
|
193 |
+
sx={{borderRadius: "20px"}}
|
194 |
variant="contained"
|
195 |
disabled={
|
196 |
+
isValidAllInputs === false ||
|
197 |
joinInProgress ||
|
198 |
+
streamingStatus !== "stopped"
|
199 |
}
|
200 |
+
onClick={() => onJoinRoom(false)}
|
201 |
+
>
|
202 |
+
{roomState?.room_id != null ? "Update Roles" : "Join Room"}
|
203 |
</Button>
|
204 |
</div>
|
205 |
+
</Stack>
|
206 |
+
</Paper>
|
207 |
+
|
208 |
+
<Paper
|
209 |
+
elevation={3}
|
210 |
+
style={{
|
211 |
+
padding: "1em",
|
212 |
+
borderRadius: "15px",
|
213 |
+
border: "1px solid #ccc",
|
214 |
+
}}
|
215 |
+
>
|
216 |
+
<Stack
|
217 |
+
direction="column"
|
218 |
+
spacing={1}
|
219 |
+
sx={{ alignItems: "center", width: "100%" }}
|
220 |
+
>
|
221 |
+
<Typography fontWeight={600}>Create a New Room</Typography>
|
222 |
+
<Typography variant="body2">
|
223 |
+
Start a new room and invite others to join you in collaborative
|
224 |
+
sessions or discussions.
|
225 |
+
</Typography>
|
226 |
+
|
227 |
+
{roomState?.room_id == null && (
|
228 |
+
<div>
|
229 |
+
<Button
|
230 |
+
sx={{borderRadius: "20px"}}
|
231 |
+
variant="contained"
|
232 |
+
disabled={
|
233 |
+
roomState?.room_id != null ||
|
234 |
+
joinInProgress ||
|
235 |
+
streamingStatus !== "stopped"
|
236 |
+
}
|
237 |
+
onClick={() => onJoinRoom(true)}
|
238 |
+
>
|
239 |
+
{"Create New Room"}
|
240 |
+
</Button>
|
241 |
+
</div>
|
242 |
+
)}
|
243 |
+
</Stack>
|
244 |
+
</Paper>
|
245 |
</Stack>
|
246 |
|
247 |
<FormGroup>
|
248 |
<Stack direction="row" justifyContent="space-between">
|
249 |
+
{Object.keys(roles).map((role) => {
|
250 |
+
return (
|
251 |
+
<FormControlLabel
|
252 |
+
disabled={streamingStatus !== "stopped"}
|
253 |
+
key={role}
|
254 |
+
control={
|
255 |
+
<Checkbox
|
256 |
+
checked={roles[role]}
|
257 |
+
onChange={(event: React.ChangeEvent<HTMLInputElement>) => {
|
258 |
+
setRoles((prevRoles) => ({
|
259 |
+
...prevRoles,
|
260 |
+
[role]: event.target.checked,
|
261 |
+
}));
|
262 |
+
}}
|
263 |
+
/>
|
264 |
+
}
|
265 |
+
label={capitalize(role)}
|
266 |
+
/>
|
267 |
+
);
|
268 |
+
})}
|
269 |
</Stack>
|
270 |
|
271 |
+
{urlParams.enableServerLock && roles["speaker"] === true && (
|
272 |
<>
|
273 |
<FormControlLabel
|
274 |
+
disabled={streamingStatus !== "stopped"}
|
275 |
control={
|
276 |
<Checkbox
|
277 |
checked={lockServer}
|
|
|
287 |
</FormGroup>
|
288 |
|
289 |
{urlParams.enableServerLock &&
|
290 |
+
roles["speaker"] === true &&
|
291 |
lockServer && (
|
292 |
<TextField
|
293 |
+
disabled={streamingStatus !== "stopped"}
|
294 |
label="Enter Your Name + Expected Lock End Time"
|
295 |
variant="outlined"
|
296 |
value={lockServerName}
|